add function (o get matches for a sport

This commit is contained in:
0nano
2022-10-14 11:39:54 +02:00
committed by Youn Mélois
parent 64cc247f7b
commit 28589f8a44
2 changed files with 34 additions and 5 deletions

19
api.php
View File

@@ -86,13 +86,22 @@
APIErrors::internalError();
}
case 'matchs' . 'GET' :
$matchs = $db->getAllMatches();
if (empty($_GET['sport_id'])){
$matchs = $db->getAllMatches();
if ($matchs != NULL){
http_response_code(200);
die(json_encode($matchs));
if ($matchs != NULL){
http_response_code(200);
die(json_encode($matchs));
}else{
APIErrors::internalError();}
}else{
APIErrors::internalError();
$matchs = $db->getAllMatchesSport($_GET['sport_id']);
if ($matchs != NULL){
http_response_code(200);
die(json_encode($matchs));
}else{
APIErrors::internalError();}
}
case 'test' . 'GET' :

View File

@@ -293,6 +293,26 @@ class Database
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
/**
* Gets all matches in the bd with their type for a sport
*
* @param int $sportId
*
* @return ?array return null is there is no match in the db
*/
public function getAllMatchesSport(int $sportId): ?array {
$request = 'SELECT m.id, type, array_agg(p.team_id) "teams_id" from matches m
left join sports s on sport_id = s.id
right join participations p on match_id = m.id
where sport_id = :id group by m.id, s.name';
$statement = $this->PDO->prepare($request);
$statement->bindParam(":id", $sportId);
$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
/**
* Gets all infor of a match
*