Removed dependency on engine from persistence

This commit is contained in:
Deluan 2020-01-09 09:51:54 -05:00 committed by Deluan Quintão
parent 542bea6fcc
commit 24cbf766a3
10 changed files with 47 additions and 44 deletions

View file

@ -1,4 +1,4 @@
package engine package domain
import "time" import "time"

View file

@ -1,4 +1,4 @@
package engine package domain
const ( const (
PropLastScan = "LastScan" PropLastScan = "LastScan"

View file

@ -20,13 +20,13 @@ type Browser interface {
GetSong(id string) (*Entry, error) GetSong(id string) (*Entry, error)
} }
func NewBrowser(pr PropertyRepository, fr domain.MediaFolderRepository, ir domain.ArtistIndexRepository, func NewBrowser(pr domain.PropertyRepository, fr domain.MediaFolderRepository, ir domain.ArtistIndexRepository,
ar domain.ArtistRepository, alr domain.AlbumRepository, mr domain.MediaFileRepository) Browser { ar domain.ArtistRepository, alr domain.AlbumRepository, mr domain.MediaFileRepository) Browser {
return &browser{pr, fr, ir, ar, alr, mr} return &browser{pr, fr, ir, ar, alr, mr}
} }
type browser struct { type browser struct {
propRepo PropertyRepository propRepo domain.PropertyRepository
folderRepo domain.MediaFolderRepository folderRepo domain.MediaFolderRepository
indexRepo domain.ArtistIndexRepository indexRepo domain.ArtistIndexRepository
artistRepo domain.ArtistRepository artistRepo domain.ArtistRepository
@ -39,7 +39,7 @@ func (b *browser) MediaFolders() (domain.MediaFolders, error) {
} }
func (b *browser) Indexes(ifModifiedSince time.Time) (domain.ArtistIndexes, time.Time, error) { func (b *browser) Indexes(ifModifiedSince time.Time) (domain.ArtistIndexes, time.Time, error) {
l, err := b.propRepo.DefaultGet(PropLastScan, "-1") l, err := b.propRepo.DefaultGet(domain.PropLastScan, "-1")
ms, _ := strconv.ParseInt(l, 10, 64) ms, _ := strconv.ParseInt(l, 10, 64)
lastModified := utils.ToTime(ms) lastModified := utils.ToTime(ms)

View file

@ -22,14 +22,14 @@ type ListGenerator interface {
GetRandomSongs(size int) (Entries, error) GetRandomSongs(size int) (Entries, error)
} }
func NewListGenerator(alr domain.AlbumRepository, mfr domain.MediaFileRepository, npr NowPlayingRepository) ListGenerator { func NewListGenerator(alr domain.AlbumRepository, mfr domain.MediaFileRepository, npr domain.NowPlayingRepository) ListGenerator {
return &listGenerator{alr, mfr, npr} return &listGenerator{alr, mfr, npr}
} }
type listGenerator struct { type listGenerator struct {
albumRepo domain.AlbumRepository albumRepo domain.AlbumRepository
mfRepository domain.MediaFileRepository mfRepository domain.MediaFileRepository
npRepo NowPlayingRepository npRepo domain.NowPlayingRepository
} }
func (g *listGenerator) query(qo domain.QueryOptions, offset int, size int) (Entries, error) { func (g *listGenerator) query(qo domain.QueryOptions, offset int, size int) (Entries, error) {

View file

@ -3,6 +3,8 @@ package engine
import ( import (
"errors" "errors"
"time" "time"
"github.com/cloudsonic/sonic-server/domain"
) )
func CreateMockNowPlayingRepo() *MockNowPlaying { func CreateMockNowPlayingRepo() *MockNowPlaying {
@ -10,8 +12,8 @@ func CreateMockNowPlayingRepo() *MockNowPlaying {
} }
type MockNowPlaying struct { type MockNowPlaying struct {
NowPlayingRepository domain.NowPlayingRepository
data []NowPlayingInfo data []domain.NowPlayingInfo
t time.Time t time.Time
err bool err bool
} }
@ -20,12 +22,12 @@ func (m *MockNowPlaying) SetError(err bool) {
m.err = err m.err = err
} }
func (m *MockNowPlaying) Enqueue(info *NowPlayingInfo) error { func (m *MockNowPlaying) Enqueue(info *domain.NowPlayingInfo) error {
if m.err { if m.err {
return errors.New("Error!") return errors.New("Error!")
} }
m.data = append(m.data, NowPlayingInfo{}) m.data = append(m.data, domain.NowPlayingInfo{})
copy(m.data[1:], m.data[0:]) copy(m.data[1:], m.data[0:])
m.data[0] = *info m.data[0] = *info
@ -37,7 +39,7 @@ func (m *MockNowPlaying) Enqueue(info *NowPlayingInfo) error {
return nil return nil
} }
func (m *MockNowPlaying) Dequeue(playerId int) (*NowPlayingInfo, error) { func (m *MockNowPlaying) Dequeue(playerId int) (*domain.NowPlayingInfo, error) {
if len(m.data) == 0 { if len(m.data) == 0 {
return nil, nil return nil, nil
} }
@ -52,15 +54,15 @@ func (m *MockNowPlaying) Count(playerId int) (int64, error) {
return int64(len(m.data)), nil return int64(len(m.data)), nil
} }
func (m *MockNowPlaying) GetAll() ([]*NowPlayingInfo, error) { func (m *MockNowPlaying) GetAll() ([]*domain.NowPlayingInfo, error) {
np, err := m.Head(1) np, err := m.Head(1)
if np == nil || err != nil { if np == nil || err != nil {
return nil, err return nil, err
} }
return []*NowPlayingInfo{np}, err return []*domain.NowPlayingInfo{np}, err
} }
func (m *MockNowPlaying) Head(playerId int) (*NowPlayingInfo, error) { func (m *MockNowPlaying) Head(playerId int) (*domain.NowPlayingInfo, error) {
if len(m.data) == 0 { if len(m.data) == 0 {
return nil, nil return nil, nil
} }
@ -68,7 +70,7 @@ func (m *MockNowPlaying) Head(playerId int) (*NowPlayingInfo, error) {
return &info, nil return &info, nil
} }
func (m *MockNowPlaying) Tail(playerId int) (*NowPlayingInfo, error) { func (m *MockNowPlaying) Tail(playerId int) (*domain.NowPlayingInfo, error) {
if len(m.data) == 0 { if len(m.data) == 0 {
return nil, nil return nil, nil
} }
@ -77,7 +79,7 @@ func (m *MockNowPlaying) Tail(playerId int) (*NowPlayingInfo, error) {
} }
func (m *MockNowPlaying) ClearAll() { func (m *MockNowPlaying) ClearAll() {
m.data = make([]NowPlayingInfo, 0) m.data = make([]domain.NowPlayingInfo, 0)
m.err = false m.err = false
} }

View file

@ -2,6 +2,8 @@ package engine
import ( import (
"errors" "errors"
"github.com/cloudsonic/sonic-server/domain"
) )
func CreateMockPropertyRepo() *MockProperty { func CreateMockPropertyRepo() *MockProperty {
@ -9,7 +11,7 @@ func CreateMockPropertyRepo() *MockProperty {
} }
type MockProperty struct { type MockProperty struct {
PropertyRepository domain.PropertyRepository
data map[string]string data map[string]string
err bool err bool
} }

View file

@ -21,14 +21,14 @@ type Scrobbler interface {
NowPlaying(ctx context.Context, playerId int, playerName, trackId, username string) (*domain.MediaFile, error) NowPlaying(ctx context.Context, playerId int, playerName, trackId, username string) (*domain.MediaFile, error)
} }
func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, npr NowPlayingRepository) Scrobbler { func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, npr domain.NowPlayingRepository) Scrobbler {
return &scrobbler{itunes, mr, npr} 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 npRepo domain.NowPlayingRepository
} }
func (s *scrobbler) detectSkipped(ctx context.Context, playerId int, trackId string) { func (s *scrobbler) detectSkipped(ctx context.Context, playerId int, trackId string) {
@ -96,6 +96,6 @@ func (s *scrobbler) NowPlaying(ctx context.Context, playerId int, playerName, tr
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId)) return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))
} }
info := &NowPlayingInfo{TrackId: trackId, Username: username, Start: time.Now(), PlayerId: playerId, PlayerName: playerName} info := &domain.NowPlayingInfo{TrackId: trackId, Username: username, Start: time.Now(), PlayerId: playerId, PlayerName: playerName}
return mf, s.npRepo.Enqueue(info) return mf, s.npRepo.Enqueue(info)
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/cloudsonic/sonic-server/engine" "github.com/cloudsonic/sonic-server/domain"
) )
var ( var (
@ -15,9 +15,9 @@ type nowPlayingRepository struct {
ledisRepository ledisRepository
} }
func NewNowPlayingRepository() engine.NowPlayingRepository { func NewNowPlayingRepository() domain.NowPlayingRepository {
r := &nowPlayingRepository{} r := &nowPlayingRepository{}
r.init("nowplaying", &engine.NowPlayingInfo{}) r.init("nowplaying", &domain.NowPlayingInfo{})
return r return r
} }
@ -25,7 +25,7 @@ func nowPlayingKeyName(playerId int) string {
return fmt.Sprintf("%s:%d", nowPlayingKeyPrefix, playerId) return fmt.Sprintf("%s:%d", nowPlayingKeyPrefix, playerId)
} }
func (r *nowPlayingRepository) Enqueue(info *engine.NowPlayingInfo) error { func (r *nowPlayingRepository) Enqueue(info *domain.NowPlayingInfo) error {
h, err := json.Marshal(info) h, err := json.Marshal(info)
if err != nil { if err != nil {
return err return err
@ -34,11 +34,11 @@ func (r *nowPlayingRepository) Enqueue(info *engine.NowPlayingInfo) error {
keyName := []byte(nowPlayingKeyName(info.PlayerId)) keyName := []byte(nowPlayingKeyName(info.PlayerId))
_, err = Db().LPush(keyName, []byte(h)) _, err = Db().LPush(keyName, []byte(h))
Db().LExpire(keyName, int64(engine.NowPlayingExpire.Seconds())) Db().LExpire(keyName, int64(domain.NowPlayingExpire.Seconds()))
return err return err
} }
func (r *nowPlayingRepository) Dequeue(playerId int) (*engine.NowPlayingInfo, error) { func (r *nowPlayingRepository) Dequeue(playerId int) (*domain.NowPlayingInfo, error) {
keyName := []byte(nowPlayingKeyName(playerId)) keyName := []byte(nowPlayingKeyName(playerId))
val, err := Db().RPop(keyName) val, err := Db().RPop(keyName)
@ -51,7 +51,7 @@ func (r *nowPlayingRepository) Dequeue(playerId int) (*engine.NowPlayingInfo, er
return r.parseInfo(val) return r.parseInfo(val)
} }
func (r *nowPlayingRepository) Head(playerId int) (*engine.NowPlayingInfo, error) { func (r *nowPlayingRepository) Head(playerId int) (*domain.NowPlayingInfo, error) {
keyName := []byte(nowPlayingKeyName(playerId)) keyName := []byte(nowPlayingKeyName(playerId))
val, err := Db().LIndex(keyName, 0) val, err := Db().LIndex(keyName, 0)
@ -61,7 +61,7 @@ func (r *nowPlayingRepository) Head(playerId int) (*engine.NowPlayingInfo, error
return r.parseInfo(val) return r.parseInfo(val)
} }
func (r *nowPlayingRepository) Tail(playerId int) (*engine.NowPlayingInfo, error) { func (r *nowPlayingRepository) Tail(playerId int) (*domain.NowPlayingInfo, error) {
keyName := []byte(nowPlayingKeyName(playerId)) keyName := []byte(nowPlayingKeyName(playerId))
val, err := Db().LIndex(keyName, -1) val, err := Db().LIndex(keyName, -1)
@ -77,16 +77,16 @@ func (r *nowPlayingRepository) Count(playerId int) (int64, error) {
} }
// TODO Will not work for multiple players // TODO Will not work for multiple players
func (r *nowPlayingRepository) GetAll() ([]*engine.NowPlayingInfo, error) { func (r *nowPlayingRepository) GetAll() ([]*domain.NowPlayingInfo, error) {
np, err := r.Head(1) np, err := r.Head(1)
if np == nil || err != nil { if np == nil || err != nil {
return nil, err return nil, err
} }
return []*engine.NowPlayingInfo{np}, err return []*domain.NowPlayingInfo{np}, err
} }
func (r *nowPlayingRepository) parseInfo(val []byte) (*engine.NowPlayingInfo, error) { func (r *nowPlayingRepository) parseInfo(val []byte) (*domain.NowPlayingInfo, error) {
info := &engine.NowPlayingInfo{} info := &domain.NowPlayingInfo{}
err := json.Unmarshal(val, info) err := json.Unmarshal(val, info)
if err != nil { if err != nil {
return nil, nil return nil, nil
@ -94,4 +94,4 @@ func (r *nowPlayingRepository) parseInfo(val []byte) (*engine.NowPlayingInfo, er
return info, nil return info, nil
} }
var _ engine.NowPlayingRepository = (*nowPlayingRepository)(nil) var _ domain.NowPlayingRepository = (*nowPlayingRepository)(nil)

View file

@ -4,21 +4,20 @@ import (
"errors" "errors"
"github.com/cloudsonic/sonic-server/domain" "github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/engine"
) )
type propertyRepository struct { type propertyRepository struct {
ledisRepository ledisRepository
} }
func NewPropertyRepository() engine.PropertyRepository { func NewPropertyRepository() domain.PropertyRepository {
r := &propertyRepository{} r := &propertyRepository{}
r.init("property", &engine.Property{}) r.init("property", &domain.Property{})
return r return r
} }
func (r *propertyRepository) Put(id string, value string) error { func (r *propertyRepository) Put(id string, value string) error {
m := &engine.Property{Id: id, Value: value} m := &domain.Property{Id: id, Value: value}
if m.Id == "" { if m.Id == "" {
return errors.New("Id is required") return errors.New("Id is required")
} }
@ -28,7 +27,7 @@ func (r *propertyRepository) Put(id string, value string) error {
func (r *propertyRepository) Get(id string) (string, error) { func (r *propertyRepository) Get(id string) (string, error) {
var rec interface{} var rec interface{}
rec, err := r.readEntity(id) rec, err := r.readEntity(id)
return rec.(*engine.Property).Value, err return rec.(*domain.Property).Value, err
} }
func (r *propertyRepository) DefaultGet(id string, defaultValue string) (string, error) { func (r *propertyRepository) DefaultGet(id string, defaultValue string) (string, error) {
@ -41,4 +40,4 @@ func (r *propertyRepository) DefaultGet(id string, defaultValue string) (string,
return v, err return v, err
} }
var _ engine.PropertyRepository = (*propertyRepository)(nil) var _ domain.PropertyRepository = (*propertyRepository)(nil)

View file

@ -32,13 +32,13 @@ type Importer struct {
artistRepo domain.ArtistRepository artistRepo domain.ArtistRepository
idxRepo domain.ArtistIndexRepository idxRepo domain.ArtistIndexRepository
plsRepo domain.PlaylistRepository plsRepo domain.PlaylistRepository
propertyRepo engine.PropertyRepository propertyRepo domain.PropertyRepository
search engine.Search search engine.Search
lastScan time.Time lastScan time.Time
lastCheck time.Time lastCheck time.Time
} }
func NewImporter(mediaFolder string, scanner Scanner, mfRepo domain.MediaFileRepository, albumRepo domain.AlbumRepository, artistRepo domain.ArtistRepository, idxRepo domain.ArtistIndexRepository, plsRepo domain.PlaylistRepository, propertyRepo engine.PropertyRepository, search engine.Search) *Importer { func NewImporter(mediaFolder string, scanner Scanner, mfRepo domain.MediaFileRepository, albumRepo domain.AlbumRepository, artistRepo domain.ArtistRepository, idxRepo domain.ArtistIndexRepository, plsRepo domain.PlaylistRepository, propertyRepo domain.PropertyRepository, search engine.Search) *Importer {
return &Importer{ return &Importer{
scanner: scanner, scanner: scanner,
mediaFolder: mediaFolder, mediaFolder: mediaFolder,
@ -106,7 +106,7 @@ func (i *Importer) scan() {
} }
func (i *Importer) lastModifiedSince() time.Time { func (i *Importer) lastModifiedSince() time.Time {
ms, err := i.propertyRepo.Get(engine.PropLastScan) ms, err := i.propertyRepo.Get(domain.PropLastScan)
if err != nil { if err != nil {
log.Warn("Couldn't read LastScan", err) log.Warn("Couldn't read LastScan", err)
return time.Time{} return time.Time{}
@ -166,7 +166,7 @@ func (i *Importer) importLibrary() (err error) {
if err == nil { if err == nil {
millis := time.Now().UnixNano() / int64(time.Millisecond) millis := time.Now().UnixNano() / int64(time.Millisecond)
i.propertyRepo.Put(engine.PropLastScan, fmt.Sprint(millis)) i.propertyRepo.Put(domain.PropLastScan, fmt.Sprint(millis))
log.Debug("LastScan", "timestamp", millis) log.Debug("LastScan", "timestamp", millis)
} }