ctrlsubsonic: implement getGenres

This commit is contained in:
Duncan Overbruck
2020-03-02 16:25:50 +01:00
parent ae31d4a893
commit 14d68f748c
5 changed files with 31 additions and 16 deletions

View File

@@ -275,3 +275,22 @@ func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
}
return sub
}
func (c *Controller) ServeGetGenres(r *http.Request) *spec.Response {
var genres []*db.Genre
c.DB.
Select(`*,
(SELECT count(id) FROM albums WHERE tag_genre_id=genres.id) album_count,
(SELECT count(id) FROM tracks WHERE tag_genre_id=genres.id) track_count`).
Group("genres.id").
Find(&genres)
sub := spec.NewResponse()
sub.Genres = &spec.Genres{
List: make([]*spec.Genre, len(genres)),
}
for i, genre := range genres {
sub.Genres.List[i] = spec.NewGenre(genre)
}
return sub
}

View File

@@ -1,17 +1,4 @@
package ctrlsubsonic
import (
"net/http"
"senan.xyz/g/gonic/server/ctrlsubsonic/spec"
)
// NOTE: when these are implemented, they should be moved to their
// respective _by_folder or _by_tag file
func (c *Controller) ServeGetGenres(r *http.Request) *spec.Response {
sub := spec.NewResponse()
sub.Genres = &spec.Genres{}
sub.Genres.List = []*spec.Genre{}
return sub
}

View File

@@ -72,3 +72,11 @@ func NewArtistByTags(a *db.Artist) *Artist {
AlbumCount: a.AlbumCount,
}
}
func NewGenre(g *db.Genre) *Genre {
return &Genre{
Name: g.Name,
AlbumCount: g.AlbumCount,
SongCount: g.TrackCount,
}
}

View File

@@ -250,8 +250,9 @@ type Genres struct {
}
type Genre struct {
SongCount string `xml:"songCount,attr"`
AlbumCount string `xml:"albumCount,attr"`
Name string `xml:",chardata",json:"value"`
SongCount int `xml:"songCount,attr,omitempty" json:"songCount,omitempty"`
AlbumCount int `xml:"albumCount,attr,omitempty" json:"albumCount,omitempty"`
}
type PlayQueue struct {

View File

@@ -161,8 +161,8 @@ func setupSubsonic(r *mux.Router, ctrl *ctrlsubsonic.Controller) {
r.Handle("/getMusicDirectory{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetMusicDirectory))
r.Handle("/getAlbumList{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetAlbumList))
r.Handle("/search2{_:(?:\\.view)?}", ctrl.H(ctrl.ServeSearchTwo))
// ** begin unimplemented
r.Handle("/getGenres{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetGenres))
// ** begin unimplemented
// middlewares should be run for not found handler
// https://github.com/gorilla/mux/issues/416
notFoundHandler := ctrl.H(ctrl.ServeNotFound)