From 96baa42fe0baa3f5bbf72c9f5b69a06dd60d147a Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 1 Oct 2023 22:05:39 +0400 Subject: [PATCH] /song endpoint was broken into two, the second one is /songs that returns a list of prev played songs, and /song just a current one. --- cmd/dwelling-radiodj/main.go | 4 ++-- internal/http/dj_handlers.go | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/cmd/dwelling-radiodj/main.go b/cmd/dwelling-radiodj/main.go index 16df62d..65c7a1c 100644 --- a/cmd/dwelling-radiodj/main.go +++ b/cmd/dwelling-radiodj/main.go @@ -59,8 +59,8 @@ func main() { s = r.Sub("/playlist") s.Handler(http.MethodGet, "/", djh.PlaylistNext) - s = r.Sub("/song") - s.Handler(http.MethodGet, "/", djh.Song) + r.Handler(http.MethodGet, "/song", djh.Song) + r.Handler(http.MethodGet, "/songs", djh.Songs) srv := ihttp.NewHttpServer(r) diff --git a/internal/http/dj_handlers.go b/internal/http/dj_handlers.go index 0562440..13fdf5f 100644 --- a/internal/http/dj_handlers.go +++ b/internal/http/dj_handlers.go @@ -79,15 +79,23 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) { } func (dj *DJHandlers) Song(w http.ResponseWriter, r *http.Request) { - isList := r.URL.Query().Has("list") w.Header().Add("Content-Type", "application/json") - if isList { - err := json.NewEncoder(w).Encode(songList.List()) - if err != nil { - log.Println("DJHandlers.Song list:", err) - } - } else { - err := json.NewEncoder(w).Encode(songList.Current()) + err := json.NewEncoder(w).Encode(songList.Current()) + if err != nil { + log.Println("DJHandlers.Song:", err) + http.Error(w, "cannot obtain current song", http.StatusInternalServerError) + } +} + +func (dj *DJHandlers) Songs(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + err := json.NewEncoder(w).Encode(songList.List()) + if err != nil { + log.Println("DJHandlers.Songs:", err) + http.Error(w, "cannot obtain list of last songs", http.StatusInternalServerError) + } +} + if err != nil { log.Println("DJHandlers.Song current:", err) }