add list playlists

This commit is contained in:
XenGi 2024-04-16 23:37:48 +02:00
parent 79a6049a91
commit bddc82399b
Signed by: xengi
SSH key fingerprint: SHA256:FGp51kRvGOcWnTHiOI39ImwVO4A3fpvR30nPX3LpV7g
3 changed files with 33 additions and 0 deletions

19
mpd.go
View file

@ -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)
}

View file

@ -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)

12
static/controls.js vendored
View file

@ -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")