From 7987f2aaf11d289e299276955bba7591610a030b Mon Sep 17 00:00:00 2001
From: Anton Bagliy <taccessviolation@gmail.com>
Date: Wed, 20 Feb 2019 20:49:15 +0300
Subject: [PATCH] ADD: 1c sync logs table, tab in updates #382

---
 db/postgresql/issue382_sync_schedule.sql      | 44 +++++++++++++++++++
 .../classes/Controller/Api/V0/SyncLogs.php    | 35 +++++++++++++++
 .../classes/Controller/Authentication.php     |  1 +
 .../application/classes/Model/System.php      | 22 ++++++++++
 ~dev_rating/application/routes/api/v0.php     | 11 +++++
 5 files changed, 113 insertions(+)
 create mode 100644 db/postgresql/issue382_sync_schedule.sql
 create mode 100644 ~dev_rating/application/classes/Controller/Api/V0/SyncLogs.php

diff --git a/db/postgresql/issue382_sync_schedule.sql b/db/postgresql/issue382_sync_schedule.sql
new file mode 100644
index 000000000..5c26a77ac
--- /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 000000000..cf7a7f868
--- /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 694d03c97..0e1a22793 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 303d12d90..ad3fe4899 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 2ee5f3272..ad71609c1 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'];
-- 
GitLab