add save playlist feature

This commit is contained in:
XenGi 2024-04-18 15:06:12 +02:00
parent 26f230e039
commit cdf12411a2
Signed by: xengi
SSH key fingerprint: SHA256:FGp51kRvGOcWnTHiOI39ImwVO4A3fpvR30nPX3LpV7g
4 changed files with 30 additions and 7 deletions

View file

@ -48,7 +48,7 @@
- [x] Show current playlists - [x] Show current playlists
- [x] `Replace` current queue with playlist button - [x] `Replace` current queue with playlist button
- [x] `Attach` playlist to current queue button - [x] `Attach` playlist to current queue button
- [ ] `Save` current queue as playlist button - [x] `Save` current queue as playlist button
- [x] Show dialog - [x] Show dialog
- [x] `Delete` selected playlist button - [x] `Delete` selected playlist button
@ -81,8 +81,6 @@
- [x] GET `/api/queue/attach/:playlist_name` - [x] GET `/api/queue/attach/:playlist_name`
- [ ] `/api/list_database/:path` - [ ] `/api/list_database/:path`
- [x] GET `/api/playlists` - [x] GET `/api/playlists`
- [x] POST `/api/playlists/:name`
- [x] GET `/api/playlists/:name` - [x] GET `/api/playlists/:name`
- [x] DELETE `/api/playlists/:name` - [x] DELETE `/api/playlists/:name`
- [ ] `/api/save_playlist`
- [ ] `/api/delete_playlist`

20
mpd.go
View file

@ -367,10 +367,28 @@ func deletePlaylist(c echo.Context) error {
err = conn.PlaylistRemove(name) err = conn.PlaylistRemove(name)
if err != nil { if err != nil {
c.Logger().Error(fmt.Sprintf("Couldn't delete playlist %s", name))
c.Logger().Error(err) c.Logger().Error(err)
return c.String(http.StatusBadRequest, err.Error()) return c.String(http.StatusBadRequest, err.Error())
} }
return c.JSON(http.StatusNoContent, "") return c.JSON(http.StatusNoContent, "")
} }
func savePlaylist(c echo.Context) error {
// Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600")
if err != nil {
c.Logger().Error(err)
}
defer conn.Close()
name := c.Param("name")
err = conn.PlaylistSave(name)
if err != nil {
c.Logger().Error(err)
return c.String(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusCreated, "")
}

View file

@ -95,6 +95,7 @@ func main() {
g.GET("/queue/attach/:playlist_name", attachPlaylist) g.GET("/queue/attach/:playlist_name", attachPlaylist)
g.GET("/playlists", listPlaylists) g.GET("/playlists", listPlaylists)
g.POST("/playlists/:name", savePlaylist)
g.GET("/playlists/:name", listPlaylist) g.GET("/playlists/:name", listPlaylist)
g.DELETE("/playlists/:name", deletePlaylist) g.DELETE("/playlists/:name", deletePlaylist)

View file

@ -7,7 +7,7 @@ const VOLUME_STEP = 5;
const dialog_save_playlist = document.getElementById("save-playlist"); const dialog_save_playlist = document.getElementById("save-playlist");
const control_playlist_name = document.getElementById("control-playlist-name"); const control_playlist_name = document.getElementById("control-playlist-name");
const dialog_save_playlist_submit = document.querySelector("#save-playlist button"); const dialog_save_playlist_submit = document.querySelector("#save-playlist form button");
const dialog_save_playlist_close = document.querySelector("#save-playlist .close"); const dialog_save_playlist_close = document.querySelector("#save-playlist .close");
const connection_state = document.getElementById("connection-state"); const connection_state = document.getElementById("connection-state");
@ -144,10 +144,12 @@ tab_playlists.addEventListener("click", () => {
} }
}); });
// Show "Save playlist" modal
control_save_playlist.addEventListener("click", () => { control_save_playlist.addEventListener("click", () => {
dialog_save_playlist.showModal() dialog_save_playlist.showModal()
}); });
// Close "Save playlist" modal
dialog_save_playlist_close.addEventListener("click", () => { dialog_save_playlist_close.addEventListener("click", () => {
dialog_save_playlist.close() dialog_save_playlist.close()
}); });
@ -174,10 +176,14 @@ control_attach_playlist.addEventListener("click", () => {
}); });
}); });
// Save current queue as new playlist and refresh playlist list
dialog_save_playlist_submit.addEventListener("click", () => { dialog_save_playlist_submit.addEventListener("click", () => {
fetch(`${API_URL}/playlists`, {method: "PUT"}).then(async r => { fetch(`${API_URL}/playlists/${control_playlist_name.value}`, {method: "POST"}).then(async r => {
if (r.status === 201) { if (r.status === 201) {
console.log(`Playlist "${control_playlist_name.value}" saved`) console.log(`Playlist "${control_playlist_name.value}" saved`)
refreshPlaylists()
} else {
console.error(`API returned ${r.status}: ${r.statusText}`);
} }
}); });
}); });