diff --git a/mpd.go b/mpd.go index e5c499c..f9ac923 100644 --- a/mpd.go +++ b/mpd.go @@ -238,3 +238,22 @@ func deleteTrackFromQueue(c echo.Context) error { return c.String(http.StatusOK, "") } + +// Playlists + +func listPlaylists(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() + + playlists, err := conn.ListPlaylists() + if err != nil { + c.Logger().Error(err) + return c.String(http.StatusBadRequest, err.Error()) + } + + return c.JSON(http.StatusOK, playlists) +} diff --git a/server.go b/server.go index 8c72c12..3a60c0e 100644 --- a/server.go +++ b/server.go @@ -91,6 +91,8 @@ func main() { g.GET("/queue/delete/:song_id", deleteTrackFromQueue) + g.GET("/playlists", listPlaylists) + g.GET("/download", downloadTrack) e.GET("/ws", wsServe) diff --git a/static/controls.js b/static/controls.js index 6a5ca17..8a2ced3 100644 --- a/static/controls.js +++ b/static/controls.js @@ -106,6 +106,18 @@ tab_search.addEventListener("click", e => { }); tab_playlists.addEventListener("click", e => { + 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.appendChild(document.createTextNode(p["playlist"])); + option.value = p["playlist"]; + control_playlist_list.appendChild(option) + }); + } + }); if (!tab_playlists.classList.contains("active")) { tab_browser.classList.remove("active"); tab_search.classList.remove("active")