add GET /ai/data to get all users info #4

Merged
remi.arnal merged 1 commits from ai_support into master 1 year ago

@ -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);
}
}

@ -0,0 +1,97 @@
<?php
namespace Gateway;
use Config\DatabaseCon;
use Config\Connection;
use PDOException;
use PhpParser\Node\Arg;
class AiGateway
{
private Connection $con;
public function __construct()
{
try {
$this->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;
}
}

@ -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);
});

Loading…
Cancel
Save