diff --git a/db/postgresql/issue382_sync_schedule.sql b/db/postgresql/issue382_sync_schedule.sql new file mode 100644 index 0000000000000000000000000000000000000000..5c26a77ac8a53a5c858bce2bba6b22f9ebf4fd83 --- /dev/null +++ b/db/postgresql/issue382_sync_schedule.sql @@ -0,0 +1,44 @@ + +CREATE SEQUENCE seq_logs_sync_1c + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +CREATE TABLE logs_sync_1c ( + id integer DEFAULT nextval('seq_logs_sync_1c'::regclass) NOT NULL, + date timestamp(0) without time zone DEFAULT now() NOT NULL +); + + +ALTER TABLE ONLY logs_sync_1c + ADD CONSTRAINT logs_sync_pk PRIMARY KEY (id); + + +CREATE OR REPLACE FUNCTION public.logs_sync_1c() + RETURNS integer +LANGUAGE plpgsql +AS $function$ +declare vID int default -1; + vRowUpd int; +begin + INSERT INTO logs_sync_1c DEFAULT VALUES returning id into vID; + get diagnostics vRowUpd = ROW_COUNT; + RETURN vRowUpd - 1; +end +$function$; + + +CREATE OR REPLACE FUNCTION public.getAllSyncDates() + RETURNS TABLE("Date" timestamp) +LANGUAGE plpgsql +AS $function$ +BEGIN + return query + SELECT logs_sync_1c."date" as "Date" + FROM logs_sync_1c + ORDER BY logs_sync_1c."date" DESC; +END +$function$; \ No newline at end of file diff --git a/~dev_rating/application/classes/Controller/Api/V0/SyncLogs.php b/~dev_rating/application/classes/Controller/Api/V0/SyncLogs.php new file mode 100644 index 0000000000000000000000000000000000000000..cf7a7f868da0527c48aa44cbeaf059a8f6d008f3 --- /dev/null +++ b/~dev_rating/application/classes/Controller/Api/V0/SyncLogs.php @@ -0,0 +1,35 @@ +<?php + +class Controller_Api_V0_SyncLogs extends Controller_Handler_Api +{ + + /** + * @api {get} api/v0/LogSync Log synchronization with 1C + * @apiName Log synchronization with 1C + * @apiGroup Sync + * @apiVersion 0.1.0 + * @apiParam {String} token Api key + * @apiDescription This method logs 1C synchronization timestamp + * @apiExample {curl} Example usage: + * curl -i http://grade/~dev_rating/api/v0/SyncLogs?token=osw839hfgh9a23hgfh92hasff232f2oasf + * @apiSuccessExample {json} Success-Response: + * HTTP/1.1 200 OK + * { + * "response": { + * } + * } + * + */ + public function action_get_index() + { + try { + $response = []; + Model_System::Log1CSync(); + return $response; + } catch (Exception $e) { + $this->badRequestError($e->getMessage()); + } + + return null; + } +} diff --git a/~dev_rating/application/classes/Controller/Authentication.php b/~dev_rating/application/classes/Controller/Authentication.php index 694d03c97500d4c1bd17e6d68ccf1077fc727b14..0e1a227932d71e963b1f4f1c1d98d9ab4a306ff3 100644 --- a/~dev_rating/application/classes/Controller/Authentication.php +++ b/~dev_rating/application/classes/Controller/Authentication.php @@ -25,6 +25,7 @@ class Controller_Authentication extends Controller 'Blocks' => [ 'News' => Model_System::getChangeLog('news.md'), 'Updates' => Model_System::getChangeLog('updates.md'), + 'Syncs' => Model_System::getSyncLog(), ] ]); $this->response->body($this->twig); diff --git a/~dev_rating/application/classes/Model/System.php b/~dev_rating/application/classes/Model/System.php index 303d12d90db749fc55a5a494e6e0136f4ea6695a..ad3fe48999dd168d1bef87c839f8260878577300 100644 --- a/~dev_rating/application/classes/Model/System.php +++ b/~dev_rating/application/classes/Model/System.php @@ -39,6 +39,28 @@ class Model_System extends Model return $log; } + public static function getSyncLog() { + $query = 'SELECT * FROM getAllSyncDates()'; + $result = DB::query(Database::SELECT, $query) + ->execute(); + + $log = []; + foreach ($result as $row) { + $log[] = [ + 'date' => substr($row["Date"], 0, 10), + 'text' => substr($row["Date"], strpos($row["Date"], ' '), 9), + ]; + } + + return $log; + } + + public static function Log1CSync() { + $query = 'SELECT * FROM logs_sync_1c() AS "Num"'; + $result = DB::query(Database::SELECT, $query) + ->execute()->get("Num"); + return $result; + } /** * Clean comments of json content and decode it with json_decode(). diff --git a/~dev_rating/application/routes/api/v0.php b/~dev_rating/application/routes/api/v0.php index 2ee5f32724433a4d8cfadcc757609aa475b20aa6..ad71609c1d9502daa1fcdeb2f7cc310652b383fc 100644 --- a/~dev_rating/application/routes/api/v0.php +++ b/~dev_rating/application/routes/api/v0.php @@ -11,6 +11,17 @@ Route::set('apiv0:final-report', 'api/v0/final-report') 'controller' => 'FinalReport', ]); +Route::set('apiv0:SyncLogs', 'api/v0/sync-logs') + ->filter(function($route, $params, $request) { + $params['action'] = strtolower($request->method()) . '_' . $params['action']; + return $params; + }) + ->defaults([ + 'action' => 'index', + 'directory' => 'Api/V0', + 'controller' => 'SyncLogs', + ]); + Route::set('apiv0:student', 'api/v0/student') ->filter(function($route, $params, $request) { $params['action'] = strtolower($request->method()) . '_' . $params['action'];