From f9d85d45f5ae54952c37cb6331b44f4069610219 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 2 Oct 2023 03:18:30 +0400 Subject: [PATCH] Song_ was renamed to Song. Also added Len() method to a SongList. --- internal/http/dj_handlers.go | 6 +++--- internal/radio/song.go | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/internal/http/dj_handlers.go b/internal/http/dj_handlers.go index ea8a743..c515135 100644 --- a/internal/http/dj_handlers.go +++ b/internal/http/dj_handlers.go @@ -64,7 +64,7 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) { return } - song := radio.Song_{ + song := radio.Song{ Artist: oggtag.GetTag(bs, "artist"), Title: oggtag.GetTag(bs, "title"), Duration: oggtag.GetDuration(bs, be), @@ -98,9 +98,9 @@ func (dj *DJHandlers) Songs(w http.ResponseWriter, r *http.Request) { func (dj *DJHandlers) Status(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") err := json.NewEncoder(w).Encode(&struct { - Current *radio.Song_ `json:"current_song,omitempty"` + Current *radio.Song `json:"current_song,omitempty"` Listeners *radio.ListenerCounter `json:"listeners"` - List []radio.Song_ `json:"last_songs,omitempty"` + List []radio.Song `json:"last_songs,omitempty"` Mls *radio.MostListenedSong `json:"most_listened_song,omitempty"` }{ Current: songList.Current(), diff --git a/internal/radio/song.go b/internal/radio/song.go index 64d5e38..28e3ee0 100644 --- a/internal/radio/song.go +++ b/internal/radio/song.go @@ -6,9 +6,9 @@ import ( "time" ) -// Song_ stores artist and title of a song, a timestamp of when it started, and +// Song stores artist and title of a song, a timestamp of when it started, and // a maximum number of listeners. -type Song_ struct { +type Song struct { Artist string Title string Duration time.Duration @@ -16,11 +16,11 @@ type Song_ struct { StartAt time.Time } -func (s *Song_) ArtistTitle() string { +func (s *Song) ArtistTitle() string { return s.Artist + " - " + s.Title } -func (s *Song_) MarshalJSON() ([]byte, error) { +func (s *Song) MarshalJSON() ([]byte, error) { return json.Marshal(&struct { Artist string `json:"artist"` Title string `json:"title"` @@ -37,17 +37,17 @@ func (s *Song_) MarshalJSON() ([]byte, error) { type SongList struct { mut sync.Mutex - current Song_ - lastSongs []Song_ + current Song + lastSongs []Song listMaxLen int } func NewSongList(length int) *SongList { - return &SongList{listMaxLen: length, lastSongs: make([]Song_, 0, length)} + return &SongList{listMaxLen: length, lastSongs: make([]Song, 0, length)} } // Add a new song that is currently playing and update a list. -func (sl *SongList) Add(newSong Song_) { +func (sl *SongList) Add(newSong Song) { sl.mut.Lock() defer sl.mut.Unlock() @@ -61,12 +61,11 @@ func (sl *SongList) Add(newSong Song_) { } else { sl.lastSongs = append(sl.lastSongs, sl.current) } - sl.current = newSong } // Current returns a current playing song. -func (sl *SongList) Current() *Song_ { +func (sl *SongList) Current() *Song { sl.mut.Lock() defer sl.mut.Unlock() if sl.current.StartAt.Year() == 1 { @@ -76,8 +75,12 @@ func (sl *SongList) Current() *Song_ { } // List returns a list of lastly played songs. -func (sl *SongList) List() []Song_ { +func (sl *SongList) List() []Song { sl.mut.Lock() defer sl.mut.Unlock() return sl.lastSongs } + +func (sl *SongList) Len() int { + return sl.listMaxLen +}