add some basic children counts

This commit is contained in:
sentriz
2019-06-07 17:29:57 +01:00
parent 188b52bc61
commit 266014da8b
5 changed files with 26 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ type Artist struct {
IDBase IDBase
Name string `gorm:"not null; unique_index"` Name string `gorm:"not null; unique_index"`
Albums []*Album `gorm:"foreignkey:TagArtistID"` Albums []*Album `gorm:"foreignkey:TagArtistID"`
AlbumCount int `sql:"-"`
} }
type Track struct { type Track struct {
@@ -81,6 +82,7 @@ type Album struct {
TagTitle string `gorm:"index" sql:"default: null"` TagTitle string `gorm:"index" sql:"default: null"`
TagYear int `sql:"default: null"` TagYear int `sql:"default: null"`
Tracks []*Track Tracks []*Track
ChildCount int `sql:"-"`
ReceivedPaths bool `gorm:"-"` ReceivedPaths bool `gorm:"-"`
ReceivedTags bool `gorm:"-"` ReceivedTags bool `gorm:"-"`
} }

View File

@@ -63,6 +63,7 @@ func newArtistByFolder(f *model.Album) *subsonic.Artist {
return &subsonic.Artist{ return &subsonic.Artist{
ID: f.ID, ID: f.ID,
Name: f.RightPath, Name: f.RightPath,
AlbumCount: f.ChildCount,
} }
} }

View File

@@ -57,5 +57,6 @@ func newArtistByTags(a *model.Artist) *subsonic.Artist {
return &subsonic.Artist{ return &subsonic.Artist{
ID: a.ID, ID: a.ID,
Name: a.Name, Name: a.Name,
AlbumCount: a.AlbumCount,
} }
} }

View File

@@ -20,8 +20,15 @@ import (
func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) { func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) {
var folders []*model.Album var folders []*model.Album
c.DB. 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) Find(&folders)
indexMap := make(map[string]*subsonic.Index) indexMap := make(map[string]*subsonic.Index)
indexes := []*subsonic.Index{} indexes := []*subsonic.Index{}

View File

@@ -14,7 +14,14 @@ import (
func (c *Controller) GetArtists(w http.ResponseWriter, r *http.Request) { func (c *Controller) GetArtists(w http.ResponseWriter, r *http.Request) {
var artists []*model.Artist 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) indexMap := make(map[string]*subsonic.Index)
indexes := &subsonic.Artists{} indexes := &subsonic.Artists{}
for _, artist := range artists { for _, artist := range artists {