From 3ad03104c5f31e1b0262098d4b022aac1e5ba300 Mon Sep 17 00:00:00 2001 From: rem Date: Tue, 12 Dec 2023 20:56:00 +0100 Subject: [PATCH] :sparkles: add support for POST /ai/data --- app/gateway/ai_gateway.php | 79 +++++++++++++++++++++++++++++++++++++- app/helpers.php | 9 +++++ app/routes.php | 29 +++++++++----- 3 files changed, 107 insertions(+), 10 deletions(-) diff --git a/app/gateway/ai_gateway.php b/app/gateway/ai_gateway.php index bed3a4a..d0f137f 100644 --- a/app/gateway/ai_gateway.php +++ b/app/gateway/ai_gateway.php @@ -4,8 +4,10 @@ namespace Gateway; use Config\DatabaseCon; use Config\Connection; +use Error; use PDOException; -use PhpParser\Node\Arg; +use PDO; +use PDORow; class AiGateway { @@ -90,8 +92,83 @@ class AiGateway return $json; } + public function addModel(string $user_uuid, string $category, string $model) + { + $res_exists = $this->checkIfCategoryExists($user_uuid, $category, $model); + + if ($res_exists === 1) { + $code = $this->insertModel($user_uuid, $category, $model); + } else if ($res_exists === 0) { + $code = $this->updateModel($user_uuid, $category, $model); + } else { + return -1; + } + + if ($code === -1) return -1; + return 0; + } + public function insertModel(string $user_uuid, string $category, string $model) { + error_log("INSERT SQL"); + error_log($user_uuid); + error_log($category); + error_log($model); + $query = "INSERT INTO trained_model VALUES(null, :user_uuid, :category, :model);"; + + try { + $this->con->executeQuery($query, array( + ':user_uuid' => array($user_uuid, PDO::PARAM_STR), + ':category' => array($category, PDO::PARAM_STR), + ':model' => array($model, PDO::PARAM_STR) + )); + } catch (PDOException) { + return -1; + } + return 0; + } + + public function updateModel(string $user_uuid, string $category, string $model) + { + $query = "UPDATE trained_model SET model = :model WHERE user_id = :user_uuid and category = :category;"; + + try { + $this->con->executeQuery($query, array( + ':user_uuid' => array($user_uuid, PDO::PARAM_STR), + ':category' => array($category, PDO::PARAM_STR), + ':model' => array($model, PDO::PARAM_STR) + )); + } catch (PDOException) { + return -1; + } + + return 0; + } + + public function checkIfCategoryExists(string $user_uuid, string $category) + { + $query = "SELECT category FROM trained_model WHERE category = :category and user_id = :user_uuid;"; + error_log("CHECK SQL"); + error_log($user_uuid); + error_log($category); + try { + error_log("AAAAAH"); + $this->con->executeQuery($query, array( + ':user_uuid' => array($user_uuid, PDO::PARAM_STR), + ':category' => array($category, PDO::PARAM_STR) + )); + error_log("BBBBBH"); + $results = $this->con->getResults(); + } catch (PDOException) { + return -1; + } + + error_log("AFTER CHECK SQL"); + + if (count($results) === 0) { + return 1; + } + return 0; } } diff --git a/app/helpers.php b/app/helpers.php index baadf85..55ab3b5 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -28,4 +28,13 @@ class Helpers return true; } + + static public function isUUID(string $uuid) + { + if (!is_string($uuid) || (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/', $uuid) !== 1)) { + return false; + } + + return true; + } } diff --git a/app/routes.php b/app/routes.php index 09a7e0d..79c39b8 100644 --- a/app/routes.php +++ b/app/routes.php @@ -61,7 +61,7 @@ return function (App $app) { return $res->withStatus(401); } $token = $req->getHeader('Authorization')[0]; - + $uuid = (new Token)->getUuidFromToken($token); $code = (new UserGateway)->deleteUser($uuid); @@ -150,7 +150,7 @@ return function (App $app) { return $res->withStatus(200); }); - + // Update Password $app->put('/user/password', function (Request $req, Response $res) { if (!(new Token)->verifyToken($req->getHeader('Authorization'))) { @@ -244,9 +244,9 @@ return function (App $app) { } $token = $req->getHeader('Authorization')[0]; $uuid = (new Token)->getUuidFromToken($token); - + $file = $req->getUploadedFiles()['file']; - + $info = $req->getParsedBody()['info']; $category = $req->getParsedBody()['SmartFit_Category']; $creation_date = $req->getParsedBody()['SmartFit_Date']; @@ -268,19 +268,30 @@ return function (App $app) { }); // ===== IA ===== - $app->get('/ai/data', function(Request $req, Response $res) { - // TODO: Authentication python server + $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 = $res->withHeader('Content-type', 'application/json'); $res->getBody()->write($json); return $res; }); - $app->post('/ai/data', function(Request $req, Response $res) { + $app->post('/ai/data', function (Request $req, Response $res) { // TODO: Authentication python server // Check uuid, category, model in json + + if (!Helpers::validJson((string) $req->getBody(), array("uuid", "category", "model"))) { + return $res->withStatus(400); + } + + $req_body = $req->getParsedBody(); + + if(!Helpers::isUUID($req_body['uuid'])) return $res->withStatus(400); - return $res; + $code = (new AiGateway)->addModel($req_body['uuid'], $req_body['category'], $req_body['model']); + if($code === -1) return $res->withStatus(500); + + return $res->withStatus(200); }); $app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function ($request, $response) {