2023-09-30 23:59:48 +04:00
|
|
|
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-09-30 23:59:48 +04:00
|
|
|
}
|
|
|
|
|
2023-10-01 01:32:57 +04:00
|
|
|
func NewDJHandlers(l *radio.Listeners, p *radio.Playlist) *DJHandlers {
|
|
|
|
return &DJHandlers{listeners: l, playlist: p}
|
2023-09-30 23:59:48 +04:00
|
|
|
}
|
|
|
|
|
2023-10-01 01:32:17 +04:00
|
|
|
func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) {
|
2023-09-30 23:59:48 +04:00
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
fmt.Fprint(w, dj.listeners.Current(), dj.listeners.Peak())
|
|
|
|
}
|
|
|
|
|
2023-10-01 01:32:17 +04:00
|
|
|
func (dj *DJHandlers) ListenersInc(w http.ResponseWriter, _ *http.Request) {
|
2023-09-30 23:59:48 +04:00
|
|
|
l := dj.listeners.Inc()
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
fmt.Fprint(w, l)
|
|
|
|
}
|
|
|
|
|
2023-10-01 01:32:17 +04:00
|
|
|
func (dj *DJHandlers) ListenersDec(w http.ResponseWriter, _ *http.Request) {
|
2023-09-30 23:59:48 +04:00
|
|
|
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)
|
|
|
|
}
|