diff --git a/api/album_list.go b/api/album_list.go index 673e2ea8c..85354c4e9 100644 --- a/api/album_list.go +++ b/api/album_list.go @@ -45,13 +45,8 @@ func (c *AlbumListController) GetAlbumList() { c.SendError(responses.ERROR_GENERIC, "Internal Error") } - albumList := make([]responses.Child, len(albums)) - for i, al := range albums { - albumList[i] = c.ToChild(al) - } - response := c.NewEmpty() - response.AlbumList = &responses.AlbumList{Album: albumList} + response.AlbumList = &responses.AlbumList{Album: c.ToChildren(albums)} c.SendResponse(response) } @@ -64,11 +59,7 @@ func (c *AlbumListController) GetStarred() { response := c.NewEmpty() response.Starred = &responses.Starred{} - response.Starred.Album = make([]responses.Child, len(albums)) - - for i, entry := range albums { - response.Starred.Album[i] = c.ToChild(entry) - } + response.Starred.Album = c.ToChildren(albums) c.SendResponse(response) } diff --git a/api/base_api_controller.go b/api/base_api_controller.go index d304966d8..9225b7206 100644 --- a/api/base_api_controller.go +++ b/api/base_api_controller.go @@ -84,6 +84,14 @@ func (c *BaseAPIController) SendResponse(response responses.Subsonic) { } } +func (c *BaseAPIController) ToChildren(entries engine.Entries) []responses.Child { + children := make([]responses.Child, len(entries)) + for i, entry := range entries { + children[i] = c.ToChild(entry) + } + return children +} + func (c *BaseAPIController) ToChild(entry engine.Entry) responses.Child { n := responses.Child{} n.Id = entry.Id diff --git a/api/browsing.go b/api/browsing.go index 2b44cdd6b..66a5fe038 100644 --- a/api/browsing.go +++ b/api/browsing.go @@ -94,9 +94,6 @@ func (c *BrowsingController) buildDirectory(d *engine.DirectoryInfo) *responses. dir.Starred = &d.Starred } - dir.Child = make([]responses.Child, len(d.Entries)) - for i, entry := range d.Entries { - dir.Child[i] = c.ToChild(entry) - } + dir.Child = c.ToChildren(d.Entries) return dir } diff --git a/api/playlists.go b/api/playlists.go index 79515b3e0..4778400cb 100644 --- a/api/playlists.go +++ b/api/playlists.go @@ -57,26 +57,6 @@ func (c *PlaylistsController) buildPlaylist(d *engine.PlaylistInfo) *responses.P pls.Id = d.Id pls.Name = d.Name - pls.Entry = make([]responses.Child, len(d.Entries)) - for i, child := range d.Entries { - pls.Entry[i].Id = child.Id - pls.Entry[i].Title = child.Title - pls.Entry[i].IsDir = child.IsDir - pls.Entry[i].Parent = child.Parent - pls.Entry[i].Album = child.Album - pls.Entry[i].Year = child.Year - pls.Entry[i].Artist = child.Artist - pls.Entry[i].Genre = child.Genre - pls.Entry[i].CoverArt = child.CoverArt - pls.Entry[i].Track = child.Track - pls.Entry[i].Duration = child.Duration - pls.Entry[i].Size = child.Size - pls.Entry[i].Suffix = child.Suffix - pls.Entry[i].BitRate = child.BitRate - pls.Entry[i].ContentType = child.ContentType - if !child.Starred.IsZero() { - pls.Entry[i].Starred = &child.Starred - } - } + pls.Entry = c.ToChildren(d.Entries) return pls } diff --git a/api/searching.go b/api/searching.go index 06345ebd8..db9f03247 100644 --- a/api/searching.go +++ b/api/searching.go @@ -44,14 +44,8 @@ func (c *SearchingController) Search2() { for i, e := range as { searchResult2.Artist[i] = responses.Artist{Id: e.Id, Name: e.Title} } - searchResult2.Album = make([]responses.Child, len(als)) - for i, e := range als { - searchResult2.Album[i] = c.ToChild(e) - } - searchResult2.Song = make([]responses.Child, len(mfs)) - for i, e := range mfs { - searchResult2.Song[i] = c.ToChild(e) - } + searchResult2.Album = c.ToChildren(als) + searchResult2.Song = c.ToChildren(mfs) response.SearchResult2 = searchResult2 c.SendResponse(response) } diff --git a/engine/search.go b/engine/search.go index 94132f6f2..7731e859e 100644 --- a/engine/search.go +++ b/engine/search.go @@ -9,8 +9,6 @@ import ( "github.com/kennygrant/sanitize" ) -type Results Entries - type Search interface { ClearAll() error IndexArtist(ar *domain.Artist) error @@ -21,9 +19,9 @@ type Search interface { RemoveAlbum(ids []string) error RemoveMediaFile(ids []string) 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) + SearchArtist(q string, offset int, size int) (Entries, error) + SearchAlbum(q string, offset int, size int) (Entries, error) + SearchSong(q string, offset int, size int) (Entries, error) } type search struct { @@ -79,7 +77,7 @@ func (s search) RemoveMediaFile(ids []string) error { return s.idxSong.Remove(ids...) } -func (s search) SearchArtist(q string, offset int, size int) (Results, error) { +func (s search) SearchArtist(q string, offset int, size int) (Entries, error) { q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*"))) min := offset max := min + size - 1 @@ -87,7 +85,7 @@ func (s search) SearchArtist(q string, offset int, size int) (Results, error) { if err != nil { return nil, nil } - res := make(Results, 0, len(resp)) + res := make(Entries, 0, len(resp)) for _, id := range resp { a, err := s.artistRepo.Get(id) if criticalError("Artist", id, err) { @@ -100,7 +98,7 @@ func (s search) SearchArtist(q string, offset int, size int) (Results, error) { return res, nil } -func (s search) SearchAlbum(q string, offset int, size int) (Results, error) { +func (s search) SearchAlbum(q string, offset int, size int) (Entries, error) { q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*"))) min := offset max := min + size - 1 @@ -108,7 +106,7 @@ func (s search) SearchAlbum(q string, offset int, size int) (Results, error) { if err != nil { return nil, nil } - res := make(Results, 0, len(resp)) + res := make(Entries, 0, len(resp)) for _, id := range resp { al, err := s.albumRepo.Get(id) if criticalError("Album", id, err) { @@ -121,7 +119,7 @@ func (s search) SearchAlbum(q string, offset int, size int) (Results, error) { return res, nil } -func (s search) SearchSong(q string, offset int, size int) (Results, error) { +func (s search) SearchSong(q string, offset int, size int) (Entries, error) { q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*"))) min := offset max := min + size - 1 @@ -129,7 +127,7 @@ func (s search) SearchSong(q string, offset int, size int) (Results, error) { if err != nil { return nil, nil } - res := make(Results, 0, len(resp)) + res := make(Entries, 0, len(resp)) for _, id := range resp { mf, err := s.mfileRepo.Get(id) if criticalError("Song", id, err) {