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
}
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(),

View File

@ -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
}