diff --git a/mpd.go b/mpd.go index 9b76102..ca3c033 100644 --- a/mpd.go +++ b/mpd.go @@ -274,6 +274,49 @@ func moveTrackInQueue(c echo.Context) error { return c.String(http.StatusOK, fmt.Sprintf("Moved song %d to position %d", songId, position)) } +func attachPlaylist(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.PlaylistLoad(name, -1, -1) + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + + return c.JSON(http.StatusOK, "") +} + +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, "") +} + // Playlists func listPlaylists(c echo.Context) error { @@ -312,7 +355,7 @@ func listPlaylist(c echo.Context) error { return c.JSON(http.StatusOK, playlist) } -func replaceQueue(c echo.Context) error { +func deletePlaylist(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") if err != nil { @@ -320,37 +363,14 @@ func replaceQueue(c echo.Context) error { } defer conn.Close() - name := c.Param("playlist_name") + name := c.Param("name") - err = conn.Clear() - if err != nil { - c.Logger().Error(err) - return c.String(http.StatusInternalServerError, err.Error()) - } - err = conn.PlaylistLoad(name, -1, -1) + err = conn.PlaylistRemove(name) if err != nil { + c.Logger().Error(fmt.Sprintf("Couldn't delete playlist %s", name)) c.Logger().Error(err) return c.String(http.StatusBadRequest, err.Error()) } - return c.JSON(http.StatusOK, "") -} - -func attachPlaylist(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.PlaylistLoad(name, -1, -1) - if err != nil { - c.Logger().Error(err) - return c.String(http.StatusBadRequest, err.Error()) - } - - return c.JSON(http.StatusOK, "") + return c.JSON(http.StatusNoContent, "") } diff --git a/server.go b/server.go index 2a8881c..bace2a9 100644 --- a/server.go +++ b/server.go @@ -96,6 +96,7 @@ func main() { g.GET("/playlists", listPlaylists) g.GET("/playlists/:name", listPlaylist) + g.DELETE("/playlists/:name", deletePlaylist) g.GET("/download", downloadTrack) diff --git a/static/index.js b/static/index.js index 17369df..bf54193 100644 --- a/static/index.js +++ b/static/index.js @@ -69,6 +69,46 @@ moveTrackInQueue = (event, direction) => { }); } +refreshPlaylists = () => { + console.log("Refreshing playlists from MPD server") + fetch(`${API_URL}/playlists`).then(async r => { + if (r.status === 200) { + const playlists = await r.json(); + control_playlist_list.options.length = 0; // clear playlists + playlists.forEach(p => { + const option = document.createElement("option") + option.innerText = p["playlist"]; + option.value = p["playlist"]; + option.addEventListener("click", () => { + fetch(`${API_URL}/playlists/${p["playlist"]}`).then(async r => { + if (r.status === 200) { + const songs = await r.json(); + console.log(songs) + result_table.innerHTML = ""; + songs.forEach(song => { + const tr = document.createElement("tr"); + const artist = document.createElement("td"); + artist.innerText = song["Artist"]; + const title = document.createElement("td"); + title.innerText = song["Title"]; + const time = document.createElement("td"); + time.innerText = secondsToTrackTime(parseInt(song["Time"])) + tr.appendChild(artist); + tr.appendChild(title); + tr.appendChild(document.createElement("td")); // album + tr.appendChild(document.createElement("td")); // genre + tr.appendChild(time); + result_table.appendChild(tr); + }); + } + }) + }); + control_playlist_list.appendChild(option) + }); + } + }); +} + // UI controls tab_browser.addEventListener("click", () => { @@ -143,10 +183,11 @@ dialog_save_playlist_submit.addEventListener("click", () => { }); control_delete_playlist.addEventListener("click", () => { - const playlist_id = control_playlist_list.value; - fetch(`${API_URL}/playlists/${playlist_id}`, {method: "DELETE"}).then(r => { + const playlist_name = control_playlist_list.value; + fetch(`${API_URL}/playlists/${control_playlist_list.value}`, {method: "DELETE"}).then(r => { if (r.status === 204) { - console.log(`Playlist ${playlist_id} successfully deleted.`); + console.log(`Playlist "${playlist_name}" successfully deleted.`); + refreshPlaylists(); } else { console.error(`API returned ${r.status}: ${r.statusText}`); } @@ -176,42 +217,7 @@ tab_search.addEventListener("click", () => { }); tab_playlists.addEventListener("click", () => { - fetch(`${API_URL}/playlists`).then(async r => { - if (r.status === 200) { - const playlists = await r.json(); - control_playlist_list.options.length = 0; // clear playlists - playlists.forEach(p => { - const option = document.createElement("option") - option.innerText = p["playlist"]; - option.value = p["playlist"]; - option.addEventListener("click", () => { - fetch(`${API_URL}/playlists/${p["playlist"]}`).then(async r => { - if (r.status === 200) { - const songs = await r.json(); - console.log(songs) - result_table.innerHTML = ""; - songs.forEach(song => { - const tr = document.createElement("tr"); - const artist = document.createElement("td"); - artist.innerText = song["Artist"]; - const title = document.createElement("td"); - title.innerText = song["Title"]; - const time = document.createElement("td"); - time.innerText = secondsToTrackTime(parseInt(song["Time"])) - tr.appendChild(artist); - tr.appendChild(title); - tr.appendChild(document.createElement("td")); // album - tr.appendChild(document.createElement("td")); // genre - tr.appendChild(time); - result_table.appendChild(tr); - }); - } - }) - }); - control_playlist_list.appendChild(option) - }); - } - }); + refreshPlaylists(); if (!tab_playlists.classList.contains("active")) { tab_browser.classList.remove("active"); tab_search.classList.remove("active")