Halfway of getNowPlaying implementation

This commit is contained in:
Deluan
2016-03-17 09:34:32 -04:00
parent a3238ce17b
commit 68c456e188
13 changed files with 113 additions and 29 deletions
+5
View File
@@ -24,6 +24,11 @@ type Entry struct {
Suffix string
BitRate int
ContentType string
UserName string
MinutesAgo int
PlayerId int
PlayerName string
}
type Entries []Entry
+30 -3
View File
@@ -2,9 +2,12 @@ package engine
import (
"math/rand"
"time"
"github.com/astaxie/beego"
"github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/utils"
"github.com/syndtr/goleveldb/leveldb/errors"
)
// TODO Use Entries instead of Albums
@@ -15,14 +18,17 @@ type ListGenerator interface {
GetHighest(offset int, size int) (*domain.Albums, error)
GetRandom(offset int, size int) (*domain.Albums, error)
GetStarred() (*Entries, error)
GetNowPlaying() (*Entries, error)
}
func NewListGenerator(alr domain.AlbumRepository) ListGenerator {
return listGenerator{alr}
func NewListGenerator(alr domain.AlbumRepository, mfr domain.MediaFileRepository, npr NowPlayingRepository) ListGenerator {
return listGenerator{alr, mfr, npr}
}
type listGenerator struct {
albumRepo domain.AlbumRepository
albumRepo domain.AlbumRepository
mfRepository domain.MediaFileRepository
npRepo NowPlayingRepository
}
func (g listGenerator) query(qo domain.QueryOptions, offset int, size int) (*domain.Albums, error) {
@@ -84,3 +90,24 @@ func (g listGenerator) GetStarred() (*Entries, error) {
return &entries, nil
}
func (g listGenerator) GetNowPlaying() (*Entries, error) {
npInfo, err := g.npRepo.GetAll()
if err != nil {
return nil, err
}
entries := make(Entries, len(*npInfo))
for i, np := range *npInfo {
mf, err := g.mfRepository.Get(np.TrackId)
if err != nil {
return nil, err
}
entries[i] = FromMediaFile(mf)
entries[i].UserName = beego.AppConfig.String("user")
entries[i].MinutesAgo = int(time.Now().Sub(np.Start).Minutes())
entries[i].PlayerId = np.PlayerId
entries[i].PlayerName = np.PlayerName
}
return &entries, errors.New("Not implemented")
}
+10 -8
View File
@@ -11,24 +11,26 @@ func CreateMockNowPlayingRepo() *MockNowPlaying {
type MockNowPlaying struct {
NowPlayingRepository
id string
start time.Time
err bool
info NowPlayingInfo
err bool
}
func (m *MockNowPlaying) SetError(err bool) {
m.err = err
}
func (m *MockNowPlaying) Set(id string) error {
func (m *MockNowPlaying) Set(id, username string, playerId int, playerName string) error {
if m.err {
return errors.New("Error!")
}
m.id = id
m.start = time.Now()
m.info.TrackId = id
m.info.Username = username
m.info.Start = time.Now()
m.info.PlayerId = playerId
m.info.PlayerName = playerName
return nil
}
func (m *MockNowPlaying) Current() (string, time.Time) {
return m.id, m.start
func (m *MockNowPlaying) Current() NowPlayingInfo {
return m.info
}
+7 -3
View File
@@ -5,10 +5,14 @@ import "time"
const NowPlayingExpire = time.Duration(30) * time.Minute
type NowPlayingInfo struct {
TrackId string
Start time.Time
TrackId string
Start time.Time
Username string
PlayerId int
PlayerName string
}
type NowPlayingRepository interface {
Set(trackId string) error
Set(trackId, username string, playerId int, playerName string) error
GetAll() (*[]NowPlayingInfo, error)
}
+6 -6
View File
@@ -10,8 +10,8 @@ import (
)
type Scrobbler interface {
Register(id string, playDate time.Time) (*domain.MediaFile, error)
NowPlaying(id string) (*domain.MediaFile, error)
Register(trackId string, playDate time.Time) (*domain.MediaFile, error)
NowPlaying(trackId, username string, playerName string) (*domain.MediaFile, error)
}
func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, npr NowPlayingRepository) Scrobbler {
@@ -40,15 +40,15 @@ func (s *scrobbler) Register(id string, playDate time.Time) (*domain.MediaFile,
return mf, nil
}
func (s *scrobbler) NowPlaying(id string) (*domain.MediaFile, error) {
mf, err := s.mfRepo.Get(id)
func (s *scrobbler) NowPlaying(trackId, username string, playerName string) (*domain.MediaFile, error) {
mf, err := s.mfRepo.Get(trackId)
if err != nil {
return nil, err
}
if mf == nil {
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, id))
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))
}
return mf, s.npRepo.Set(id)
return mf, s.npRepo.Set(trackId, username, 1, playerName)
}
+6 -4
View File
@@ -54,7 +54,7 @@ func TestScrobbler(t *testing.T) {
})
Convey("When I inform the song that is now playing", func() {
mf, err := scrobbler.NowPlaying("2")
mf, err := scrobbler.NowPlaying("2", "deluan", "DSub")
Convey("Then I get the song for that id back", func() {
So(err, ShouldBeNil)
@@ -62,9 +62,11 @@ func TestScrobbler(t *testing.T) {
})
Convey("And it saves the song as the one current playing", func() {
id, start := npRepo.Current()
So(id, ShouldEqual, "2")
So(start, ShouldHappenBefore, time.Now())
info := npRepo.Current()
So(info.TrackId, ShouldEqual, "2")
So(info.Start, ShouldHappenBefore, time.Now())
So(info.Username, ShouldEqual, "deluan")
So(info.PlayerName, ShouldEqual, "DSub")
})
Convey("And iTunes is not notified", func() {