Refactored object mapping

This commit is contained in:
Deluan
2016-03-11 09:10:40 -05:00
parent 94ccad225d
commit 3a3bd91324
7 changed files with 144 additions and 101 deletions
+7 -38
View File
@@ -54,9 +54,9 @@ func (b browser) Indexes(ifModifiedSince time.Time) (*domain.ArtistIndexes, time
}
type DirectoryInfo struct {
Id string
Name string
Children []Child
Id string
Name string
Entries []Entry
}
func (c browser) Directory(id string) (*DirectoryInfo, error) {
@@ -87,21 +87,9 @@ func (c browser) Directory(id string) (*DirectoryInfo, error) {
func (c browser) buildArtistDir(a *domain.Artist, albums *domain.Albums) *DirectoryInfo {
dir := &DirectoryInfo{Id: a.Id, Name: a.Name}
dir.Children = make([]Child, len(*albums))
dir.Entries = make([]Entry, len(*albums))
for i, al := range *albums {
dir.Children[i].Id = al.Id
dir.Children[i].Title = al.Name
dir.Children[i].IsDir = true
dir.Children[i].Parent = al.ArtistId
dir.Children[i].Album = al.Name
dir.Children[i].Year = al.Year
dir.Children[i].Artist = al.AlbumArtist
dir.Children[i].Genre = al.Genre
dir.Children[i].CoverArt = al.CoverArtId
if al.Starred {
dir.Children[i].Starred = al.UpdatedAt
}
dir.Entries[i] = FromAlbum(&al)
}
return dir
}
@@ -109,28 +97,9 @@ func (c browser) buildArtistDir(a *domain.Artist, albums *domain.Albums) *Direct
func (c browser) buildAlbumDir(al *domain.Album, tracks *domain.MediaFiles) *DirectoryInfo {
dir := &DirectoryInfo{Id: al.Id, Name: al.Name}
dir.Children = make([]Child, len(*tracks))
dir.Entries = make([]Entry, len(*tracks))
for i, mf := range *tracks {
dir.Children[i].Id = mf.Id
dir.Children[i].Title = mf.Title
dir.Children[i].IsDir = false
dir.Children[i].Parent = mf.AlbumId
dir.Children[i].Album = mf.Album
dir.Children[i].Year = mf.Year
dir.Children[i].Artist = mf.Artist
dir.Children[i].Genre = mf.Genre
dir.Children[i].Track = mf.TrackNumber
dir.Children[i].Duration = mf.Duration
dir.Children[i].Size = mf.Size
dir.Children[i].Suffix = mf.Suffix
dir.Children[i].BitRate = mf.BitRate
if mf.Starred {
dir.Children[i].Starred = mf.UpdatedAt
}
if mf.HasCoverArt {
dir.Children[i].CoverArt = mf.Id
}
dir.Children[i].ContentType = mf.ContentType()
dir.Entries[i] = FromMediaFile(&mf)
}
return dir
}
+45 -1
View File
@@ -3,9 +3,11 @@ package engine
import (
"errors"
"time"
"github.com/deluan/gosonic/domain"
)
type Child struct {
type Entry struct {
Id string
Title string
IsDir bool
@@ -27,3 +29,45 @@ type Child struct {
var (
ErrDataNotFound = errors.New("Data Not Found")
)
func FromAlbum(al *domain.Album) Entry {
c := Entry{}
c.Id = al.Id
c.Title = al.Name
c.IsDir = true
c.Parent = al.ArtistId
c.Album = al.Name
c.Year = al.Year
c.Artist = al.AlbumArtist
c.Genre = al.Genre
c.CoverArt = al.CoverArtId
if al.Starred {
c.Starred = al.UpdatedAt
}
return c
}
func FromMediaFile(mf *domain.MediaFile) Entry {
c := Entry{}
c.Id = mf.Id
c.Title = mf.Title
c.IsDir = false
c.Parent = mf.AlbumId
c.Album = mf.Album
c.Year = mf.Year
c.Artist = mf.Artist
c.Genre = mf.Genre
c.Track = mf.TrackNumber
c.Duration = mf.Duration
c.Size = mf.Size
c.Suffix = mf.Suffix
c.BitRate = mf.BitRate
if mf.Starred {
c.Starred = mf.UpdatedAt
}
if mf.HasCoverArt {
c.CoverArt = mf.Id
}
c.ContentType = mf.ContentType()
return c
}
+4 -22
View File
@@ -25,7 +25,7 @@ func (p playlists) GetAll() (*domain.Playlists, error) {
type PlaylistInfo struct {
Id string
Name string
Entries []Child
Entries []Entry
}
func (p playlists) Get(id string) (*PlaylistInfo, error) {
@@ -39,7 +39,7 @@ func (p playlists) Get(id string) (*PlaylistInfo, error) {
}
pinfo := &PlaylistInfo{Id: pl.Id, Name: pl.Name}
pinfo.Entries = make([]Child, len(pl.Tracks))
pinfo.Entries = make([]Entry, len(pl.Tracks))
// TODO Optimize: Get all tracks at once
for i, mfId := range pl.Tracks {
@@ -47,26 +47,8 @@ func (p playlists) Get(id string) (*PlaylistInfo, error) {
if err != nil {
return nil, err
}
pinfo.Entries[i].Id = mf.Id
pinfo.Entries[i].Title = mf.Title
pinfo.Entries[i].IsDir = false
pinfo.Entries[i].Parent = mf.AlbumId
pinfo.Entries[i].Album = mf.Album
pinfo.Entries[i].Year = mf.Year
pinfo.Entries[i].Artist = mf.Artist
pinfo.Entries[i].Genre = mf.Genre
//pinfo.Entries[i].Track = mf.TrackNumber
pinfo.Entries[i].Duration = mf.Duration
pinfo.Entries[i].Size = mf.Size
pinfo.Entries[i].Suffix = mf.Suffix
pinfo.Entries[i].BitRate = mf.BitRate
if mf.Starred {
pinfo.Entries[i].Starred = mf.UpdatedAt
}
if mf.HasCoverArt {
pinfo.Entries[i].CoverArt = mf.Id
}
pinfo.Entries[i].ContentType = mf.ContentType()
pinfo.Entries[i] = FromMediaFile(mf)
pinfo.Entries[i].Track = 0
}
return pinfo, nil
+44 -18
View File
@@ -7,15 +7,17 @@ import (
"github.com/deluan/gosonic/domain"
)
type Results []Entry
type Search interface {
ClearAll() error
IndexArtist(ar *domain.Artist) error
IndexAlbum(al *domain.Album) error
IndexMediaFile(mf *domain.MediaFile) error
SearchArtist(q string, offset int, size int) (*domain.Artists, error)
//SearchAlbum(q string, offset int, size int) (*domain.Albums, error)
//SearchSong(q string, offset int, size int) (*domain.MediaFiles, error)
SearchArtist(q string, offset int, size int) (*Results, error)
SearchAlbum(q string, offset int, size int) (*Results, error)
SearchSong(q string, offset int, size int) (*Results, error)
}
type search struct {
@@ -59,29 +61,53 @@ func (s search) IndexMediaFile(mf *domain.MediaFile) error {
return s.idxSong.Index(mf.Id, strings.ToLower(mf.Title))
}
func (s search) SearchArtist(q string, offset int, size int) (*domain.Artists, error) {
func (s search) SearchArtist(q string, offset int, size int) (*Results, error) {
q = strings.TrimSuffix(q, "*")
res, err := s.sArtist.Search(q)
resp, err := s.sArtist.Search(q)
if err != nil {
return nil, nil
}
as := make(domain.Artists, 0, len(res))
for _, id := range res {
res := make(Results, 0, len(resp))
for i, id := range resp {
a, err := s.artistRepo.Get(id)
if err != nil {
return nil, err
}
as = append(as, *a)
res[i] = Entry{Id: a.Id, Title: a.Name, IsDir: true}
}
return &as, nil
return &res, nil
}
//func (s search) SearchAlbum(q string, offset int, size int) (*domain.Albums, error) {
// q := strings.TrimSuffix(q, "*")
// return nil
//}
//
//func (s search) SearchSong(q string, offset int, size int) (*domain.MediaFiles, error) {
// q := strings.TrimSuffix(q, "*")
// return nil
//}
func (s search) SearchAlbum(q string, offset int, size int) (*Results, error) {
q = strings.TrimSuffix(q, "*")
resp, err := s.sAlbum.Search(q)
if err != nil {
return nil, nil
}
res := make(Results, 0, len(resp))
for i, id := range resp {
al, err := s.albumRepo.Get(id)
if err != nil {
return nil, err
}
res[i] = FromAlbum(al)
}
return &res, nil
}
func (s search) SearchSong(q string, offset int, size int) (*Results, error) {
q = strings.TrimSuffix(q, "*")
resp, err := s.sSong.Search(q)
if err != nil {
return nil, nil
}
res := make(Results, 0, len(resp))
for i, id := range resp {
mf, err := s.mfileRepo.Get(id)
if err != nil {
return nil, err
}
res[i] = FromMediaFile(mf)
}
return &res, nil
}