From f709ac4b0251f13080fb7317d33ee9c2a340a584 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 2 Oct 2023 02:28:46 +0400 Subject: [PATCH] Added a MostListenedSong() handler. --- internal/http/dj_handlers.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/http/dj_handlers.go b/internal/http/dj_handlers.go index a5d4be8..d0d7aaf 100644 --- a/internal/http/dj_handlers.go +++ b/internal/http/dj_handlers.go @@ -110,5 +110,17 @@ func (dj *DJHandlers) Status(w http.ResponseWriter, r *http.Request) { http.Error(w, "status parsing failed", http.StatusInternalServerError) } } + +func (dj *DJHandlers) MostListenedSong(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + mls := radio.MostListened() + if mls == nil { + w.WriteHeader(http.StatusNotFound) + return + } + err := json.NewEncoder(w).Encode(mls) + if err != nil { + log.Println("DJHandlers.MostListenedSong:", err) + http.Error(w, "MostListenedSong parsing failed", http.StatusInternalServerError) } }