From 088b1a5281a27b2f248125dfcfb9bf1a99ca72bc Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 8 Jan 2024 10:49:13 +0100 Subject: [PATCH] wip --- [, | 0 public/api/index.php | 2 ++ sql/database.php | 4 +-- src/Api/API.php | 3 +- src/Api/Controller/APITeamController.php | 40 +++++++++++++++--------- src/Core/Gateway/TeamGateway.php | 39 ++++++++++++++--------- src/Core/Model/TeamModel.php | 9 ++++++ 7 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 [, diff --git a/[, b/[, new file mode 100644 index 0000000..e69de29 diff --git a/public/api/index.php b/public/api/index.php index 399309b..b7f966d 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -64,6 +64,8 @@ function getRoutes(): AltoRouter { $router->map("POST", "/admin/user/[i:id]/update", Action::noAuth(fn(int $id) => getAccountController()->updateUser($id))); $router->map("GET", "/admin/server-info", Action::noAuth(fn() => getServerController()->getServerInfo())); $router->map("GET", "/admin/list-team", Action::noAuth(fn() => getAPITeamController()->listTeam($_GET))); + $router->map("POST", "/admin/add-team", Action::noAuth(fn() => getAPITeamController()->addTeam())); + return $router; diff --git a/sql/database.php b/sql/database.php index f11a8fe..b18c750 100644 --- a/sql/database.php +++ b/sql/database.php @@ -36,7 +36,7 @@ function init_database(PDO $pdo): void { $accounts = new AccountGateway(new Connection($pdo)); $teams = new \IQBall\Core\Gateway\TeamGateway(new Connection($pdo)); - $defaultAccounts = ["maxime", "mael", "yanis", "vivien"]; + $defaultAccounts = ["maxime", "mael", "yanis", "vivien", "samuel"]; $defaultTeams = ["Lakers", "Celtics", "Bulls"]; foreach ($defaultAccounts as $name) { @@ -46,6 +46,6 @@ function init_database(PDO $pdo): void { } foreach ($defaultTeams as $name){ - $id = $teams->insert($name,"https://lebasketographe.fr/wp-content/uploads/2019/11/nom-equipes-nba.jpg","ff5733","85f251"); + $id = $teams->insert($name,"https://lebasketographe.fr/wp-content/uploads/2019/11/nom-equipes-nba.jpg","#1a2b3c","#FF00AA"); } } \ No newline at end of file diff --git a/src/Api/API.php b/src/Api/API.php index d751c75..2d5e8b3 100644 --- a/src/Api/API.php +++ b/src/Api/API.php @@ -26,6 +26,8 @@ class API { header('Content-type: application/json'); echo $response->getJson(); } elseif (get_class($response) != HttpResponse::class) { + var_dump($response); + var_dump(get_class($response)); throw new Exception("API returned unknown Http Response"); } } @@ -56,7 +58,6 @@ class API { } if ($action->getAuthType() == Action::AUTH_ADMIN && !$account->getUser()->isAdmin()) { - return new JsonHttpResponse([ValidationFail::unauthorized()], HttpCodes::UNAUTHORIZED); } } diff --git a/src/Api/Controller/APITeamController.php b/src/Api/Controller/APITeamController.php index 8c68c55..2c1fa90 100644 --- a/src/Api/Controller/APITeamController.php +++ b/src/Api/Controller/APITeamController.php @@ -6,10 +6,12 @@ use IQBall\App\Control; use IQBall\Core\Data\Account; use IQBall\Core\Data\Team; use IQBall\Core\Data\TeamInfo; +use IQBall\Core\Http\HttpCodes; use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\JsonHttpResponse; use IQBall\Core\Model\TeamModel; +use IQBall\Core\Validation\DefaultValidators; use IQBall\Core\Validation\Validators; class APITeamController { @@ -22,27 +24,35 @@ class APITeamController { $this->teamModel = $teamModel; } + /** + * @param array $req_params + * @return HttpResponse + */ public function listTeam(array $req_params): HttpResponse { - return Control::runCheckedFrom($req_params, [ - 'start' => [Validators::isUnsignedInteger()], - 'n' => [Validators::isUnsignedInteger()] + 'start' => [DefaultValidators::isUnsignedInteger()], + 'n' => [DefaultValidators::isUnsignedInteger()] ], function (HttpRequest $req) { - $team = $this->teamModel->listAll(intval($req['start']), intval($req['n'])); - $response = array_map(fn(Team $t) => $this->accountExposedFields($t), $team); - return new JsonHttpResponse($response); + $teams = $this->teamModel->listAll(intval($req['start']), intval($req['n'])); + return new JsonHttpResponse([ + "totalCount" => $this->teamModel->countTeam(), + "teams" => $teams + ]); }, true); } - private function accountExposedFields(Team $team): array { - $info = $team->getInfo(); - return [ - 'id' => $info->getId(), - 'name' => $info->getName(), - 'picture' => $info->getPicture(), - 'maincolor' => $info->getMainColor(), - 'secondcolor' => $info->getSecondColor(), - ]; + public function addTeam(): HttpResponse { + return Control::runChecked([ + 'name' => [DefaultValidators::name()], + 'picture' => [DefaultValidators::isURL()], + 'mainColor' => [DefaultValidators::hexColor()], + 'secondaryColor' => [DefaultValidators::hexColor()] + + ], function (HttpRequest $req){ + $this->teamModel->createTeam($req['name'],$req['picture'],$req['mainColor'],$req['secondaryColor']); + return HttpResponse::fromCode(HttpCodes::OK); + }, true); } + } \ No newline at end of file diff --git a/src/Core/Gateway/TeamGateway.php b/src/Core/Gateway/TeamGateway.php index be55a12..68d5f58 100644 --- a/src/Core/Gateway/TeamGateway.php +++ b/src/Core/Gateway/TeamGateway.php @@ -48,6 +48,7 @@ class TeamGateway { "id" => [$id, PDO::PARAM_INT], ] ); + return array_map(fn($row) => new TeamInfo($row['id'], $row['name'], $row['picture'], $row['main_color'], $row['second_color']), $result); } @@ -57,11 +58,11 @@ class TeamGateway { */ public function getTeamById(int $id): ?TeamInfo { $row = $this->con->fetch( - "SELECT * FROM team WHERE id = :id", - [ + "SELECT * FROM team WHERE id = :id", + [ ":id" => [$id, PDO::PARAM_INT], ] - )[0] ?? null; + )[0] ?? null; if ($row == null) { return null; } @@ -74,11 +75,11 @@ class TeamGateway { */ public function getTeamIdByName(string $name): ?int { return $this->con->fetch( - "SELECT id FROM team WHERE name = :name", - [ + "SELECT id FROM team WHERE name = :name", + [ ":name" => [$name, PDO::PARAM_INT], ] - )[0]['id'] ?? null; + )[0]['id'] ?? null; } /** @@ -139,20 +140,30 @@ class TeamGateway { ); } - - public function listAll(int $start, int $n): ?TeamInfo{ - $row = $this->con->fetch( - "SELECT * FROM Team WHERE id BETWEEN :start AND :n", + /** + * @param int $start + * @param int $n + * @return TeamInfo[] + */ + public function listAll(int $start, int $n): array { + $rows = $this->con->fetch( + "SELECT * FROM Team WHERE id BETWEEN :start AND :n LIMIT :limit", [ ":start" => [$start, PDO::PARAM_INT], ":n" => [$n, PDO::PARAM_INT], + ":limit" => [$n - $start + 1, PDO::PARAM_INT], //nombre de lignes à récupérer ] ); - if ($row == null) { - return null; - } + return array_map(fn($row) => new TeamInfo($row['id'], $row['name'], $row['picture'], $row['main_color'], $row['second_color']), $rows); + } - return new TeamInfo($row['id'], $row['name'], $row['picture'], $row['main_color'], $row['second_color']); + public function countTeam(): int { + $result = $this->con->fetch( + "SELECT count(*) FROM Team", []); + if (empty($result) || !isset($result[0]['count'])) { + return 0; + } + return $result[0]['count']; } diff --git a/src/Core/Model/TeamModel.php b/src/Core/Model/TeamModel.php index a7cd9d2..58cd336 100644 --- a/src/Core/Model/TeamModel.php +++ b/src/Core/Model/TeamModel.php @@ -140,8 +140,17 @@ class TeamModel { return $this->teams->getAll($user); } + /** + * @param int $start + * @param int $n + * @return TeamInfo[] + */ public function listAll(int $start, int $n) { return $this->teams->listAll($start, $n); } + public function countTeam():int{ + return $this->teams->countTeam(); + } + }