1
0

Pass a SongList instead of instantiating.

This commit is contained in:
Alexander Andreev 2023-10-02 03:18:54 +04:00
parent f9d85d45f5
commit 58b3d18288
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -10,16 +10,14 @@ import (
"time" "time"
) )
var songList radio.SongList
type DJHandlers struct { type DJHandlers struct {
listeners *radio.ListenerCounter listeners *radio.ListenerCounter
playlist *radio.Playlist playlist *radio.Playlist
songList *radio.SongList
} }
func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist, slLen int) *DJHandlers { func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist, sl *radio.SongList, slLen int) *DJHandlers {
songList = *radio.NewSongList(slLen) return &DJHandlers{listeners: l, playlist: p, songList: sl}
return &DJHandlers{listeners: l, playlist: p}
} }
func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) { func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) {
@ -30,8 +28,8 @@ func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) {
func (dj *DJHandlers) ListenersInc(w http.ResponseWriter, _ *http.Request) { func (dj *DJHandlers) ListenersInc(w http.ResponseWriter, _ *http.Request) {
l := dj.listeners.Inc() l := dj.listeners.Inc()
go func() { go func() {
if l > songList.Current().MaxListeners { if l > dj.songList.Current().MaxListeners {
songList.Current().MaxListeners = l dj.songList.Current().MaxListeners = l
} }
}() }()
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
@ -71,7 +69,7 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
MaxListeners: dj.listeners.Current(), MaxListeners: dj.listeners.Current(),
StartAt: time.Now()} StartAt: time.Now()}
// radio.CheckAndUpdateMostListenedSong(song, currentSong) // radio.CheckAndUpdateMostListenedSong(song, currentSong)
songList.Add(song) dj.songList.Add(song)
}() }()
} }
fmt.Fprint(w, nxt) fmt.Fprint(w, nxt)
@ -79,7 +77,7 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
func (dj *DJHandlers) Song(w http.ResponseWriter, r *http.Request) { func (dj *DJHandlers) Song(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(songList.Current()) err := json.NewEncoder(w).Encode(dj.songList.Current())
if err != nil { if err != nil {
log.Println("DJHandlers.Song:", err) log.Println("DJHandlers.Song:", err)
http.Error(w, "cannot obtain current song", http.StatusInternalServerError) http.Error(w, "cannot obtain current song", http.StatusInternalServerError)
@ -88,7 +86,7 @@ func (dj *DJHandlers) Song(w http.ResponseWriter, r *http.Request) {
func (dj *DJHandlers) Songs(w http.ResponseWriter, r *http.Request) { func (dj *DJHandlers) Songs(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(songList.List()) err := json.NewEncoder(w).Encode(dj.songList.List())
if err != nil { if err != nil {
log.Println("DJHandlers.Songs:", err) log.Println("DJHandlers.Songs:", err)
http.Error(w, "cannot obtain list of last songs", http.StatusInternalServerError) http.Error(w, "cannot obtain list of last songs", http.StatusInternalServerError)
@ -103,9 +101,9 @@ func (dj *DJHandlers) Status(w http.ResponseWriter, r *http.Request) {
List []radio.Song `json:"last_songs,omitempty"` List []radio.Song `json:"last_songs,omitempty"`
Mls *radio.MostListenedSong `json:"most_listened_song,omitempty"` Mls *radio.MostListenedSong `json:"most_listened_song,omitempty"`
}{ }{
Current: songList.Current(), Current: dj.songList.Current(),
Listeners: dj.listeners, Listeners: dj.listeners,
List: songList.List(), List: dj.songList.List(),
Mls: radio.MostListened()}) Mls: radio.MostListened()})
if err != nil { if err != nil {
log.Println("DJHandlers.Status:", err) log.Println("DJHandlers.Status:", err)