diff --git a/api/media_annotation.go b/api/media_annotation.go index 901e763fa..3bfdd4872 100644 --- a/api/media_annotation.go +++ b/api/media_annotation.go @@ -23,7 +23,6 @@ func (c *MediaAnnotationController) Scrobble() { id := c.RequiredParamString("id", "Required id parameter is missing") time := c.ParamTime("time", time.Now()) submission := c.ParamBool("submission", false) - println(submission) if submission { mf, err := c.scrobbler.Register(id, time, true) if err != nil { diff --git a/conf/inject_definitions.go b/conf/inject_definitions.go index c38718a6e..daf42f780 100644 --- a/conf/inject_definitions.go +++ b/conf/inject_definitions.go @@ -22,6 +22,7 @@ func init() { // Engine (Use cases) utils.DefineSingleton(new(engine.PropertyRepository), persistence.NewPropertyRepository) + utils.DefineSingleton(new(engine.NowPlayingRepository), persistence.NewNowPlayingRepository) utils.DefineSingleton(new(engine.Browser), engine.NewBrowser) utils.DefineSingleton(new(engine.ListGenerator), engine.NewListGenerator) utils.DefineSingleton(new(engine.Cover), engine.NewCover) diff --git a/engine/mock_nowplaying_repo.go b/engine/mock_nowplaying_repo.go new file mode 100644 index 000000000..a1a95cb30 --- /dev/null +++ b/engine/mock_nowplaying_repo.go @@ -0,0 +1,28 @@ +package engine + +import ( + "errors" + "time" +) + +func CreateMockNowPlayingRepo() *MockNowPlaying { + return &MockNowPlaying{data: make(map[string]time.Time)} +} + +type MockNowPlaying struct { + NowPlayingRepository + data map[string]time.Time + err bool +} + +func (m *MockNowPlaying) SetError(err bool) { + m.err = err +} + +func (m *MockNowPlaying) Add(id string) error { + if m.err { + return errors.New("Error!") + } + m.data[id] = time.Now() + return nil +} diff --git a/engine/nowplaying.go b/engine/nowplaying.go new file mode 100644 index 000000000..65d7acb60 --- /dev/null +++ b/engine/nowplaying.go @@ -0,0 +1,14 @@ +package engine + +import "time" + +const NowPlayingExpire = time.Duration(30) * time.Minute + +type NowPlayingInfo struct { + TrackId string + Start time.Time +} + +type NowPlayingRepository interface { + Add(trackId string) error +} diff --git a/engine/scrobbler.go b/engine/scrobbler.go index 3347fcac9..fed7366f8 100644 --- a/engine/scrobbler.go +++ b/engine/scrobbler.go @@ -13,13 +13,14 @@ type Scrobbler interface { Register(id string, playDate time.Time, submit bool) (*domain.MediaFile, error) } -func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository) Scrobbler { - return scrobbler{itunes, mr} +func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, npr NowPlayingRepository) Scrobbler { + return scrobbler{itunes, mr, npr} } type scrobbler struct { itunes itunesbridge.ItunesControl mfRepo domain.MediaFileRepository + npRepo NowPlayingRepository } func (s scrobbler) Register(id string, playDate time.Time, submit bool) (*domain.MediaFile, error) { diff --git a/engine/scrobbler_test.go b/engine/scrobbler_test.go index d7bea3358..153c0b1f0 100644 --- a/engine/scrobbler_test.go +++ b/engine/scrobbler_test.go @@ -17,9 +17,10 @@ func TestScrobbler(t *testing.T) { Init(t, false) mfRepo := persistence.CreateMockMediaFileRepo() + npRepo := engine.CreateMockNowPlayingRepo() itCtrl := &mockItunesControl{} - scrobbler := engine.NewScrobbler(itCtrl, mfRepo) + scrobbler := engine.NewScrobbler(itCtrl, mfRepo, npRepo) Convey("Given a DB with one song", t, func() { mfRepo.SetData(`[{"Id":"2","Title":"Hands Of Time"}]`, 1) diff --git a/persistence/nowplaying_repository.go b/persistence/nowplaying_repository.go new file mode 100644 index 000000000..ccbd02f79 --- /dev/null +++ b/persistence/nowplaying_repository.go @@ -0,0 +1,28 @@ +package persistence + +import ( + "errors" + "time" + + "github.com/deluan/gosonic/engine" +) + +type nowPlayingRepository struct { + ledisRepository +} + +func NewNowPlayingRepository() engine.NowPlayingRepository { + r := &nowPlayingRepository{} + r.init("nnowplaying", &engine.NowPlayingInfo{}) + return r +} + +func (r *nowPlayingRepository) Add(id string) error { + if id == "" { + return errors.New("Id is required") + } + m := &engine.NowPlayingInfo{TrackId: id, Start: time.Now()} + return r.saveOrUpdate(m.TrackId, m) +} + +var _ engine.NowPlayingRepository = (*nowPlayingRepository)(nil)