diff --git a/public/api/index.php b/public/api/index.php index e57872c..399309b 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -41,6 +41,13 @@ function getServerController(): APIServerController { return new APIServerController($basePath, get_database()); } +function getAPITeamController(): \IQBall\Api\Controller\APITeamController{ + $con = new Connection(get_database()); + return new \IQBall\Api\Controller\APITeamController(new \IQBall\Core\Model\TeamModel(new \IQBall\Core\Gateway\TeamGateway($con),new \IQBall\Core\Gateway\MemberGateway($con),new AccountGateway($con))); +} + + + function getRoutes(): AltoRouter { $router = new AltoRouter(); global $basePath; @@ -56,6 +63,7 @@ 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))); return $router; diff --git a/sql/database.php b/sql/database.php index 849d613..f11a8fe 100644 --- a/sql/database.php +++ b/sql/database.php @@ -34,12 +34,18 @@ function get_database(): PDO { 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"]; + $defaultTeams = ["Lakers", "Celtics", "Bulls"]; foreach ($defaultAccounts as $name) { $email = "$name@mail.com"; $id = $accounts->insertAccount($name, $email, AuthModel::generateToken(), password_hash("123456", PASSWORD_DEFAULT)); $accounts->setIsAdmin($id, true); } + + foreach ($defaultTeams as $name){ + $id = $teams->insert($name,"https://lebasketographe.fr/wp-content/uploads/2019/11/nom-equipes-nba.jpg","ff5733","85f251"); + } } \ No newline at end of file diff --git a/src/Api/Controller/APIAccountsController.php b/src/Api/Controller/APIAccountsController.php index cfaa3e0..1d8c60a 100644 --- a/src/Api/Controller/APIAccountsController.php +++ b/src/Api/Controller/APIAccountsController.php @@ -36,6 +36,7 @@ class APIAccountsController { */ public function listUsers(array $request): HttpResponse { return APIControl::runCheckedFrom($request, [ + 'start' => [DefaultValidators::isUnsignedInteger()], 'n' => [DefaultValidators::isIntInRange(0, 250)], 'search' => [DefaultValidators::lenBetween(0, 256)], diff --git a/src/Api/Controller/APITeamController.php b/src/Api/Controller/APITeamController.php new file mode 100644 index 0000000..8c68c55 --- /dev/null +++ b/src/Api/Controller/APITeamController.php @@ -0,0 +1,48 @@ +teamModel = $teamModel; + } + + public function listTeam(array $req_params): HttpResponse { + + return Control::runCheckedFrom($req_params, [ + 'start' => [Validators::isUnsignedInteger()], + 'n' => [Validators::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); + }, 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(), + ]; + } + +} \ No newline at end of file diff --git a/src/Core/Gateway/TeamGateway.php b/src/Core/Gateway/TeamGateway.php index a817687..be55a12 100644 --- a/src/Core/Gateway/TeamGateway.php +++ b/src/Core/Gateway/TeamGateway.php @@ -3,6 +3,8 @@ namespace IQBall\Core\Gateway; use IQBall\Core\Connection; +use IQBall\Core\Data\Color; +use IQBall\Core\Data\Team; use IQBall\Core\Data\TeamInfo; use PDO; @@ -138,4 +140,20 @@ class TeamGateway { } + public function listAll(int $start, int $n): ?TeamInfo{ + $row = $this->con->fetch( + "SELECT * FROM Team WHERE id BETWEEN :start AND :n", + [ + ":start" => [$start, PDO::PARAM_INT], + ":n" => [$n, PDO::PARAM_INT], + ] + ); + if ($row == null) { + return null; + } + + return new TeamInfo($row['id'], $row['name'], $row['picture'], $row['main_color'], $row['second_color']); + } + + } diff --git a/src/Core/Model/TeamModel.php b/src/Core/Model/TeamModel.php index 2bfe36e..a7cd9d2 100644 --- a/src/Core/Model/TeamModel.php +++ b/src/Core/Model/TeamModel.php @@ -45,10 +45,10 @@ class TeamModel { */ public function addMember(string $mail, int $teamId, string $role): int { $user = $this->users->getAccountFromMail($mail); - if($user == null) { + if ($user == null) { return -1; } - if(!$this->members->isMemberOfTeam($teamId, $user->getUser()->getId())) { + if (!$this->members->isMemberOfTeam($teamId, $user->getUser()->getId())) { $this->members->insert($teamId, $user->getUser()->getId(), $role); return 1; } @@ -70,7 +70,7 @@ class TeamModel { * @return Team|null */ public function getTeam(int $idTeam, int $idCurrentUser): ?Team { - if(!$this->members->isMemberOfTeam($idTeam, $idCurrentUser)) { + if (!$this->members->isMemberOfTeam($idTeam, $idCurrentUser)) { return null; } $teamInfo = $this->teams->getTeamById($idTeam); @@ -86,7 +86,7 @@ class TeamModel { */ public function deleteMember(int $idMember, int $teamId): int { $this->members->remove($teamId, $idMember); - if(empty($this->members->getMembersOfTeam($teamId))) { + if (empty($this->members->getMembersOfTeam($teamId))) { $this->teams->deleteTeam($teamId); return -1; } @@ -100,7 +100,7 @@ class TeamModel { * @return int */ public function deleteTeam(string $email, int $idTeam): int { - if($this->members->isCoach($email, $idTeam)) { + if ($this->members->isCoach($email, $idTeam)) { $this->teams->deleteTeam($idTeam); return 0; } @@ -139,4 +139,9 @@ class TeamModel { public function getAll(int $user): array { return $this->teams->getAll($user); } + + public function listAll(int $start, int $n) { + return $this->teams->listAll($start, $n); + } + }