navidrome/engine/scrobbler.go
Deluan b660a70688 Introduced engine.Scrobbler
Also refactored mocks into their original packages, to avoid cyclic references. Is there a better way to have mocks in GoLang tests?
2016-03-16 17:59:28 -04:00

41 lines
840 B
Go

package engine
import (
"errors"
"fmt"
"time"
"github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/itunesbridge"
)
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}
}
type scrobbler struct {
itunes itunesbridge.ItunesControl
mfRepo domain.MediaFileRepository
}
func (s scrobbler) Register(id string, playDate time.Time, submit bool) (*domain.MediaFile, error) {
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))
}
if submit {
if err := s.itunes.MarkAsPlayed(id, playDate); err != nil {
return nil, err
}
}
return mf, nil
}