1
0
dwelling-radio/internal/http/dj_handlers.go

50 lines
1.2 KiB
Go
Raw Normal View History

package http
import (
"dwelling-radio/internal/radio"
"fmt"
"log"
"net/http"
)
type DJHandlers struct {
listeners *radio.Listeners
2023-10-01 01:32:57 +04:00
playlist *radio.Playlist
}
2023-10-01 01:32:57 +04:00
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)
}
2023-10-01 01:32:57 +04:00
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)
}