package http import ( "dwelling-radio/internal/radio" "encoding/json" "fmt" "log" "net/http" ) type DJHandlers struct { listeners *radio.Listeners playlist *radio.Playlist } func NewDJHandlers(l *radio.Listeners, p *radio.Playlist) *DJHandlers { return &DJHandlers{listeners: l, playlist: p} } func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) { w.Header().Add("Content-Type", "text/plain") fmt.Fprint(w, dj.listeners.Current(), dj.listeners.Peak()) } func (dj *DJHandlers) ListenersInc(w http.ResponseWriter, _ *http.Request) { l := dj.listeners.Inc() w.WriteHeader(http.StatusCreated) w.Header().Add("Content-Type", "text/plain") fmt.Fprint(w, l) } func (dj *DJHandlers) ListenersDec(w http.ResponseWriter, _ *http.Request) { l, err := dj.listeners.Dec() if err != nil { log.Print(err) http.Error(w, err.Error(), http.StatusBadRequest) } w.WriteHeader(http.StatusOK) w.Header().Add("Content-Type", "text/plain") fmt.Fprint(w, l) } func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) { w.Header().Add("Content-Type", "text/plain") nxt := dj.playlist.Next() if nxt == "" { log.Println("the end of a playlist has been reached") } fmt.Fprint(w, nxt) } 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 { // TODO json.NewEncoder(w).Encode("") } else { // TODO json.NewEncoder(w).Encode("") } }