From 49ef11d183b9de9c2a8b1f706d8052a388945f42 Mon Sep 17 00:00:00 2001 From: rem Date: Tue, 12 Dec 2023 17:35:58 +0100 Subject: [PATCH] :sparkles: add GET /ai/data to get all users info --- app/database_init.php | 11 +++++ app/gateway/ai_gateway.php | 97 ++++++++++++++++++++++++++++++++++++++ app/routes.php | 22 ++++++++- 3 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 app/gateway/ai_gateway.php diff --git a/app/database_init.php b/app/database_init.php index 7fadbe4..5c4ee1b 100644 --- a/app/database_init.php +++ b/app/database_init.php @@ -17,6 +17,7 @@ class DatabaseInit $this->con = (new DatabaseCon)->connect(); $this->createUserTable(); $this->createFileTable(); + $this->createTrainedModelTable(); } catch (PDOException $e) { throw new PDOException($e->getMessage(), $e->getCode(), $e); } @@ -48,4 +49,14 @@ class DatabaseInit $this->con->executeQuery($query); } + + private function createTrainedModelTable() { + $query = 'CREATE TABLE IF NOT EXISTS trained_model ( + id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, + user_id UUID REFERENCES `user`(`id`) ON DELETE CASCADE, + category VARCHAR(50), + model VARCHAR(1000));'; + + $this->con->executeQuery($query); + } } diff --git a/app/gateway/ai_gateway.php b/app/gateway/ai_gateway.php new file mode 100644 index 0000000..bed3a4a --- /dev/null +++ b/app/gateway/ai_gateway.php @@ -0,0 +1,97 @@ +con = (new DatabaseCon)->connect(); + } catch (PDOException $e) { + throw new PDOException($e->getMessage(), $e->getCode(), $e); + } + } + + public function getUsersCategoryAndInfo() + { + $query = "SELECT u.id, f.category, f.info FROM user u, file f WHERE u.id = f.user_id ORDER BY u.id, f.category DESC;"; + try { + $this->con->executeQuery($query, array()); + $results = $this->con->getResults(); + } catch (PDOException $e) { + throw new PDOException($e->getMessage(), $e->getCode(), $e); + } + + $users = array(); + $tmp_user = null; + $tmp_category = null; + $cur_user = null; + + foreach ($results as $row) { + $user_uuid = $row['id']; + + // Check if user is the same as last user + if ($user_uuid !== $tmp_user) { + if ($tmp_user !== null) { + $users[] = $cur_user; + $tmp_category = null; + } + $tmp_user = $user_uuid; + + $cur_user = array( + 'uuid' => $user_uuid, + 'categories' => array() + ); + } + + $cur_category = $row['category']; + $cur_info = $row['info']; + + // Check if category already exist for cur_user + foreach ($cur_user['categories'] as $category) { + if (isset($category['name']) && $category['name'] === $cur_category) { + $tmp_category = true; + break; + } + } + + // Add category if it doesn't exists for cur_user + if (!$tmp_category) { + $cur_user['categories'][] = array( + "name" => $cur_category, + "infos" => array() + ); + } + + // Add info to current category + foreach ($cur_user['categories'] as &$c) { + if ($c['name'] === $cur_category) { + $c["infos"][] = array( + "json" => $cur_info + ); + break; + } + } + + error_log(json_encode($cur_user)); + } + + $users[] = $cur_user; + + $json = json_encode($users, JSON_PRETTY_PRINT); + return $json; + } + + public function insertModel(string $user_uuid, string $category, string $model) + { + return 0; + } +} diff --git a/app/routes.php b/app/routes.php index f9c3709..09a7e0d 100644 --- a/app/routes.php +++ b/app/routes.php @@ -3,6 +3,7 @@ declare(strict_types=1); require_once "gateway/user_gateway.php"; require_once "gateway/file_gateway.php"; +require_once "gateway/ai_gateway.php"; require_once "database_con.php"; require_once "token.php"; require_once "helpers.php"; @@ -16,9 +17,10 @@ use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\App; use Slim\Exception\HttpNotFoundException; -use gateway\UserGateway; -use Config\Token; +use Gateway\UserGateway; +use Gateway\AiGateway; use Gateway\FileGateway; +use Config\Token; return function (App $app) { $app->options('/{routes:.+}', function ($request, $response, $args) { @@ -265,6 +267,22 @@ return function (App $app) { return $res->withStatus(200); }); + // ===== IA ===== + $app->get('/ai/data', function(Request $req, Response $res) { + // TODO: Authentication python server + $json = (new AiGateway)->getUsersCategoryAndInfo(); + $res = $res->withHeader('Content-type','application/json'); + $res->getBody()->write($json); + return $res; + }); + + $app->post('/ai/data', function(Request $req, Response $res) { + // TODO: Authentication python server + // Check uuid, category, model in json + + return $res; + }); + $app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function ($request, $response) { throw new HttpNotFoundException($request); }); -- 2.36.3