diff --git a/api.php b/api.php index 7e4eec8..0e5429b 100644 --- a/api.php +++ b/api.php @@ -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' : diff --git a/resources/database.php b/resources/database.php index fe99465..9f77360 100644 --- a/resources/database.php +++ b/resources/database.php @@ -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 *