50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package http
|
|
|
|
import (
|
|
"dwelling-radio/internal/radio"
|
|
"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)
|
|
}
|