From b0d0409a0653cf20dd3a533aacd14760f3cdb049 Mon Sep 17 00:00:00 2001 From: samuel Date: Wed, 17 Jan 2024 17:36:37 +0100 Subject: [PATCH] WIP --- public/api/index.php | 3 ++- src/Api/Controller/APITeamController.php | 11 ++++++++++- src/Core/Gateway/TeamGateway.php | 16 +++++++++++++--- src/Core/Model/TeamModel.php | 4 ++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/public/api/index.php b/public/api/index.php index b7f966d..137edec 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -63,8 +63,9 @@ function getRoutes(): AltoRouter { $router->map("POST", "/admin/user/remove-all", Action::noAuth(fn() => getAccountController()->removeUsers())); $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("GET", "/admin/list-team", Action::noAuth(fn() => getAPITeamController()->listTeams($_GET))); $router->map("POST", "/admin/add-team", Action::noAuth(fn() => getAPITeamController()->addTeam())); + $router->map("POST", "/admin/delete-teams", Action::noAuth(fn() => getAPITeamController()->deleteTeamSelected())); diff --git a/src/Api/Controller/APITeamController.php b/src/Api/Controller/APITeamController.php index 2c1fa90..26b2b76 100644 --- a/src/Api/Controller/APITeamController.php +++ b/src/Api/Controller/APITeamController.php @@ -28,7 +28,7 @@ class APITeamController { * @param array $req_params * @return HttpResponse */ - public function listTeam(array $req_params): HttpResponse { + public function listTeams(array $req_params): HttpResponse { return Control::runCheckedFrom($req_params, [ 'start' => [DefaultValidators::isUnsignedInteger()], 'n' => [DefaultValidators::isUnsignedInteger()] @@ -54,5 +54,14 @@ class APITeamController { }, true); } + public function deleteTeamSelected(): HttpResponse{ + return Control::runChecked([ + 'teams' => [] + ], function (HttpRequest $req){ + $this->teamModel->deleteTeamSelected($req['teams']); + 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 68d5f58..0c96ba6 100644 --- a/src/Core/Gateway/TeamGateway.php +++ b/src/Core/Gateway/TeamGateway.php @@ -147,11 +147,10 @@ class TeamGateway { */ public function listAll(int $start, int $n): array { $rows = $this->con->fetch( - "SELECT * FROM Team WHERE id BETWEEN :start AND :n LIMIT :limit", + "SELECT * FROM Team LIMIT :start, :n", [ ":start" => [$start, PDO::PARAM_INT], ":n" => [$n, PDO::PARAM_INT], - ":limit" => [$n - $start + 1, PDO::PARAM_INT], //nombre de lignes à récupérer ] ); return array_map(fn($row) => new TeamInfo($row['id'], $row['name'], $row['picture'], $row['main_color'], $row['second_color']), $rows); @@ -159,12 +158,23 @@ class TeamGateway { public function countTeam(): int { $result = $this->con->fetch( - "SELECT count(*) FROM Team", []); + "SELECT count(*) as count FROM Team", []); if (empty($result) || !isset($result[0]['count'])) { return 0; } return $result[0]['count']; } + public function deleteTeamSelected(array $selectedTeams): void { + foreach ($selectedTeams as $team) { + $this->con->exec( + "DELETE FROM TEAM WHERE id=:team", + [ + "team" => [$team, PDO::PARAM_INT], + ] + ); + } + } + } diff --git a/src/Core/Model/TeamModel.php b/src/Core/Model/TeamModel.php index 58cd336..a37ba17 100644 --- a/src/Core/Model/TeamModel.php +++ b/src/Core/Model/TeamModel.php @@ -153,4 +153,8 @@ class TeamModel { return $this->teams->countTeam(); } + public function deleteTeamSelected(array $selectedTeams){ + $this->teams->deleteTeamSelected($selectedTeams); + } + }