rebase and fix phpstan
continuous-integration/drone/push Build is passing Details

pull/97/head
sam 1 year ago
parent b0d0409a06
commit 4cf9089e80

@ -21,12 +21,19 @@ function _asset(string $assetURI): string {
function _init_database(PDO $pdo): void { function _init_database(PDO $pdo): void {
$accounts = new AccountGateway(new Connection($pdo)); $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"];
$defaultTeams = ["Lakers", "Celtics", "Bulls"];
foreach ($defaultAccounts as $name) { foreach ($defaultAccounts as $name) {
$email = "$name@mail.com"; $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"); $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); $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");
}
} }

@ -41,9 +41,9 @@ function getServerController(): APIServerController {
return new APIServerController($basePath, get_database()); return new APIServerController($basePath, get_database());
} }
function getAPITeamController(): \IQBall\Api\Controller\APITeamController{ function getAPITeamController(): \IQBall\Api\Controller\APITeamController {
$con = new Connection(get_database()); $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("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/add-team", Action::noAuth(fn() => getAPITeamController()->addTeam()));
$router->map("POST", "/admin/delete-teams", Action::noAuth(fn() => getAPITeamController()->deleteTeamSelected())); $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)));

@ -31,21 +31,3 @@ function get_database(): PDO {
return $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");
}
}

@ -17,7 +17,7 @@ CREATE TABLE Account
username varchar NOT NULL, username varchar NOT NULL,
token varchar UNIQUE NOT NULL, token varchar UNIQUE NOT NULL,
hash varchar NOT NULL, hash varchar NOT NULL,
profile_picture varchar NOT NULL, profile_picture varchar NOT NULL
); );
CREATE TABLE Tactic CREATE TABLE Tactic

@ -2,9 +2,7 @@
namespace IQBall\Api\Controller; namespace IQBall\Api\Controller;
use IQBall\Api\APIControl; use IQBall\Api\APIControl;
use IQBall\App\Control;
use IQBall\Core\Data\Account; use IQBall\Core\Data\Account;
use IQBall\Core\Gateway\AccountGateway; use IQBall\Core\Gateway\AccountGateway;
use IQBall\Core\Http\HttpCodes; use IQBall\Core\Http\HttpCodes;
@ -109,6 +107,3 @@ class APIAccountsController {
}); });
} }
} }
}

@ -39,6 +39,6 @@ class APIAuthController {
} }
return new JsonHttpResponse(["authorization" => $account->getToken()]); return new JsonHttpResponse(["authorization" => $account->getToken()]);
}, true); });
} }
} }

@ -6,7 +6,6 @@ use IQBall\Core\Http\HttpResponse;
use IQBall\Core\Http\JsonHttpResponse; use IQBall\Core\Http\JsonHttpResponse;
class APIServerController { class APIServerController {
private string $basePath; private string $basePath;
private \PDO $pdo; private \PDO $pdo;
@ -44,4 +43,3 @@ class APIServerController {
} }
} }

@ -15,15 +15,13 @@ use IQBall\Core\Validation\DefaultValidators;
/** /**
* API endpoint related to tactics * API endpoint related to tactics
*/ */
class APITacticController class APITacticController {
{
private TacticModel $model; private TacticModel $model;
/** /**
* @param TacticModel $model * @param TacticModel $model
*/ */
public function __construct(TacticModel $model) public function __construct(TacticModel $model) {
{
$this->model = $model; $this->model = $model;
} }
@ -33,8 +31,7 @@ class APITacticController
* @param Account $account * @param Account $account
* @return HttpResponse * @return HttpResponse
*/ */
public function updateName(int $tactic_id, Account $account): HttpResponse public function updateName(int $tactic_id, Account $account): HttpResponse {
{
return APIControl::runChecked([ return APIControl::runChecked([
"name" => [DefaultValidators::lenBetween(1, 50), DefaultValidators::nameWithSpaces()], "name" => [DefaultValidators::lenBetween(1, 50), DefaultValidators::nameWithSpaces()],
], function (HttpRequest $request) use ($tactic_id, $account) { ], function (HttpRequest $request) use ($tactic_id, $account) {
@ -47,15 +44,14 @@ class APITacticController
} }
return HttpResponse::fromCode(HttpCodes::OK); return HttpResponse::fromCode(HttpCodes::OK);
}, true); });
} }
/** /**
* @param int $id * @param int $id
* @return HttpResponse * @return HttpResponse
*/ */
public function saveContent(int $id, Account $account): HttpResponse public function saveContent(int $id, Account $account): HttpResponse {
{
return APIControl::runChecked([ return APIControl::runChecked([
"content" => [], "content" => [],
], function (HttpRequest $req) use ($id) { ], function (HttpRequest $req) use ($id) {
@ -64,7 +60,7 @@ class APITacticController
return new JsonHttpResponse([$fail], HttpCodes::BAD_REQUEST); return new JsonHttpResponse([$fail], HttpCodes::BAD_REQUEST);
} }
return HttpResponse::fromCode(HttpCodes::OK); return HttpResponse::fromCode(HttpCodes::OK);
}, true); });
} }
@ -72,8 +68,7 @@ class APITacticController
* @param int $userId * @param int $userId
* @return HttpResponse given user information. * @return HttpResponse given user information.
*/ */
public function getUserTactics(int $userId): HttpResponse public function getUserTactics(int $userId): HttpResponse {
{
$tactics = $this->model->listAllOf($userId); $tactics = $this->model->listAllOf($userId);
$response = array_map(fn(TacticInfo $t) => [ $response = array_map(fn(TacticInfo $t) => [

@ -2,19 +2,20 @@
namespace IQBall\Api\Controller; namespace IQBall\Api\Controller;
use IQBall\App\Control; use IQBall\Api\APIControl;
use IQBall\Core\Data\Account; use IQBall\Core\Data\Account;
use IQBall\Core\Data\Team; use IQBall\Core\Data\Team;
use IQBall\Core\Data\TeamInfo; use IQBall\Core\Data\TeamInfo;
use IQBall\Core\Gateway\TeamGateway;
use IQBall\Core\Http\HttpCodes; use IQBall\Core\Http\HttpCodes;
use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpRequest;
use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\HttpResponse;
use IQBall\Core\Http\JsonHttpResponse; use IQBall\Core\Http\JsonHttpResponse;
use IQBall\Core\Model\TeamModel; use IQBall\Core\Model\TeamModel;
use IQBall\Core\Validation\DefaultValidators; use IQBall\Core\Validation\DefaultValidators;
use IQBall\Core\Validation\Validators;
class APITeamController { class APITeamController {
private TeamModel $teamModel; private TeamModel $teamModel;
/** /**
@ -29,38 +30,50 @@ class APITeamController {
* @return HttpResponse * @return HttpResponse
*/ */
public function listTeams(array $req_params): HttpResponse { public function listTeams(array $req_params): HttpResponse {
return Control::runCheckedFrom($req_params, [ return APIControl::runCheckedFrom($req_params, [
'start' => [DefaultValidators::isUnsignedInteger()], 'start' => [DefaultValidators::isUnsignedInteger()],
'n' => [DefaultValidators::isUnsignedInteger()] 'n' => [DefaultValidators::isUnsignedInteger()],
], function (HttpRequest $req) { ], function (HttpRequest $req) {
$teams = $this->teamModel->listAll(intval($req['start']), intval($req['n'])); $teams = $this->teamModel->listAll(intval($req['start']), intval($req['n']));
return new JsonHttpResponse([ return new JsonHttpResponse([
"totalCount" => $this->teamModel->countTeam(), "totalCount" => $this->teamModel->countTeam(),
"teams" => $teams "teams" => $teams,
]); ]);
}, true); });
} }
public function addTeam(): HttpResponse { public function addTeam(): HttpResponse {
return Control::runChecked([ return APIControl::runChecked([
'name' => [DefaultValidators::name()], 'name' => [DefaultValidators::name()],
'picture' => [DefaultValidators::isURL()], 'picture' => [DefaultValidators::isURL()],
'mainColor' => [DefaultValidators::hexColor()], 'mainColor' => [DefaultValidators::hexColor()],
'secondaryColor' => [DefaultValidators::hexColor()] 'secondaryColor' => [DefaultValidators::hexColor()],
], function (HttpRequest $req){ ], function (HttpRequest $req) {
$this->teamModel->createTeam($req['name'],$req['picture'],$req['mainColor'],$req['secondaryColor']); $this->teamModel->createTeam($req['name'], $req['picture'], $req['mainColor'], $req['secondaryColor']);
return HttpResponse::fromCode(HttpCodes::OK); return HttpResponse::fromCode(HttpCodes::OK);
}, true); });
} }
public function deleteTeamSelected(): HttpResponse{ public function deleteTeamSelected(): HttpResponse {
return Control::runChecked([ return APIControl::runChecked([
'teams' => [] 'teams' => [],
], function (HttpRequest $req){ ], function (HttpRequest $req) {
$this->teamModel->deleteTeamSelected($req['teams']); $this->teamModel->deleteTeamSelected($req['teams']);
return HttpResponse::fromCode(HttpCodes::OK); 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);
});
} }

@ -58,11 +58,11 @@ class TeamGateway {
*/ */
public function getTeamById(int $id): ?TeamInfo { public function getTeamById(int $id): ?TeamInfo {
$row = $this->con->fetch( $row = $this->con->fetch(
"SELECT * FROM team WHERE id = :id", "SELECT * FROM team WHERE id = :id",
[ [
":id" => [$id, PDO::PARAM_INT], ":id" => [$id, PDO::PARAM_INT],
] ]
)[0] ?? null; )[0] ?? null;
if ($row == null) { if ($row == null) {
return null; return null;
} }
@ -75,11 +75,11 @@ class TeamGateway {
*/ */
public function getTeamIdByName(string $name): ?int { public function getTeamIdByName(string $name): ?int {
return $this->con->fetch( return $this->con->fetch(
"SELECT id FROM team WHERE name = :name", "SELECT id FROM team WHERE name = :name",
[ [
":name" => [$name, PDO::PARAM_INT], ":name" => [$name, PDO::PARAM_INT],
] ]
)[0]['id'] ?? null; )[0]['id'] ?? null;
} }
/** /**
@ -158,13 +158,19 @@ class TeamGateway {
public function countTeam(): int { public function countTeam(): int {
$result = $this->con->fetch( $result = $this->con->fetch(
"SELECT count(*) as count FROM Team", []); "SELECT count(*) as count FROM Team",
[]
);
if (empty($result) || !isset($result[0]['count'])) { if (empty($result) || !isset($result[0]['count'])) {
return 0; return 0;
} }
return $result[0]['count']; return $result[0]['count'];
} }
/**
* @param array<Team> $selectedTeams
* @return void
*/
public function deleteTeamSelected(array $selectedTeams): void { public function deleteTeamSelected(array $selectedTeams): void {
foreach ($selectedTeams as $team) { foreach ($selectedTeams as $team) {
$this->con->exec( $this->con->exec(

@ -28,16 +28,11 @@ class AuthModel {
* @param string $email * @param string $email
* @return Account|null the registered account or null if the account already exists for the given email address * @return Account|null the registered account or null if the account already exists for the given email address
*/ */
public function register(
public function register(string $username, string $username,
string $password, string $password,
string $confirmPassword, string $email
string $email, ): ?Account {
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)) { if ($this->gateway->exists($email)) {
return null; return null;
} }
@ -58,7 +53,6 @@ class AuthModel {
} catch (Exception $e) { } catch (Exception $e) {
throw new \RuntimeException($e); throw new \RuntimeException($e);
} }
} }
/** /**

@ -149,11 +149,15 @@ class TeamModel {
return $this->teams->listAll($start, $n); return $this->teams->listAll($start, $n);
} }
public function countTeam():int{ public function countTeam(): int {
return $this->teams->countTeam(); return $this->teams->countTeam();
} }
public function deleteTeamSelected(array $selectedTeams){ /**
* @param array<Team> $selectedTeams
* @return void
*/
public function deleteTeamSelected(array $selectedTeams) {
$this->teams->deleteTeamSelected($selectedTeams); $this->teams->deleteTeamSelected($selectedTeams);
} }

Loading…
Cancel
Save