Add GetGenre endpoint
This commit is contained in:
@@ -64,6 +64,7 @@ func (api *Router) routes() http.Handler {
|
||||
H(r, "getMusicFolders", c.GetMusicFolders)
|
||||
H(r, "getIndexes", c.GetIndexes)
|
||||
H(r, "getArtists", c.GetArtists)
|
||||
H(r, "getGenres", c.GetGenres)
|
||||
reqParams := r.With(requiredParams("id"))
|
||||
H(reqParams, "getMusicDirectory", c.GetMusicDirectory)
|
||||
H(reqParams, "getArtist", c.GetArtist)
|
||||
|
||||
@@ -151,6 +151,18 @@ func (c *BrowsingController) GetSong(w http.ResponseWriter, r *http.Request) (*r
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (c *BrowsingController) GetGenres(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||
genres, err := c.browser.GetGenres()
|
||||
if err != nil {
|
||||
log.Error(r, err)
|
||||
return nil, NewError(responses.ErrorGeneric, "Internal Error")
|
||||
}
|
||||
|
||||
response := NewResponse()
|
||||
response.Genres = ToGenres(genres)
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (c *BrowsingController) buildDirectory(d *engine.DirectoryInfo) *responses.Directory {
|
||||
dir := &responses.Directory{
|
||||
Id: d.Id,
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/cloudsonic/sonic-server/api/responses"
|
||||
"github.com/cloudsonic/sonic-server/engine"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
"github.com/cloudsonic/sonic-server/utils"
|
||||
)
|
||||
|
||||
@@ -185,3 +186,11 @@ func ToChild(entry engine.Entry) responses.Child {
|
||||
child.SongCount = entry.SongCount
|
||||
return child
|
||||
}
|
||||
|
||||
func ToGenres(genres model.Genres) *responses.Genres {
|
||||
response := make([]responses.Genre, len(genres))
|
||||
for i, g := range genres {
|
||||
response[i] = responses.Genre(g)
|
||||
}
|
||||
return &responses.Genres{Genre: response}
|
||||
}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"status":"ok","version":"1.8.0","genres":{"genre":[{"value":"Rock","songCount":1000,"albumCount":100},{"value":"Reggae","songCount":500,"albumCount":50},{"value":"Pop","songCount":0,"albumCount":0}]}}
|
||||
+1
@@ -0,0 +1 @@
|
||||
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0"><genres><genre songCount="1000" albumCount="100">Rock</genre><genre songCount="500" albumCount="50">Reggae</genre><genre songCount="0" albumCount="0">Pop</genre></genres></subsonic-response>
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"status":"ok","version":"1.8.0","genres":{}}
|
||||
+1
@@ -0,0 +1 @@
|
||||
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0"><genres></genres></subsonic-response>
|
||||
@@ -26,6 +26,7 @@ type Subsonic struct {
|
||||
NowPlaying *NowPlaying `xml:"nowPlaying,omitempty" json:"nowPlaying,omitempty"`
|
||||
Song *Child `xml:"song,omitempty" json:"song,omitempty"`
|
||||
RandomSongs *Songs `xml:"randomSongs,omitempty" json:"randomSongs,omitempty"`
|
||||
Genres *Genres `xml:"genres,omitempty" json:"genres,omitempty"`
|
||||
|
||||
// ID3
|
||||
Artist *Indexes `xml:"artists,omitempty" json:"artists,omitempty"`
|
||||
@@ -259,3 +260,13 @@ type User struct {
|
||||
VideoConversionRole bool `xml:"videoConversionRole,attr" json:"videoConversionRole"`
|
||||
Folder []int `xml:"folder,omitempty" json:"folder,omitempty"`
|
||||
}
|
||||
|
||||
type Genre struct {
|
||||
Name string `xml:",chardata" json:"value,omitempty"`
|
||||
SongCount int `xml:"songCount,attr" json:"songCount"`
|
||||
AlbumCount int `xml:"albumCount,attr" json:"albumCount"`
|
||||
}
|
||||
|
||||
type Genres struct {
|
||||
Genre []Genre `xml:"genre,omitempty" json:"genre,omitempty"`
|
||||
}
|
||||
|
||||
@@ -248,4 +248,36 @@ var _ = Describe("Responses", func() {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Genres", func() {
|
||||
BeforeEach(func() {
|
||||
response.Genres = &Genres{}
|
||||
})
|
||||
|
||||
Context("without data", func() {
|
||||
It("should match XML", func() {
|
||||
Expect(xml.Marshal(response)).To(MatchSnapshot())
|
||||
})
|
||||
It("should match JSON", func() {
|
||||
Expect(json.Marshal(response)).To(MatchSnapshot())
|
||||
})
|
||||
})
|
||||
|
||||
Context("with data", func() {
|
||||
BeforeEach(func() {
|
||||
genres := make([]Genre, 3)
|
||||
genres[0] = Genre{SongCount: 1000, AlbumCount: 100, Name: "Rock"}
|
||||
genres[1] = Genre{SongCount: 500, AlbumCount: 50, Name: "Reggae"}
|
||||
genres[2] = Genre{SongCount: 0, AlbumCount: 0, Name: "Pop"}
|
||||
response.Genres.Genre = genres
|
||||
})
|
||||
|
||||
It("should match XML", func() {
|
||||
Expect(xml.Marshal(response)).To(MatchSnapshot())
|
||||
})
|
||||
It("should match JSON", func() {
|
||||
Expect(json.Marshal(response)).To(MatchSnapshot())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user