pull/97/head
sam 2 years ago committed by sam
parent 9117d4afab
commit 088b1a5281

@ -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;

@ -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");
}
}

@ -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);
}
}

@ -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<string, mixed> $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);
}
}

@ -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'];
}

@ -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();
}
}

Loading…
Cancel
Save