Introduced NowPlayingRepository. Don't do anything for now

This commit is contained in:
Deluan 2016-03-16 20:27:48 -04:00
parent b660a70688
commit 4748ce142f
7 changed files with 76 additions and 4 deletions

View file

@ -23,7 +23,6 @@ func (c *MediaAnnotationController) Scrobble() {
id := c.RequiredParamString("id", "Required id parameter is missing") id := c.RequiredParamString("id", "Required id parameter is missing")
time := c.ParamTime("time", time.Now()) time := c.ParamTime("time", time.Now())
submission := c.ParamBool("submission", false) submission := c.ParamBool("submission", false)
println(submission)
if submission { if submission {
mf, err := c.scrobbler.Register(id, time, true) mf, err := c.scrobbler.Register(id, time, true)
if err != nil { if err != nil {

View file

@ -22,6 +22,7 @@ func init() {
// Engine (Use cases) // Engine (Use cases)
utils.DefineSingleton(new(engine.PropertyRepository), persistence.NewPropertyRepository) 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.Browser), engine.NewBrowser)
utils.DefineSingleton(new(engine.ListGenerator), engine.NewListGenerator) utils.DefineSingleton(new(engine.ListGenerator), engine.NewListGenerator)
utils.DefineSingleton(new(engine.Cover), engine.NewCover) utils.DefineSingleton(new(engine.Cover), engine.NewCover)

View file

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

14
engine/nowplaying.go Normal file
View file

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

View file

@ -13,13 +13,14 @@ type Scrobbler interface {
Register(id string, playDate time.Time, submit bool) (*domain.MediaFile, error) Register(id string, playDate time.Time, submit bool) (*domain.MediaFile, error)
} }
func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository) Scrobbler { func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, npr NowPlayingRepository) Scrobbler {
return scrobbler{itunes, mr} return scrobbler{itunes, mr, npr}
} }
type scrobbler struct { type scrobbler struct {
itunes itunesbridge.ItunesControl itunes itunesbridge.ItunesControl
mfRepo domain.MediaFileRepository mfRepo domain.MediaFileRepository
npRepo NowPlayingRepository
} }
func (s scrobbler) Register(id string, playDate time.Time, submit bool) (*domain.MediaFile, error) { func (s scrobbler) Register(id string, playDate time.Time, submit bool) (*domain.MediaFile, error) {

View file

@ -17,9 +17,10 @@ func TestScrobbler(t *testing.T) {
Init(t, false) Init(t, false)
mfRepo := persistence.CreateMockMediaFileRepo() mfRepo := persistence.CreateMockMediaFileRepo()
npRepo := engine.CreateMockNowPlayingRepo()
itCtrl := &mockItunesControl{} itCtrl := &mockItunesControl{}
scrobbler := engine.NewScrobbler(itCtrl, mfRepo) scrobbler := engine.NewScrobbler(itCtrl, mfRepo, npRepo)
Convey("Given a DB with one song", t, func() { Convey("Given a DB with one song", t, func() {
mfRepo.SetData(`[{"Id":"2","Title":"Hands Of Time"}]`, 1) mfRepo.SetData(`[{"Id":"2","Title":"Hands Of Time"}]`, 1)

View file

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