Now Playing backend implemented

This commit is contained in:
Deluan 2016-03-16 21:04:41 -04:00
parent 9bfb61d994
commit cc89cb5bd1
6 changed files with 49 additions and 11 deletions

View File

@ -30,6 +30,13 @@ func (c *MediaAnnotationController) Scrobble() {
c.SendError(responses.ERROR_GENERIC, "Internal error")
}
beego.Info(fmt.Sprintf(`Scrobbled (%s) "%s" at %v`, id, mf.Title, time))
} else {
mf, err := c.scrobbler.NowPlaying(id)
if err != nil {
beego.Error("Error setting", id, "as current song:", err)
c.SendError(responses.ERROR_GENERIC, "Internal error")
}
beego.Info(fmt.Sprintf(`Current Song (%s) "%s" at %v`, id, mf.Title, time))
}
response := c.NewEmpty()

View File

@ -6,7 +6,7 @@ import (
)
func CreateMockNowPlayingRepo() *MockNowPlaying {
return &MockNowPlaying{data: make(map[string]time.Time)}
return &MockNowPlaying{}
}
type MockNowPlaying struct {
@ -20,7 +20,7 @@ func (m *MockNowPlaying) SetError(err bool) {
m.err = err
}
func (m *MockNowPlaying) Add(id string) error {
func (m *MockNowPlaying) Set(id string) error {
if m.err {
return errors.New("Error!")
}
@ -28,3 +28,7 @@ func (m *MockNowPlaying) Add(id string) error {
m.start = time.Now()
return nil
}
func (m *MockNowPlaying) Current() (string, time.Time) {
return m.id, m.start
}

View File

@ -10,5 +10,5 @@ type NowPlayingInfo struct {
}
type NowPlayingRepository interface {
Add(trackId string) error
Set(trackId string) error
}

View File

@ -41,5 +41,14 @@ func (s *scrobbler) Register(id string, playDate time.Time) (*domain.MediaFile,
}
func (s *scrobbler) NowPlaying(id string) (*domain.MediaFile, error) {
return nil, errors.New("Not implemented")
mf, err := s.mfRepo.Get(id)
if err != nil {
return nil, err
}
if mf == nil {
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, id))
}
return mf, s.npRepo.Set(id)
}

View File

@ -53,6 +53,29 @@ func TestScrobbler(t *testing.T) {
})
})
Convey("When I inform the song that is now playing", func() {
mf, err := scrobbler.NowPlaying("2")
Convey("Then I get the song for that id back", func() {
So(err, ShouldBeNil)
So(mf.Title, ShouldEqual, "Hands Of Time")
})
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())
})
Convey("And iTunes is not notified", func() {
So(itCtrl.played, ShouldNotContainKey, "2")
})
})
Reset(func() {
itCtrl.played = make(map[string]time.Time)
})
})
}

View File

@ -22,7 +22,7 @@ func NewNowPlayingRepository() engine.NowPlayingRepository {
return r
}
func (r *nowPlayingRepository) Add(id string) error {
func (r *nowPlayingRepository) Set(id string) error {
if id == "" {
return errors.New("Id is required")
}
@ -32,12 +32,7 @@ func (r *nowPlayingRepository) Add(id string) error {
if err != nil {
return err
}
err = Db().Set(nowPlayingKeyName, []byte(h))
if err != nil {
return err
}
_, err = Db().Expire(nowPlayingKeyName, int64(engine.NowPlayingExpire.Seconds()))
return err
return Db().SetEX(nowPlayingKeyName, int64(engine.NowPlayingExpire.Seconds()), []byte(h))
}
var _ engine.NowPlayingRepository = (*nowPlayingRepository)(nil)