diff --git a/NOTES.md b/NOTES.md index 03f80fc..b8f027b 100644 --- a/NOTES.md +++ b/NOTES.md @@ -45,8 +45,8 @@ - [ ] Select tracks in results - [ ] `Add` selected tracks to queue button - Playlist browser - - [ ] Show current playlists - - [ ] `Replace` current queue with playlist button + - [x] Show current playlists + - [x] `Replace` current queue with playlist button - [ ] `Attach` playlist to current queue button - [ ] `Save` current queue as playlist button - [x] Show dialog @@ -77,8 +77,10 @@ - [ ] POST `/api/queue` `{"song_id": 123}` - [x] GET `/api/queue/:song_id/delete` - [x] GET `/api/queue/:song_id/move/:position` + - [x] GET `/api/queue/replace/:playlist_name` - [ ] `/api/list_database/:path` - - [ ] `/api/list_playlists` + - [x] GET `/api/playlists` + - [x] GET `/api/playlists/:name` - [ ] `/api/save_playlist` - [ ] `/api/delete_playlist` diff --git a/mpd.go b/mpd.go index 0ad537b..5ea21fe 100644 --- a/mpd.go +++ b/mpd.go @@ -311,3 +311,27 @@ func listPlaylist(c echo.Context) error { return c.JSON(http.StatusOK, playlist) } + +func replaceQueue(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("playlist_name") + + err = conn.Clear() + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusInternalServerError, err.Error()) + } + err = conn.PlaylistLoad(name, -1, -1) + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + + return c.JSON(http.StatusOK, "") +} diff --git a/server.go b/server.go index 4784dc1..67baad6 100644 --- a/server.go +++ b/server.go @@ -91,6 +91,7 @@ func main() { g.GET("/queue/:song_id/delete", deleteTrackFromQueue) g.GET("/queue/:song_id/move/:position", moveTrackInQueue) + g.GET("/queue/replace/:playlist_name", replaceQueue) g.GET("/playlists", listPlaylists) g.GET("/playlists/:name", listPlaylist) diff --git a/static/index.js b/static/index.js index 6acf687..4ffcb18 100644 --- a/static/index.js +++ b/static/index.js @@ -118,15 +118,15 @@ dialog_save_playlist_close.addEventListener("click", () => { // Add API calls to controls -control_replace_playlist.addEventListener("click", e => { - fetch(`${API_URL}/`).then(async r => { +control_replace_playlist.addEventListener("click", () => { + fetch(`${API_URL}/queue/replace/${control_playlist_list.value}`).then(async r => { if (r.status !== 200) { console.error(`API returned ${r.status}: ${r.statusText}`); } }); }); -control_attach_playlist.addEventListener("click", e => { +control_attach_playlist.addEventListener("click", () => { fetch(`${API_URL}/`).then(async r => { if (r.status !== 200) { console.error(`API returned ${r.status}: ${r.statusText}`); @@ -153,7 +153,7 @@ control_delete_playlist.addEventListener("click", () => { }); }); -tab_browser.addEventListener("click", e => { +tab_browser.addEventListener("click", () => { if (!tab_browser.classList.contains("active")) { tab_browser.classList.add("active"); tab_search.classList.remove("active") @@ -164,7 +164,7 @@ tab_browser.addEventListener("click", e => { } }); -tab_search.addEventListener("click", e => { +tab_search.addEventListener("click", () => { if (!tab_search.classList.contains("active")) { tab_browser.classList.remove("active"); tab_search.classList.add("active") @@ -175,7 +175,7 @@ tab_search.addEventListener("click", e => { } }); -tab_playlists.addEventListener("click", e => { +tab_playlists.addEventListener("click", () => { fetch(`${API_URL}/playlists`).then(async r => { if (r.status === 200) { const playlists = await r.json(); @@ -224,7 +224,7 @@ tab_playlists.addEventListener("click", e => { // Add API calls to controls -control_update_db.addEventListener("click", e => { +control_update_db.addEventListener("click", () => { console.log("Issuing database update") fetch(`${API_URL}/update_db`).then(async r => { if (r.status === 200) {