1
0

Song_ was renamed to Song. Also added Len() method to a SongList.

This commit is contained in:
Alexander Andreev 2023-10-02 03:18:30 +04:00
parent d0722131df
commit f9d85d45f5
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 17 additions and 14 deletions

View File

@ -64,7 +64,7 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
return return
} }
song := radio.Song_{ song := radio.Song{
Artist: oggtag.GetTag(bs, "artist"), Artist: oggtag.GetTag(bs, "artist"),
Title: oggtag.GetTag(bs, "title"), Title: oggtag.GetTag(bs, "title"),
Duration: oggtag.GetDuration(bs, be), 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) { func (dj *DJHandlers) Status(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(&struct { 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"` 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"` Mls *radio.MostListenedSong `json:"most_listened_song,omitempty"`
}{ }{
Current: songList.Current(), Current: songList.Current(),

View File

@ -6,9 +6,9 @@ import (
"time" "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. // a maximum number of listeners.
type Song_ struct { type Song struct {
Artist string Artist string
Title string Title string
Duration time.Duration Duration time.Duration
@ -16,11 +16,11 @@ type Song_ struct {
StartAt time.Time StartAt time.Time
} }
func (s *Song_) ArtistTitle() string { func (s *Song) ArtistTitle() string {
return s.Artist + " - " + s.Title return s.Artist + " - " + s.Title
} }
func (s *Song_) MarshalJSON() ([]byte, error) { func (s *Song) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct { return json.Marshal(&struct {
Artist string `json:"artist"` Artist string `json:"artist"`
Title string `json:"title"` Title string `json:"title"`
@ -37,17 +37,17 @@ func (s *Song_) MarshalJSON() ([]byte, error) {
type SongList struct { type SongList struct {
mut sync.Mutex mut sync.Mutex
current Song_ current Song
lastSongs []Song_ lastSongs []Song
listMaxLen int listMaxLen int
} }
func NewSongList(length int) *SongList { 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. // 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() sl.mut.Lock()
defer sl.mut.Unlock() defer sl.mut.Unlock()
@ -61,12 +61,11 @@ func (sl *SongList) Add(newSong Song_) {
} else { } else {
sl.lastSongs = append(sl.lastSongs, sl.current) sl.lastSongs = append(sl.lastSongs, sl.current)
} }
sl.current = newSong sl.current = newSong
} }
// Current returns a current playing song. // Current returns a current playing song.
func (sl *SongList) Current() *Song_ { func (sl *SongList) Current() *Song {
sl.mut.Lock() sl.mut.Lock()
defer sl.mut.Unlock() defer sl.mut.Unlock()
if sl.current.StartAt.Year() == 1 { if sl.current.StartAt.Year() == 1 {
@ -76,8 +75,12 @@ func (sl *SongList) Current() *Song_ {
} }
// List returns a list of lastly played songs. // List returns a list of lastly played songs.
func (sl *SongList) List() []Song_ { func (sl *SongList) List() []Song {
sl.mut.Lock() sl.mut.Lock()
defer sl.mut.Unlock() defer sl.mut.Unlock()
return sl.lastSongs return sl.lastSongs
} }
func (sl *SongList) Len() int {
return sl.listMaxLen
}