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"

View file

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

View file

@ -20,13 +20,13 @@ type Browser interface {
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 {
return &browser{pr, fr, ir, ar, alr, mr}
}
type browser struct {
propRepo PropertyRepository
propRepo domain.PropertyRepository
folderRepo domain.MediaFolderRepository
indexRepo domain.ArtistIndexRepository
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) {
l, err := b.propRepo.DefaultGet(PropLastScan, "-1")
l, err := b.propRepo.DefaultGet(domain.PropLastScan, "-1")
ms, _ := strconv.ParseInt(l, 10, 64)
lastModified := utils.ToTime(ms)

View file

@ -22,14 +22,14 @@ type ListGenerator interface {
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}
}
type listGenerator struct {
albumRepo domain.AlbumRepository
mfRepository domain.MediaFileRepository
npRepo NowPlayingRepository
npRepo domain.NowPlayingRepository
}
func (g *listGenerator) query(qo domain.QueryOptions, offset int, size int) (Entries, error) {

View file

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

View file

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

View file

@ -21,14 +21,14 @@ type Scrobbler interface {
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}
}
type scrobbler struct {
itunes itunesbridge.ItunesControl
mfRepo domain.MediaFileRepository
npRepo NowPlayingRepository
npRepo domain.NowPlayingRepository
}
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))
}
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)
}

View file

@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/cloudsonic/sonic-server/engine"
"github.com/cloudsonic/sonic-server/domain"
)
var (
@ -15,9 +15,9 @@ type nowPlayingRepository struct {
ledisRepository
}
func NewNowPlayingRepository() engine.NowPlayingRepository {
func NewNowPlayingRepository() domain.NowPlayingRepository {
r := &nowPlayingRepository{}
r.init("nowplaying", &engine.NowPlayingInfo{})
r.init("nowplaying", &domain.NowPlayingInfo{})
return r
}
@ -25,7 +25,7 @@ func nowPlayingKeyName(playerId int) string {
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)
if err != nil {
return err
@ -34,11 +34,11 @@ func (r *nowPlayingRepository) Enqueue(info *engine.NowPlayingInfo) error {
keyName := []byte(nowPlayingKeyName(info.PlayerId))
_, err = Db().LPush(keyName, []byte(h))
Db().LExpire(keyName, int64(engine.NowPlayingExpire.Seconds()))
Db().LExpire(keyName, int64(domain.NowPlayingExpire.Seconds()))
return err
}
func (r *nowPlayingRepository) Dequeue(playerId int) (*engine.NowPlayingInfo, error) {
func (r *nowPlayingRepository) Dequeue(playerId int) (*domain.NowPlayingInfo, error) {
keyName := []byte(nowPlayingKeyName(playerId))
val, err := Db().RPop(keyName)
@ -51,7 +51,7 @@ func (r *nowPlayingRepository) Dequeue(playerId int) (*engine.NowPlayingInfo, er
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))
val, err := Db().LIndex(keyName, 0)
@ -61,7 +61,7 @@ func (r *nowPlayingRepository) Head(playerId int) (*engine.NowPlayingInfo, error
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))
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
func (r *nowPlayingRepository) GetAll() ([]*engine.NowPlayingInfo, error) {
func (r *nowPlayingRepository) GetAll() ([]*domain.NowPlayingInfo, error) {
np, err := r.Head(1)
if np == nil || err != nil {
return nil, err
}
return []*engine.NowPlayingInfo{np}, err
return []*domain.NowPlayingInfo{np}, err
}
func (r *nowPlayingRepository) parseInfo(val []byte) (*engine.NowPlayingInfo, error) {
info := &engine.NowPlayingInfo{}
func (r *nowPlayingRepository) parseInfo(val []byte) (*domain.NowPlayingInfo, error) {
info := &domain.NowPlayingInfo{}
err := json.Unmarshal(val, info)
if err != nil {
return nil, nil
@ -94,4 +94,4 @@ func (r *nowPlayingRepository) parseInfo(val []byte) (*engine.NowPlayingInfo, er
return info, nil
}
var _ engine.NowPlayingRepository = (*nowPlayingRepository)(nil)
var _ domain.NowPlayingRepository = (*nowPlayingRepository)(nil)

View file

@ -4,21 +4,20 @@ import (
"errors"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/engine"
)
type propertyRepository struct {
ledisRepository
}
func NewPropertyRepository() engine.PropertyRepository {
func NewPropertyRepository() domain.PropertyRepository {
r := &propertyRepository{}
r.init("property", &engine.Property{})
r.init("property", &domain.Property{})
return r
}
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 == "" {
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) {
var rec interface{}
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) {
@ -41,4 +40,4 @@ func (r *propertyRepository) DefaultGet(id string, defaultValue string) (string,
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
idxRepo domain.ArtistIndexRepository
plsRepo domain.PlaylistRepository
propertyRepo engine.PropertyRepository
propertyRepo domain.PropertyRepository
search engine.Search
lastScan 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{
scanner: scanner,
mediaFolder: mediaFolder,
@ -106,7 +106,7 @@ func (i *Importer) scan() {
}
func (i *Importer) lastModifiedSince() time.Time {
ms, err := i.propertyRepo.Get(engine.PropLastScan)
ms, err := i.propertyRepo.Get(domain.PropLastScan)
if err != nil {
log.Warn("Couldn't read LastScan", err)
return time.Time{}
@ -166,7 +166,7 @@ func (i *Importer) importLibrary() (err error) {
if err == nil {
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)
}