From c82df663df975ddfbffb36cd3c247ae2d40b4ef8 Mon Sep 17 00:00:00 2001 From: igor Date: Sun, 4 May 2025 15:08:57 +0200 Subject: [PATCH] pridana API metoda getallgrouped, implementovane nacitanie dat do Dashboard z backend --- api.php | 29 +++-- config.php | 4 + lib/functions.inc.php | 194 +++++++++++++++++++++++++++++++++ webapp/src/backend.js | 4 + webapp/src/views/Dashboard.vue | 108 ++++++------------ 5 files changed, 257 insertions(+), 82 deletions(-) create mode 100644 lib/functions.inc.php diff --git a/api.php b/api.php index fd9fe03..cd04fe4 100644 --- a/api.php +++ b/api.php @@ -2,6 +2,8 @@ include_once 'config.php'; $action = $_REQUEST['action']; +$result = null; +$error = null; switch ($action) { default: @@ -9,7 +11,9 @@ switch ($action) { $result = help(); break; case 'add': - $result = reportAdd($_REQUEST['title'], $_REQUEST['description'], $_REQUEST['status'], $_REQUEST['group'], $_REQUEST['priority']); + $report_id = reportAdd($_REQUEST['title'], $_REQUEST['description'], $_REQUEST['status'], $_REQUEST['group'], $_REQUEST['priority']); + if ($report_id === false) $error = 'Report add failed'; + $result = array('report_id' => $report_id); break; case 'update': $result = reportUpdate($_REQUEST['report_id'], $_REQUEST['title'], $_REQUEST['description'], $_REQUEST['status'], $_REQUEST['group'], $_REQUEST['priority']); @@ -23,13 +27,26 @@ switch ($action) { case 'getall': $result = reportGetAll($_REQUEST['status']); break; + case 'getallgrouped': + $result = reportGetAllGrouped($_REQUEST['status']); + break; } header('Content-Type: application/json'); -echo json_encode($result); +$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*'; +header('Access-Control-Allow-Origin: ' . $origin); +header('Access-Control-Allow-Credentials: true'); +header('Access-Control-Allow-Methods: GET, POST, OPTIONS'); +header('Access-Control-Allow-Headers: Origin, Content-Type, Accept'); +echo json_encode( + is_null($error) + ? array('status' => 'OK', 'data' => $result) + : array('status' => 'ERROR', 'msg' => $error), +); exit; -function help() { +function help() +{ return [ 'actions' => [ 'help' => 'Show this help', @@ -37,10 +54,8 @@ function help() { 'update' => 'Update report', 'delete' => 'Delete report', 'get' => 'Get report', - 'getall' => 'Get all reports' + 'getall' => 'Get all reports', + 'getallgrouped' => 'Get all reports grouped by group', ] ]; } - - -?> \ No newline at end of file diff --git a/config.php b/config.php index e592607..933ce57 100644 --- a/config.php +++ b/config.php @@ -1,5 +1,9 @@ create('options', [ + 'key' => [ + 'VARCHAR(64)', + 'NOT NULL', + 'UNIQUE' + ], + 'value' => [ + 'TEXT', + 'NOT NULL' + ], + 'created_at' => [ + 'DATETIME', + 'DEFAULT CURRENT_TIMESTAMP' + ] + ]); + option('version', '0'); + $db_version = '0'; + } + if ($db_version === '0') { + $db->create('reports', [ + 'report_id' => [ + 'INTEGER', + 'PRIMARY KEY', + 'AUTOINCREMENT' + ], + 'report_title' => [ + 'VARCHAR(255)', + 'DEFAULT NULL' + ], + 'report_description' => [ + 'TEXT', + 'DEFAULT NULL' + ], + 'report_status' => [ + 'INTEGER', + 'DEFAULT 0' + ], + 'report_group' => [ + 'VARCHAR(255)', + 'DEFAULT NULL' + ], + 'report_priority' => [ + 'INTEGER', + 'DEFAULT 0' + ], + 'created_dt' => [ + 'DATETIME', + 'DEFAULT NULL' + ], + ]); + option('version', '1'); + $db_version = '1'; + } + if ($db_version === '1') { + $db->create('attachments', [ + 'attachment_id' => [ + 'INTEGER', + 'PRIMARY KEY', + 'AUTOINCREMENT' + ], + 'report_id' => [ + 'INTEGER', + 'NOT NULL' + ], + 'attachment_type' => [ + 'VARCHAR(255)', + 'DEFAULT NULL' + ], + 'attachment_content' => [ + 'TEXT', + 'DEFAULT NULL' + ], + 'created_dt' => [ + 'DATETIME', + 'DEFAULT NULL' + ], + 'updated_dt' => [ + 'DATETIME', + 'DEFAULT NULL' + ], + ]); + option('version', '2'); + $db_version = '2'; + } +} + +function option($key, $value = null) +{ + global $db; + if (tableExits('options') === null) { + return null; + } + if ($value === null) { + return $db->get('options', 'value', [ + 'key' => $key + ]); + } + $exits = $db->get('options', 'value', [ + 'key' => $key + ]); + if ($exits !== null) { + return $db->update('options', [ + 'value' => $value + ], [ + 'key' => $key + ]); + } + return $db->insert('options', [ + 'key' => $key, + 'value' => $value + ]); +} + +function tableExits($table) { + global $db; + return $db->get('sqlite_master', 'name', [ + 'type' => 'table', + 'name' => $table + ]); +} + +/** + * Reports + */ +function reportAdd($title, $description, $status = 0, $group = null, $priority = 0) { + global $db; + $status = intval($status); + $priority = intval($priority); + $db->insert('reports', [ + 'report_title' => $title, + 'report_description' => $description, + 'report_status' => $status, + 'report_group' => $group, + 'report_priority' => $priority, + 'created_dt' => date('Y-m-d H:i:s') + ]); + return $db->id(); +} + +function reportUpdate($report_id, $title, $description, $status = 0, $group = null, $priority = 0) { + global $db; + return $db->update('reports', [ + 'report_title' => $title, + 'report_description' => $description, + 'report_status' => $status, + 'report_group' => $group, + 'report_priority' => $priority, + ], [ + 'report_id' => $report_id + ]); +} + +function reportDelete($report_id) { + global $db; + return $db->delete('reports', [ + 'report_id' => $report_id + ]); +} + +function reportGet($report_id) { + global $db; + return $db->get('reports', '*', [ + 'report_id' => $report_id + ]); +} + +function reportGetAll($status = null) { + global $db; + return $db->select('reports', '*', [ + 'ORDER' => 'report_priority', + 'report_status' => $status + ]); +} + +function reportGetAllGrouped($status = null) { + if ($status === null) $status = array(0, 1, 2, 3); + $all = reportGetAll($status); + $groups = []; + foreach ($all as $report) { + $groups[$report['report_status']][] = $report; + } + return $groups; +} + +?> \ No newline at end of file diff --git a/webapp/src/backend.js b/webapp/src/backend.js index 2f3912d..32430b4 100644 --- a/webapp/src/backend.js +++ b/webapp/src/backend.js @@ -73,4 +73,8 @@ export const backend = { return this.callPromise('getall', {}); }, + getAllGrouped(status) { + return this.callPromise('getallgrouped', {}); + }, + }; diff --git a/webapp/src/views/Dashboard.vue b/webapp/src/views/Dashboard.vue index 77fca60..705fcc8 100644 --- a/webapp/src/views/Dashboard.vue +++ b/webapp/src/views/Dashboard.vue @@ -4,7 +4,7 @@

Nezaradené

@@ -30,7 +30,7 @@

Čakajúce

@@ -56,7 +56,7 @@

Rozpracované

@@ -82,7 +82,7 @@

Hotové

@@ -120,13 +120,14 @@ function onDragEnd(evt) { itemDragable.value = 0; } function isDragable(element) { - return itemDragable.value === element.id; + return itemDragable.value === element.report_id; }