diff --git a/api/index.php b/api/index.php old mode 100644 new mode 100755 index fe6931d..bd36870 --- a/api/index.php +++ b/api/index.php @@ -8,6 +8,9 @@ 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) { @@ -74,6 +77,44 @@ function findArtist($id = null) } } +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(); ?> diff --git a/api/model/model.php b/api/model/model.php old mode 100644 new mode 100755 index 200d1b6..c3af7fe --- a/api/model/model.php +++ b/api/model/model.php @@ -3,10 +3,10 @@ class Database { private $host; - private $user; - private $pass; + private $user; + private $pass; private $dbname; - private $pdo; + private $pdo; public function __construct($host, $user, $pass, $dbname) { $this->host = $host; @@ -127,5 +127,32 @@ class MusicAPI return $stmt->fetchAll(); } + // Fonctions gestion des playlists + public static function findAllPlaylists() + { + $sql = "SELECT * FROM Playlist;"; + $stmt = self::$db->query($sql); + return $stmt->fetchAll(); + } + + public static function findPlaylistById($id) + { + $sql = "SELECT * FROM Playlist WHERE Playlist.id=?;"; + $stmt = self::$db->query($sql, [$id]); + return $stmt->fetchAll(); + } + + public static function createPlaylist($name) + { + $sql = "INSERT INTO Playlist (name) VALUES (?);"; + self::$db->query($sql, [$name]); + return self::$db->lastInsertId(); + } + + public static function deletePlaylist($id) + { + $sql = "DELETE FROM Playlist WHERE id=?;"; + return self::$db->query($sql, [$id]); + } } ?> diff --git a/index.html b/index.html index ea8fde4..13981fe 100644 --- a/index.html +++ b/index.html @@ -1,11 +1,13 @@ +