diff --git a/server/ctrlsubsonic/handlers_by_tags.go b/server/ctrlsubsonic/handlers_by_tags.go index 548ccf9..e1941c7 100644 --- a/server/ctrlsubsonic/handlers_by_tags.go +++ b/server/ctrlsubsonic/handlers_by_tags.go @@ -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 +} diff --git a/server/ctrlsubsonic/handlers_unimplemented.go b/server/ctrlsubsonic/handlers_unimplemented.go index 0767ede..e518df3 100644 --- a/server/ctrlsubsonic/handlers_unimplemented.go +++ b/server/ctrlsubsonic/handlers_unimplemented.go @@ -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 -} diff --git a/server/ctrlsubsonic/spec/construct_by_tags.go b/server/ctrlsubsonic/spec/construct_by_tags.go index b42c27b..3314412 100644 --- a/server/ctrlsubsonic/spec/construct_by_tags.go +++ b/server/ctrlsubsonic/spec/construct_by_tags.go @@ -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, + } +} diff --git a/server/ctrlsubsonic/spec/spec.go b/server/ctrlsubsonic/spec/spec.go index 9f979ef..99e761c 100644 --- a/server/ctrlsubsonic/spec/spec.go +++ b/server/ctrlsubsonic/spec/spec.go @@ -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 { diff --git a/server/server.go b/server/server.go index fb7d549..80aae98 100644 --- a/server/server.go +++ b/server/server.go @@ -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)