Add duration and track_count to getArtist, getAlbum and getAlbumList2

This commit is contained in:
Duncan Overbruck
2020-07-24 14:27:26 +02:00
committed by Senan Kelly
parent 606d761039
commit df679a6ae3
3 changed files with 11 additions and 2 deletions

View File

@@ -54,7 +54,12 @@ func (c *Controller) ServeGetArtist(r *http.Request) *spec.Response {
}
artist := &db.Artist{}
c.DB.
Preload("Albums").
Preload("Albums", func(db *gorm.DB) *gorm.DB {
return db.
Select("*, count(sub.id) child_count, sum(sub.length) duration").
Joins("LEFT JOIN tracks sub ON albums.id=sub.album_id").
Group("albums.id")
}).
First(artist, id.Value)
sub := spec.NewResponse()
sub.Artist = spec.NewArtistByTags(artist)
@@ -74,6 +79,8 @@ func (c *Controller) ServeGetAlbum(r *http.Request) *spec.Response {
}
album := &db.Album{}
err = c.DB.
Select("albums.*, count(tracks.id) child_count, sum(tracks.length) duration").
Joins("LEFT JOIN tracks ON tracks.album_id=albums.id").
Preload("TagArtist").
Preload("TagGenre").
Preload("Tracks", func(db *gorm.DB) *gorm.DB {
@@ -139,7 +146,7 @@ func (c *Controller) ServeGetAlbumListTwo(r *http.Request) *spec.Response {
// TODO: think about removing this extra join to count number
// of children. it might make sense to store that in the db
q.
Select("albums.*, count(tracks.id) child_count").
Select("albums.*, count(tracks.id) child_count, sum(tracks.length) duration").
Joins("LEFT JOIN tracks ON tracks.album_id=albums.id").
Group("albums.id").
Where("albums.tag_artist_id IS NOT NULL").

View File

@@ -13,6 +13,7 @@ func NewAlbumByTags(a *db.Album, artist *db.Artist) *Album {
Name: a.TagTitle,
Year: a.TagYear,
TrackCount: a.ChildCount,
Duration: a.Duration,
}
if a.TagGenre != nil {
ret.Genre = a.TagGenre.Name

View File

@@ -172,6 +172,7 @@ type Album struct {
TagYear int `sql:"default: null"`
Tracks []*Track
ChildCount int `sql:"-"`
Duration int `sql:"-"`
ReceivedPaths bool `gorm:"-"`
ReceivedTags bool `gorm:"-"`
}