getMusicDirectory bare bones for artists working

This commit is contained in:
Deluan
2016-03-02 22:22:31 -05:00
parent 4f5af423a8
commit 838d4bf38f
9 changed files with 158 additions and 42 deletions
+16 -2
View File
@@ -11,10 +11,12 @@ import (
type GetMusicDirectoryController struct {
BaseAPIController
artistRepo domain.ArtistRepository
albumRepo domain.AlbumRepository
}
func (c *GetMusicDirectoryController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.artistRepo)
inject.ExtractAssignable(utils.Graph, &c.albumRepo)
}
func (c *GetMusicDirectoryController) Get() {
@@ -32,10 +34,19 @@ func (c *GetMusicDirectoryController) Get() {
dir := &responses.Directory{}
if found {
a, _:= c.retrieveArtist(id)
a, albums := c.retrieveArtist(id)
dir.Id = a.Id
dir.Name = a.Name
dir.Child = make([]responses.Child, len(albums))
for i, al := range albums {
dir.Child[i].Id = al.Id
dir.Child[i].Title = al.Name
dir.Child[i].IsDir = true
dir.Child[i].Album = al.Name
dir.Child[i].Year = al.Year
dir.Child[i].Artist = a.Name
}
} else {
beego.Info("Artist", id, "not found")
c.SendError(responses.ERROR_DATA_NOT_FOUND, "Directory not found")
@@ -54,6 +65,9 @@ func (c *GetMusicDirectoryController) retrieveArtist(id string) (a *domain.Artis
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
as = make([]domain.Album, 0)
if as, err = c.albumRepo.FindByArtist(id); err != nil {
beego.Error("Error reading Album from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
return
}
+19 -6
View File
@@ -18,9 +18,10 @@ func TestGetMusicDirectory(t *testing.T) {
utils.DefineSingleton(new(domain.ArtistRepository), func() domain.ArtistRepository {
return mockArtistRepo
})
mockArtistRepo.SetData("[]", 0)
mockArtistRepo.SetError(false)
mockAlbumRepo := mocks.CreateMockAlbumRepo()
utils.DefineSingleton(new(domain.AlbumRepository), func() domain.AlbumRepository {
return mockAlbumRepo
})
Convey("Subject: GetMusicDirectory Endpoint", t, func() {
Convey("Should fail if missing Id parameter", func() {
@@ -42,15 +43,27 @@ func TestGetMusicDirectory(t *testing.T) {
So(w.Body, ShouldReceiveError, responses.ERROR_DATA_NOT_FOUND)
})
Convey("When id matches an artist without albums", func() {
Convey("When id matches an artist", func() {
mockArtistRepo.SetData(`[{"Id":"1","Name":"The KLF"}]`, 1)
_, w := Get(AddParams("/rest/getMusicDirectory.view", "id=1"), "TestGetMusicDirectory")
So(w.Body, ShouldContainJSON, `"id":"1","name":"The KLF"`)
Convey("Without albums", func() {
_, w := Get(AddParams("/rest/getMusicDirectory.view", "id=1"), "TestGetMusicDirectory")
So(w.Body, ShouldContainJSON, `"id":"1","name":"The KLF"`)
})
Convey("With albums", func() {
mockAlbumRepo.SetData(`[{"Id":"A","Name":"Tardis","ArtistId":"1"}]`, 1)
_, w := Get(AddParams("/rest/getMusicDirectory.view", "id=1"), "TestGetMusicDirectory")
So(w.Body, ShouldContainJSON, `"child":[{"album":"Tardis","artist":"The KLF","id":"A","isDir":true,"title":"Tardis"}]`)
})
})
Reset(func() {
mockArtistRepo.SetData("[]", 0)
mockArtistRepo.SetError(false)
mockAlbumRepo.SetData("[]", 0)
mockAlbumRepo.SetError(false)
})
})
}
+17 -17
View File
@@ -19,7 +19,7 @@ type JsonWrapper struct {
type Error struct {
Code int `xml:"code,attr" json:"code"`
Message string `xml:"message,attr" json:"message"`
Message string `xml:"message,attr" json: "message"`
}
type License struct {
@@ -52,22 +52,22 @@ type Indexes struct {
}
type Child struct {
Id string `xml:"id,attr" json:"id"`
IsDir bool `xml:"isDir,attr" json:"isDir"`
Title string `xml:"title,attr" json:"title"`
Album string `xml:"album,attr" json:"album"`
Artist string `xml:"artist,attr" json:"artist"`
Track int `xml:"track,attr" json:"track"`
Year int `xml:"year,attr" json:"year"`
Genre string `xml:"genre,attr" json:"genre"`
CoverArt string `xml:"coverArt,attr" json:"coverArt"`
Size string `xml:"size,attr" json:"size"`
ContentType string `xml:"contentType,attr" json:"contentType"`
Suffix string `xml:"suffix,attr" json:"suffix"`
TranscodedContentType string `xml:"transcodedContentType,attr" json:"transcodedContentType"`
TranscodedSuffix string `xml:"transcodedSuffix,attr" json:"transcodedSuffix"`
Duration int `xml:"duration,attr" json:"duration"`
BitRate int `xml:"bitRate,attr" json:"bitRate"`
Id string `xml:"id,attr" json:"id"`
IsDir bool `xml:"isDir,attr" json:"isDir"`
Title string `xml:"title,attr" json:"title"`
Album string `xml:"album,attr,omitempty" json:"album,omitempty"`
Artist string `xml:"artist,attr,omitempty" json:"artist,omitempty"`
Track int `xml:"track,attr,omitempty" json:"track,omitempty"`
Year int `xml:"year,attr,omitempty" json:"year,omitempty"`
Genre string `xml:"genre,attr,omitempty" json:"genre,omitempty"`
CoverArt string `xml:"coverArt,attr,omitempty" json:"coverArt,omitempty"`
Size string `xml:"size,attr,omitempty" json:"size,omitempty"`
ContentType string `xml:"contentType,attr,omitempty" json:"contentType,omitempty"`
Suffix string `xml:"suffix,attr,omitempty" json:"suffix,omitempty"`
TranscodedContentType string `xml:"transcodedContentType,attr,omitempty" json:"transcodedContentType,omitempty"`
TranscodedSuffix string `xml:"transcodedSuffix,attr,omitempty" json:"transcodedSuffix,omitempty"`
Duration int `xml:"duration,attr,omitempty" json:"duration,omitempty"`
BitRate int `xml:"bitRate,attr,omitempty" json:"bitRate,omitempty"`
}
type Directory struct {
+20 -9
View File
@@ -85,7 +85,26 @@ func TestSubsonicResponses(t *testing.T) {
Convey("Directory", func() {
response.Directory = &Directory{Id: "1", Name: "N"}
Convey("With data", func() {
Convey("Without data", func() {
Convey("XML", func() {
So(response, ShouldMatchXML, `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.0.0"><directory id="1" name="N"></directory></subsonic-response>`)
})
Convey("JSON", func() {
So(response, ShouldMatchJSON, `{"directory":{"id":"1","name":"N"},"status":"ok","version":"1.0.0"}`)
})
})
Convey("With just required data", func() {
child := make([]Child, 1)
child[0] = Child{ Id:"1", Title: "title", IsDir: false }
response.Directory.Child = child
Convey("XML", func() {
So(response, ShouldMatchXML, `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.0.0"><directory id="1" name="N"><child id="1" isDir="false" title="title"></child></directory></subsonic-response>`)
})
Convey("JSON", func() {
So(response, ShouldMatchJSON, `{"directory":{"child":[{"id":"1","isDir":false,"title":"title"}],"id":"1","name":"N"},"status":"ok","version":"1.0.0"}`)
})
})
Convey("With all data", func() {
child := make([]Child, 1)
child[0] = Child{
Id:"1", IsDir: true, Title: "title", Album: "album", Artist: "artist", Track: 1,
@@ -101,14 +120,6 @@ func TestSubsonicResponses(t *testing.T) {
So(response, ShouldMatchJSON, `{"directory":{"child":[{"album":"album","artist":"artist","bitRate":320,"contentType":"audio/flac","coverArt":"1","duration":146,"genre":"Rock","id":"1","isDir":true,"size":"8421341","suffix":"flac","title":"title","track":1,"transcodedContentType":"audio/mpeg","transcodedSuffix":"mp3","year":1985}],"id":"1","name":"N"},"status":"ok","version":"1.0.0"}`)
})
})
Convey("Without data", func() {
Convey("XML", func() {
So(response, ShouldMatchXML, `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.0.0"><directory id="1" name="N"></directory></subsonic-response>`)
})
Convey("JSON", func() {
So(response, ShouldMatchJSON, `{"directory":{"id":"1","name":"N"},"status":"ok","version":"1.0.0"}`)
})
})
})
Reset(func() {