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"
)
var songList radio.SongList
type DJHandlers struct {
listeners *radio.ListenerCounter
playlist *radio.Playlist
songList *radio.SongList
}
func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist, slLen int) *DJHandlers {
songList = *radio.NewSongList(slLen)
return &DJHandlers{listeners: l, playlist: p}
func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist, sl *radio.SongList, slLen int) *DJHandlers {
return &DJHandlers{listeners: l, playlist: p, songList: sl}
}
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) {
l := dj.listeners.Inc()
go func() {
if l > songList.Current().MaxListeners {
songList.Current().MaxListeners = l
if l > dj.songList.Current().MaxListeners {
dj.songList.Current().MaxListeners = l
}
}()
w.WriteHeader(http.StatusCreated)
@ -71,7 +69,7 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
MaxListeners: dj.listeners.Current(),
StartAt: time.Now()}
// radio.CheckAndUpdateMostListenedSong(song, currentSong)
songList.Add(song)
dj.songList.Add(song)
}()
}
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) {
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 {
log.Println("DJHandlers.Song:", err)
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) {
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 {
log.Println("DJHandlers.Songs:", err)
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"`
Mls *radio.MostListenedSong `json:"most_listened_song,omitempty"`
}{
Current: songList.Current(),
Current: dj.songList.Current(),
Listeners: dj.listeners,
List: songList.List(),
List: dj.songList.List(),
Mls: radio.MostListened()})
if err != nil {
log.Println("DJHandlers.Status:", err)