SAE_RIOT_2024/api/index.php

121 lines
2.4 KiB
PHP
Executable File

<?php
require 'flight/Flight.php';
require 'model/model.php';
MusicAPI::init();
Flight::route('GET /songs(/@id)', 'findSong');
Flight::route('GET /albums(/@id)', 'findAlbum');
Flight::route('GET /artists(/@id)', 'findArtist');
Flight::route('GET /playlists(/@id)', 'findPlaylist');
Flight::route('POST /playlists', 'createPlaylist');
Flight::route('DELETE /playlists/@id', 'deletePlaylist');
function findSong($id = null)
{
$title = Flight::request()->query->title ?? false;
if ($id === null) {
if($title) {
$res = MusicAPI::findAllSongByTitleContaining($title);
} else {
$res = MusicAPI::findAllSong();
}
Flight::json(["results" => $res]);
} else {
$res = MusicAPI::findSongById($id);
if ($res)
{
Flight::json($res);
} else {
Flight::halt(404);
}
}
}
function findAlbum($id = null)
{
$name = Flight::request()->query->name ?? false;
if ($id === null) {
if ($name){
$res = MusicAPI::findAlbumByNameContaining($name);
} else {
$res = MusicAPI::findAllAlbum();
}
Flight::json(["results" => $res]);
} else {
$res = MusicAPI::findAlbumById($id);
if ($res) {
Flight::json($res);
} else {
Flight::halt(404);
}
}
}
function findArtist($id = null)
{
$name = Flight::request()->query->name ?? false;
if ($id === null) {
if ($name){
$res = MusicAPI::findArtsistByNameContaining($name);
} else {
$res = MusicAPI::findAllArtist();
}
Flight::json(["results" => $res]);
}
else {
$res = MusicAPI::findArtsistById($id);
if ($res) {
Flight::json($res);
} else {
Flight::halt(404);
}
}
}
function findPlaylist($id = null)
{
if ($id === null) {
$res = MusicAPI::findAllPlaylists();
Flight::json(["results" => $res]);
} else {
$res = MusicAPI::findPlaylistById($id);
if ($res) {
Flight::json($res);
} else {
Flight::halt(404);
}
}
}
function createPlaylist()
{
$data = Flight::request()->data;
$name = $data->name ?? null;
if ($name) {
$id = MusicAPI::createPlaylist($name);
Flight::json(["id" => $id], 201);
} else {
Flight::halt(400, "Missing name parameter");
}
}
function deletePlaylist($id)
{
$res = MusicAPI::deletePlaylist($id);
if ($res) {
Flight::json(["status" => "success"]);
} else {
Flight::halt(404);
}
}
Flight::start();
?>