From 4cf9089e803b18ed0fdcb972673ed45a6074a102 Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 25 Jan 2024 22:22:42 +0100 Subject: [PATCH] rebase and fix phpstan --- [, | 0 profiles/dev-config-profile.php | 7 +++ public/api/index.php | 5 ++- sql/database.php | 18 -------- sql/setup-tables.sql | 2 +- src/Api/Controller/APIAccountsController.php | 5 --- src/Api/Controller/APIAuthController.php | 2 +- src/Api/Controller/APIServerController.php | 2 - src/Api/Controller/APITacticController.php | 19 +++----- src/Api/Controller/APITeamController.php | 47 +++++++++++++------- src/Core/Gateway/TeamGateway.php | 20 ++++++--- src/Core/Model/AuthModel.php | 16 +++---- src/Core/Model/TeamModel.php | 8 +++- 13 files changed, 73 insertions(+), 78 deletions(-) delete mode 100644 [, diff --git a/[, b/[, deleted file mode 100644 index e69de29..0000000 diff --git a/profiles/dev-config-profile.php b/profiles/dev-config-profile.php index e39f2f0..62b089e 100644 --- a/profiles/dev-config-profile.php +++ b/profiles/dev-config-profile.php @@ -21,12 +21,19 @@ function _asset(string $assetURI): string { 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), "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png"); $accounts->setIsAdmin($id, true); } + + foreach ($defaultTeams as $name) { + $id = $teams->insert($name, "https://lebasketographe.fr/wp-content/uploads/2019/11/nom-equipes-nba.jpg", "#1a2b3c", "#FF00AA"); + } } diff --git a/public/api/index.php b/public/api/index.php index 137edec..ab8cb50 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -41,9 +41,9 @@ function getServerController(): APIServerController { return new APIServerController($basePath, get_database()); } -function getAPITeamController(): \IQBall\Api\Controller\APITeamController{ +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))); + 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))); } @@ -66,6 +66,7 @@ function getRoutes(): AltoRouter { $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())); + $router->map("POST", "/admin/team/[i:id]/update", Action::noAuth(fn(int $id) => getAPITeamController()->updateTeam($id))); diff --git a/sql/database.php b/sql/database.php index b18c750..6684235 100644 --- a/sql/database.php +++ b/sql/database.php @@ -31,21 +31,3 @@ function get_database(): PDO { return $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", "samuel"]; - $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","#1a2b3c","#FF00AA"); - } -} \ No newline at end of file diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index b904370..77f2b3d 100644 --- a/sql/setup-tables.sql +++ b/sql/setup-tables.sql @@ -17,7 +17,7 @@ CREATE TABLE Account username varchar NOT NULL, token varchar UNIQUE NOT NULL, hash varchar NOT NULL, - profile_picture varchar NOT NULL, + profile_picture varchar NOT NULL ); CREATE TABLE Tactic diff --git a/src/Api/Controller/APIAccountsController.php b/src/Api/Controller/APIAccountsController.php index 1d8c60a..6ebf0fc 100644 --- a/src/Api/Controller/APIAccountsController.php +++ b/src/Api/Controller/APIAccountsController.php @@ -2,9 +2,7 @@ namespace IQBall\Api\Controller; - use IQBall\Api\APIControl; -use IQBall\App\Control; use IQBall\Core\Data\Account; use IQBall\Core\Gateway\AccountGateway; use IQBall\Core\Http\HttpCodes; @@ -109,6 +107,3 @@ class APIAccountsController { }); } } - -} - diff --git a/src/Api/Controller/APIAuthController.php b/src/Api/Controller/APIAuthController.php index c8393d3..c715803 100644 --- a/src/Api/Controller/APIAuthController.php +++ b/src/Api/Controller/APIAuthController.php @@ -39,6 +39,6 @@ class APIAuthController { } return new JsonHttpResponse(["authorization" => $account->getToken()]); - }, true); + }); } } diff --git a/src/Api/Controller/APIServerController.php b/src/Api/Controller/APIServerController.php index e61e6b9..1c82d3e 100644 --- a/src/Api/Controller/APIServerController.php +++ b/src/Api/Controller/APIServerController.php @@ -6,7 +6,6 @@ use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\JsonHttpResponse; class APIServerController { - private string $basePath; private \PDO $pdo; @@ -44,4 +43,3 @@ class APIServerController { } } - diff --git a/src/Api/Controller/APITacticController.php b/src/Api/Controller/APITacticController.php index fed5abf..8b161d1 100644 --- a/src/Api/Controller/APITacticController.php +++ b/src/Api/Controller/APITacticController.php @@ -15,15 +15,13 @@ use IQBall\Core\Validation\DefaultValidators; /** * API endpoint related to tactics */ -class APITacticController -{ +class APITacticController { private TacticModel $model; /** * @param TacticModel $model */ - public function __construct(TacticModel $model) - { + public function __construct(TacticModel $model) { $this->model = $model; } @@ -33,8 +31,7 @@ class APITacticController * @param Account $account * @return HttpResponse */ - public function updateName(int $tactic_id, Account $account): HttpResponse - { + public function updateName(int $tactic_id, Account $account): HttpResponse { return APIControl::runChecked([ "name" => [DefaultValidators::lenBetween(1, 50), DefaultValidators::nameWithSpaces()], ], function (HttpRequest $request) use ($tactic_id, $account) { @@ -47,15 +44,14 @@ class APITacticController } return HttpResponse::fromCode(HttpCodes::OK); - }, true); + }); } /** * @param int $id * @return HttpResponse */ - public function saveContent(int $id, Account $account): HttpResponse - { + public function saveContent(int $id, Account $account): HttpResponse { return APIControl::runChecked([ "content" => [], ], function (HttpRequest $req) use ($id) { @@ -64,7 +60,7 @@ class APITacticController return new JsonHttpResponse([$fail], HttpCodes::BAD_REQUEST); } return HttpResponse::fromCode(HttpCodes::OK); - }, true); + }); } @@ -72,8 +68,7 @@ class APITacticController * @param int $userId * @return HttpResponse given user information. */ - public function getUserTactics(int $userId): HttpResponse - { + public function getUserTactics(int $userId): HttpResponse { $tactics = $this->model->listAllOf($userId); $response = array_map(fn(TacticInfo $t) => [ diff --git a/src/Api/Controller/APITeamController.php b/src/Api/Controller/APITeamController.php index 26b2b76..1d4fa87 100644 --- a/src/Api/Controller/APITeamController.php +++ b/src/Api/Controller/APITeamController.php @@ -2,19 +2,20 @@ namespace IQBall\Api\Controller; -use IQBall\App\Control; +use IQBall\Api\APIControl; use IQBall\Core\Data\Account; use IQBall\Core\Data\Team; use IQBall\Core\Data\TeamInfo; +use IQBall\Core\Gateway\TeamGateway; 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 { + private TeamModel $teamModel; /** @@ -29,39 +30,51 @@ class APITeamController { * @return HttpResponse */ public function listTeams(array $req_params): HttpResponse { - return Control::runCheckedFrom($req_params, [ + return APIControl::runCheckedFrom($req_params, [ 'start' => [DefaultValidators::isUnsignedInteger()], - 'n' => [DefaultValidators::isUnsignedInteger()] + 'n' => [DefaultValidators::isUnsignedInteger()], ], function (HttpRequest $req) { $teams = $this->teamModel->listAll(intval($req['start']), intval($req['n'])); return new JsonHttpResponse([ "totalCount" => $this->teamModel->countTeam(), - "teams" => $teams + "teams" => $teams, ]); - }, true); + }); } public function addTeam(): HttpResponse { - return Control::runChecked([ + return APIControl::runChecked([ 'name' => [DefaultValidators::name()], 'picture' => [DefaultValidators::isURL()], 'mainColor' => [DefaultValidators::hexColor()], - 'secondaryColor' => [DefaultValidators::hexColor()] + 'secondaryColor' => [DefaultValidators::hexColor()], - ], function (HttpRequest $req){ - $this->teamModel->createTeam($req['name'],$req['picture'],$req['mainColor'],$req['secondaryColor']); + ], function (HttpRequest $req) { + $this->teamModel->createTeam($req['name'], $req['picture'], $req['mainColor'], $req['secondaryColor']); return HttpResponse::fromCode(HttpCodes::OK); - }, true); + }); } - public function deleteTeamSelected(): HttpResponse{ - return Control::runChecked([ - 'teams' => [] - ], function (HttpRequest $req){ + public function deleteTeamSelected(): HttpResponse { + return APIControl::runChecked([ + 'teams' => [], + ], function (HttpRequest $req) { $this->teamModel->deleteTeamSelected($req['teams']); return HttpResponse::fromCode(HttpCodes::OK); - },true); + }); + } + + public function updateTeam(int $id):HttpResponse{ + return APIControl::runChecked([ + 'name' => [DefaultValidators::name()], + 'picture' => [DefaultValidators::isURL()], + 'mainColor' => [DefaultValidators::hexColor()], + 'secondaryColor' => [DefaultValidators::hexColor()], + ], function (HttpRequest $req){ + $this->teamModel->editTeam($req['id'],$req['name'], $req['picture'], $req['mainColor'], $req['secondaryColor']); + return HttpResponse::fromCode(HttpCodes::OK); + }); } -} \ No newline at end of file +} diff --git a/src/Core/Gateway/TeamGateway.php b/src/Core/Gateway/TeamGateway.php index 0c96ba6..4309a49 100644 --- a/src/Core/Gateway/TeamGateway.php +++ b/src/Core/Gateway/TeamGateway.php @@ -58,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; } @@ -75,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; } /** @@ -158,13 +158,19 @@ class TeamGateway { public function countTeam(): int { $result = $this->con->fetch( - "SELECT count(*) as count FROM Team", []); + "SELECT count(*) as count FROM Team", + [] + ); if (empty($result) || !isset($result[0]['count'])) { return 0; } return $result[0]['count']; } + /** + * @param array $selectedTeams + * @return void + */ public function deleteTeamSelected(array $selectedTeams): void { foreach ($selectedTeams as $team) { $this->con->exec( diff --git a/src/Core/Model/AuthModel.php b/src/Core/Model/AuthModel.php index 034e210..e1fc1bb 100644 --- a/src/Core/Model/AuthModel.php +++ b/src/Core/Model/AuthModel.php @@ -28,16 +28,11 @@ class AuthModel { * @param string $email * @return Account|null the registered account or null if the account already exists for the given email address */ - - public function register(string $username, - string $password, - string $confirmPassword, - string $email, - array &$failures): ?Account { - - if ($password != $confirmPassword) { - $failures[] = new FieldValidationFail("confirmpassword", "Le mot de passe et la confirmation ne sont pas les mêmes."); - } + public function register( + string $username, + string $password, + string $email + ): ?Account { if ($this->gateway->exists($email)) { return null; } @@ -58,7 +53,6 @@ class AuthModel { } catch (Exception $e) { throw new \RuntimeException($e); } - } /** diff --git a/src/Core/Model/TeamModel.php b/src/Core/Model/TeamModel.php index a37ba17..b6b7bdd 100644 --- a/src/Core/Model/TeamModel.php +++ b/src/Core/Model/TeamModel.php @@ -149,11 +149,15 @@ class TeamModel { return $this->teams->listAll($start, $n); } - public function countTeam():int{ + public function countTeam(): int { return $this->teams->countTeam(); } - public function deleteTeamSelected(array $selectedTeams){ + /** + * @param array $selectedTeams + * @return void + */ + public function deleteTeamSelected(array $selectedTeams) { $this->teams->deleteTeamSelected($selectedTeams); }