From 266014da8b03df626ff8ccee5748363ea60f96df Mon Sep 17 00:00:00 2001 From: sentriz Date: Fri, 7 Jun 2019 17:29:57 +0100 Subject: [PATCH] add some basic children counts --- model/model.go | 6 ++++-- server/handler/construct_sub_by_folder.go | 5 +++-- server/handler/construct_sub_by_tags.go | 5 +++-- server/handler/handler_sub_by_folder.go | 9 ++++++++- server/handler/handler_sub_by_tags.go | 9 ++++++++- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/model/model.go b/model/model.go index 6717089..34cba87 100644 --- a/model/model.go +++ b/model/model.go @@ -9,8 +9,9 @@ import ( type Artist struct { IDBase - Name string `gorm:"not null; unique_index"` - Albums []*Album `gorm:"foreignkey:TagArtistID"` + Name string `gorm:"not null; unique_index"` + Albums []*Album `gorm:"foreignkey:TagArtistID"` + AlbumCount int `sql:"-"` } type Track struct { @@ -81,6 +82,7 @@ type Album struct { TagTitle string `gorm:"index" sql:"default: null"` TagYear int `sql:"default: null"` Tracks []*Track + ChildCount int `sql:"-"` ReceivedPaths bool `gorm:"-"` ReceivedTags bool `gorm:"-"` } diff --git a/server/handler/construct_sub_by_folder.go b/server/handler/construct_sub_by_folder.go index a76046a..49532b4 100644 --- a/server/handler/construct_sub_by_folder.go +++ b/server/handler/construct_sub_by_folder.go @@ -61,8 +61,9 @@ func newTCTrackByFolder(t *model.Track, parent *model.Album) *subsonic.TrackChil func newArtistByFolder(f *model.Album) *subsonic.Artist { return &subsonic.Artist{ - ID: f.ID, - Name: f.RightPath, + ID: f.ID, + Name: f.RightPath, + AlbumCount: f.ChildCount, } } diff --git a/server/handler/construct_sub_by_tags.go b/server/handler/construct_sub_by_tags.go index e5a895e..957991c 100644 --- a/server/handler/construct_sub_by_tags.go +++ b/server/handler/construct_sub_by_tags.go @@ -55,7 +55,8 @@ func newTrackByTags(t *model.Track, album *model.Album) *subsonic.TrackChild { func newArtistByTags(a *model.Artist) *subsonic.Artist { return &subsonic.Artist{ - ID: a.ID, - Name: a.Name, + ID: a.ID, + Name: a.Name, + AlbumCount: a.AlbumCount, } } diff --git a/server/handler/handler_sub_by_folder.go b/server/handler/handler_sub_by_folder.go index 01847d7..5d8a2ad 100644 --- a/server/handler/handler_sub_by_folder.go +++ b/server/handler/handler_sub_by_folder.go @@ -20,8 +20,15 @@ import ( func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) { var folders []*model.Album + c.DB. - Where("parent_id = 1"). + Select("*, count(sub.id) as child_count"). + Joins(` + LEFT JOIN albums sub + ON albums.id = sub.parent_id + `). + Where("albums.parent_id = 1"). + Group("albums.id"). Find(&folders) indexMap := make(map[string]*subsonic.Index) indexes := []*subsonic.Index{} diff --git a/server/handler/handler_sub_by_tags.go b/server/handler/handler_sub_by_tags.go index 716d1eb..1732846 100644 --- a/server/handler/handler_sub_by_tags.go +++ b/server/handler/handler_sub_by_tags.go @@ -14,7 +14,14 @@ import ( func (c *Controller) GetArtists(w http.ResponseWriter, r *http.Request) { var artists []*model.Artist - c.DB.Find(&artists) + c.DB. + Select("*, count(sub.id) as album_count"). + Joins(` + LEFT JOIN albums sub + ON artists.id = sub.tag_artist_id + `). + Group("artists.id"). + Find(&artists) indexMap := make(map[string]*subsonic.Index) indexes := &subsonic.Artists{} for _, artist := range artists {