From ec69adab4483fe021e0fd3a4abd4b105870ad2d1 Mon Sep 17 00:00:00 2001 From: Override-6 Date: Fri, 8 Dec 2023 17:51:25 +0100 Subject: [PATCH 1/7] add basic api routes to get info on server, users and tactics --- public/api/index.php | 3 ++- sql/database.php | 17 +++++++++++++++++ sql/setup-tables.sql | 2 +- src/Api/API.php | 1 + src/Api/Controller/APIAccountsController.php | 6 ++++++ src/Api/Controller/APIAuthController.php | 2 +- src/Api/Controller/APIServerController.php | 2 ++ src/Api/Controller/APITacticController.php | 20 +++++++++++++------- src/App/App.php | 2 +- src/Core/Control.php | 2 ++ src/Core/Data/User.php | 2 +- src/Core/Gateway/AccountGateway.php | 3 ++- src/Core/Gateway/MemberGateway.php | 1 + src/Core/Model/AuthModel.php | 16 +++++++++++----- 14 files changed, 61 insertions(+), 18 deletions(-) diff --git a/public/api/index.php b/public/api/index.php index 226e8f1..e57872c 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -33,6 +33,7 @@ function getAccountController(): APIAccountsController { $con = new Connection(get_database()); $gw = new AccountGateway($con); return new APIAccountsController(new AuthModel($gw), $gw); + } function getServerController(): APIServerController { @@ -48,7 +49,6 @@ function getRoutes(): AltoRouter { $router->map("POST", "/auth", Action::noAuth(fn() => getAuthController()->authorize())); $router->map("POST", "/tactic/[i:id]/edit/name", Action::auth(fn(int $id, Account $acc) => getTacticController()->updateName($id, $acc))); $router->map("POST", "/tactic/[i:id]/save", Action::auth(fn(int $id, Account $acc) => getTacticController()->saveContent($id, $acc))); - $router->map("GET", "/admin/list-users", Action::noAuth(fn() => getAccountController()->listUsers($_GET))); $router->map("GET", "/admin/user/[i:id]", Action::noAuth(fn(int $id) => getAccountController()->getUser($id))); $router->map("GET", "/admin/user/[i:id]/space", Action::noAuth(fn(int $id) => getTacticController()->getUserTactics($id))); @@ -57,6 +57,7 @@ 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())); + return $router; } diff --git a/sql/database.php b/sql/database.php index 69b53e7..849d613 100644 --- a/sql/database.php +++ b/sql/database.php @@ -1,5 +1,9 @@ insertAccount($name, $email, AuthModel::generateToken(), password_hash("123456", PASSWORD_DEFAULT)); + $accounts->setIsAdmin($id, true); + } +} \ No newline at end of file diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql index 77f2b3d..b904370 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/API.php b/src/Api/API.php index cc61c8d..6fd7b71 100644 --- a/src/Api/API.php +++ b/src/Api/API.php @@ -55,6 +55,7 @@ class API { } if ($action->getAuthType() == Action::AUTH_ADMIN && !$account->getUser()->isAdmin()) { + return new JsonHttpResponse([ValidationFail::unauthorized()], HttpCodes::UNAUTHORIZED); } } diff --git a/src/Api/Controller/APIAccountsController.php b/src/Api/Controller/APIAccountsController.php index 32fd956..7b70f7d 100644 --- a/src/Api/Controller/APIAccountsController.php +++ b/src/Api/Controller/APIAccountsController.php @@ -2,6 +2,7 @@ namespace IQBall\Api\Controller; + use IQBall\Api\APIControl; use IQBall\App\Control; use IQBall\Core\Data\Account; @@ -25,6 +26,7 @@ class APIAccountsController { public function __construct(AuthModel $model, AccountGateway $accounts) { $this->accounts = $accounts; $this->authModel = $model; + } @@ -47,6 +49,7 @@ class APIAccountsController { }); } + /** * @param int $userId * @return HttpResponse given user information. @@ -106,3 +109,6 @@ class APIAccountsController { }); } } + +} + diff --git a/src/Api/Controller/APIAuthController.php b/src/Api/Controller/APIAuthController.php index c715803..c8393d3 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 1c82d3e..e61e6b9 100644 --- a/src/Api/Controller/APIServerController.php +++ b/src/Api/Controller/APIServerController.php @@ -6,6 +6,7 @@ use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\JsonHttpResponse; class APIServerController { + private string $basePath; private \PDO $pdo; @@ -43,3 +44,4 @@ class APIServerController { } } + diff --git a/src/Api/Controller/APITacticController.php b/src/Api/Controller/APITacticController.php index 9f71212..fed5abf 100644 --- a/src/Api/Controller/APITacticController.php +++ b/src/Api/Controller/APITacticController.php @@ -15,13 +15,15 @@ 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; } @@ -31,7 +33,8 @@ 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) { @@ -44,14 +47,15 @@ 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) { @@ -60,7 +64,7 @@ class APITacticController { return new JsonHttpResponse([$fail], HttpCodes::BAD_REQUEST); } return HttpResponse::fromCode(HttpCodes::OK); - }); + }, true); } @@ -68,7 +72,8 @@ 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) => [ @@ -76,6 +81,7 @@ class APITacticController { 'name' => $t->getName(), 'court' => $t->getCourtType(), 'creation_date' => $t->getCreationDate(), + ], $tactics); return new JsonHttpResponse($response); diff --git a/src/App/App.php b/src/App/App.php index 5f208bc..5140aa0 100644 --- a/src/App/App.php +++ b/src/App/App.php @@ -12,7 +12,6 @@ use Twig\Environment; use Twig\Error\LoaderError; use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; -use Twig\Loader\FilesystemLoader; class App { /** @@ -90,6 +89,7 @@ class App { if ($action->getAuthType() == Action::AUTH_ADMIN && !$account->getUser()->isAdmin()) { return new JsonHttpResponse([ValidationFail::unauthorized()], HttpCodes::UNAUTHORIZED); } + } return $action->run($params, $session); diff --git a/src/Core/Control.php b/src/Core/Control.php index 106052d..7456202 100644 --- a/src/Core/Control.php +++ b/src/Core/Control.php @@ -15,6 +15,7 @@ class Control { * @param array $schema an array of `fieldName => DefaultValidators` which represents the request object schema * @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema. * The callback must accept an HttpRequest, and return an HttpResponse object. +<<<<<<< HEAD:src/Core/Control.php * @param ControlSchemaErrorResponseFactory $errorFactory an error factory to use if the request does not validate the required schema * @return HttpResponse */ @@ -35,6 +36,7 @@ class Control { * @param array $schema an array of `fieldName => DefaultValidators` which represents the request object schema * @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema. * The callback must accept an HttpRequest, and return an HttpResponse object. +<<<<<<< HEAD:src/Core/Control.php * @param ControlSchemaErrorResponseFactory $errorFactory an error factory to use if the request does not validate the required schema * @return HttpResponse */ diff --git a/src/Core/Data/User.php b/src/Core/Data/User.php index 02a44c0..8471929 100644 --- a/src/Core/Data/User.php +++ b/src/Core/Data/User.php @@ -24,7 +24,7 @@ class User implements \JsonSerializable { private string $profilePicture; /** - * @var bool isAdmin + * @var bool true if the user is an administrator */ private bool $isAdmin; diff --git a/src/Core/Gateway/AccountGateway.php b/src/Core/Gateway/AccountGateway.php index 6752b01..1a0c689 100644 --- a/src/Core/Gateway/AccountGateway.php +++ b/src/Core/Gateway/AccountGateway.php @@ -47,7 +47,6 @@ class AccountGateway { return !empty($result); } - /** * promote or demote a user to server administrator * @param int $id @@ -60,6 +59,7 @@ class AccountGateway { } else { $stmnt = $this->con->prepare("DELETE FROM Admins WHERE id = :id"); } + $stmnt->bindValue(':id', $id); $stmnt->execute(); @@ -155,6 +155,7 @@ class AccountGateway { ] ); return array_map(fn(array $acc) => new Account($acc["token"], new User($acc["email"], $acc["username"], $acc["id"], $acc["profile_picture"], $this->isAdmin($acc["id"]))), $res); + } /** diff --git a/src/Core/Gateway/MemberGateway.php b/src/Core/Gateway/MemberGateway.php index 98d2d41..f79ff60 100644 --- a/src/Core/Gateway/MemberGateway.php +++ b/src/Core/Gateway/MemberGateway.php @@ -47,6 +47,7 @@ class MemberGateway { ] ); return array_map(fn($row) => new Member(new User($row['email'], $row['username'], $row['id'], $row['profile_picture'], $row['is_admin']), $teamId, $row['role']), $rows); + } /** diff --git a/src/Core/Model/AuthModel.php b/src/Core/Model/AuthModel.php index e1fc1bb..034e210 100644 --- a/src/Core/Model/AuthModel.php +++ b/src/Core/Model/AuthModel.php @@ -28,11 +28,16 @@ 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 $email - ): ?Account { + + 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."); + } if ($this->gateway->exists($email)) { return null; } @@ -53,6 +58,7 @@ class AuthModel { } catch (Exception $e) { throw new \RuntimeException($e); } + } /** -- 2.36.3 From cfa9b40bc1fceec140af77a128ab772f0d9ff11a Mon Sep 17 00:00:00 2001 From: Override-6 Date: Fri, 8 Dec 2023 22:54:34 +0100 Subject: [PATCH 2/7] WIP --- src/Api/API.php | 1 + src/Api/Controller/APIAccountsController.php | 1 - src/Core/Control.php | 3 ++- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Api/API.php b/src/Api/API.php index 6fd7b71..d751c75 100644 --- a/src/Api/API.php +++ b/src/Api/API.php @@ -17,6 +17,7 @@ class API { header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Headers: *'); + foreach ($response->getHeaders() as $header => $value) { header("$header: $value"); } diff --git a/src/Api/Controller/APIAccountsController.php b/src/Api/Controller/APIAccountsController.php index 7b70f7d..cfaa3e0 100644 --- a/src/Api/Controller/APIAccountsController.php +++ b/src/Api/Controller/APIAccountsController.php @@ -49,7 +49,6 @@ class APIAccountsController { }); } - /** * @param int $userId * @return HttpResponse given user information. diff --git a/src/Core/Control.php b/src/Core/Control.php index 7456202..a8ac6c6 100644 --- a/src/Core/Control.php +++ b/src/Core/Control.php @@ -25,6 +25,7 @@ class Control { if (!$payload_obj instanceof \stdClass) { $fail = new ValidationFail("bad-payload", "request body is not a valid json object"); return $errorFactory->apply([$fail]); + } $payload = get_object_vars($payload_obj); return self::runCheckedFrom($payload, $schema, $run, $errorFactory); @@ -36,7 +37,6 @@ class Control { * @param array $schema an array of `fieldName => DefaultValidators` which represents the request object schema * @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema. * The callback must accept an HttpRequest, and return an HttpResponse object. -<<<<<<< HEAD:src/Core/Control.php * @param ControlSchemaErrorResponseFactory $errorFactory an error factory to use if the request does not validate the required schema * @return HttpResponse */ @@ -46,6 +46,7 @@ class Control { if (!empty($fails)) { return $errorFactory->apply($fails); + } return call_user_func_array($run, [$request]); -- 2.36.3 From 9117d4afab163ebac5f3c038cb04db0428e18374 Mon Sep 17 00:00:00 2001 From: samuel Date: Thu, 21 Dec 2023 09:27:15 +0100 Subject: [PATCH 3/7] api teams --- public/api/index.php | 8 ++++ sql/database.php | 6 +++ src/Api/Controller/APIAccountsController.php | 1 + src/Api/Controller/APITeamController.php | 48 ++++++++++++++++++++ src/Core/Gateway/TeamGateway.php | 18 ++++++++ src/Core/Model/TeamModel.php | 15 ++++-- 6 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 src/Api/Controller/APITeamController.php 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); + } + } -- 2.36.3 From 088b1a5281a27b2f248125dfcfb9bf1a99ca72bc Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 8 Jan 2024 10:49:13 +0100 Subject: [PATCH 4/7] 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(); + } + } -- 2.36.3 From b0d0409a0653cf20dd3a533aacd14760f3cdb049 Mon Sep 17 00:00:00 2001 From: samuel Date: Wed, 17 Jan 2024 17:36:37 +0100 Subject: [PATCH 5/7] 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); + } + } -- 2.36.3 From 4cf9089e803b18ed0fdcb972673ed45a6074a102 Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 25 Jan 2024 22:22:42 +0100 Subject: [PATCH 6/7] 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); } -- 2.36.3 From cadab3c1eb54bf5169f643182c683df87f525450 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 31 Jan 2024 19:19:54 +0100 Subject: [PATCH 7/7] requests for changes fixed and format + verify --- sql/database.php | 1 - src/Api/API.php | 2 -- src/Api/Controller/APITeamController.php | 7 +++---- src/Core/Control.php | 1 - 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/sql/database.php b/sql/database.php index 6684235..6d20c56 100644 --- a/sql/database.php +++ b/sql/database.php @@ -30,4 +30,3 @@ function get_database(): PDO { return $pdo; } - diff --git a/src/Api/API.php b/src/Api/API.php index 2d5e8b3..3437536 100644 --- a/src/Api/API.php +++ b/src/Api/API.php @@ -26,8 +26,6 @@ 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"); } } diff --git a/src/Api/Controller/APITeamController.php b/src/Api/Controller/APITeamController.php index 1d4fa87..270468d 100644 --- a/src/Api/Controller/APITeamController.php +++ b/src/Api/Controller/APITeamController.php @@ -15,7 +15,6 @@ use IQBall\Core\Model\TeamModel; use IQBall\Core\Validation\DefaultValidators; class APITeamController { - private TeamModel $teamModel; /** @@ -64,14 +63,14 @@ class APITeamController { }); } - public function updateTeam(int $id):HttpResponse{ + 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']); + ], function (HttpRequest $req) { + $this->teamModel->editTeam($req['id'], $req['name'], $req['picture'], $req['mainColor'], $req['secondaryColor']); return HttpResponse::fromCode(HttpCodes::OK); }); } diff --git a/src/Core/Control.php b/src/Core/Control.php index a8ac6c6..51d6622 100644 --- a/src/Core/Control.php +++ b/src/Core/Control.php @@ -15,7 +15,6 @@ class Control { * @param array $schema an array of `fieldName => DefaultValidators` which represents the request object schema * @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema. * The callback must accept an HttpRequest, and return an HttpResponse object. -<<<<<<< HEAD:src/Core/Control.php * @param ControlSchemaErrorResponseFactory $errorFactory an error factory to use if the request does not validate the required schema * @return HttpResponse */ -- 2.36.3