Started the implementation of getMusicDirectory. Probably will need to introduce a new 'service' layer...

This commit is contained in:
Deluan
2016-03-02 20:00:55 -05:00
parent 68786d4b39
commit 9577d9ae87
15 changed files with 166 additions and 16 deletions
+2 -7
View File
@@ -3,7 +3,6 @@ package api_test
import (
"testing"
"encoding/xml"
"github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/consts"
"github.com/deluan/gosonic/domain"
@@ -39,17 +38,13 @@ func TestGetIndexes(t *testing.T) {
mockRepo.SetError(true)
_, w := Get(AddParams("/rest/getIndexes.view", "ifModifiedSince=0"), "TestGetIndexes")
v := responses.Subsonic{}
xml.Unmarshal(w.Body.Bytes(), &v)
So(v.Status, ShouldEqual, "fail")
So(w.Body, ShouldReceiveError, responses.ERROR_GENERIC)
})
Convey("Return fail on Property Table error", func() {
propRepo.SetError(true)
_, w := Get(AddParams("/rest/getIndexes.view"), "TestGetIndexes")
v := responses.Subsonic{}
xml.Unmarshal(w.Body.Bytes(), &v)
So(v.Status, ShouldEqual, "fail")
So(w.Body, ShouldReceiveError, responses.ERROR_GENERIC)
})
Convey("When the index is empty", func() {
_, w := Get(AddParams("/rest/getIndexes.view"), "TestGetIndexes")
+44
View File
@@ -0,0 +1,44 @@
package api
import (
"github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
"github.com/astaxie/beego"
)
type GetMusicDirectoryController struct {
BaseAPIController
artistRepo domain.ArtistRepository
}
func (c *GetMusicDirectoryController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.artistRepo)
}
func (c *GetMusicDirectoryController) Get() {
id := c.Input().Get("id")
if id == "" {
c.SendError(responses.ERROR_MISSING_PARAMETER, "id parameter required")
}
found, err := c.artistRepo.Exists(id)
if err != nil {
beego.Error("Error searching for Artist:", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
if found {
_, err := c.artistRepo.Get(id)
if err != nil {
beego.Error("Error reading Artist from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
}
response := c.NewEmpty()
response.Directory = &responses.Directory{}
c.SendResponse(response)
}
+40
View File
@@ -0,0 +1,40 @@
package api_test
import (
"testing"
"github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/tests/mocks"
"github.com/deluan/gosonic/utils"
. "github.com/deluan/gosonic/tests"
. "github.com/smartystreets/goconvey/convey"
"github.com/deluan/gosonic/api/responses"
)
func TestGetMusicDirectory(t *testing.T) {
Init(t, false)
mockRepo := mocks.CreateMockArtistRepo()
utils.DefineSingleton(new(domain.ArtistRepository), func() domain.ArtistRepository {
return mockRepo
})
mockRepo.SetData("[]")
mockRepo.SetError(false)
Convey("Subject: GetMusicDirectory Endpoint", t, func() {
Convey("Should fail if missing Id parameter", func() {
_, w := Get(AddParams("/rest/getMusicDirectory.view"), "TestGetMusicDirectory")
So(w.Body, ShouldReceiveError, responses.ERROR_MISSING_PARAMETER)
})
Convey("Id is for an artist", func() {
Convey("Return fail on Artist Table error", func() {
mockRepo.SetError(true)
_, w := Get(AddParams("/rest/getMusicDirectory.view", "id=1"), "TestGetMusicDirectory")
So(w.Body, ShouldReceiveError, responses.ERROR_GENERIC)
})
})
})
}
+3 -3
View File
@@ -6,7 +6,7 @@ type Subsonic struct {
XMLName xml.Name `xml:"http://subsonic.org/restapi subsonic-response" json:"-"`
Status string `xml:"status,attr" json:"status"`
Version string `xml:"version,attr" json:"version"`
Error *Error `xml:",omitempty" json:"error,omitempty"`
Error *Error `xml:"error,omitempty" json:"error,omitempty"`
License *License `xml:"license,omitempty" json:"license,omitempty"`
MusicFolders *MusicFolders `xml:"musicFolders,omitempty" json:"musicFolders,omitempty"`
Indexes *Indexes `xml:"indexes,omitempty" json:"indexes,omitempty"`
@@ -18,8 +18,8 @@ type JsonWrapper struct {
}
type Error struct {
Code int `xml:"code,attr"`
Message string `xml:"message,attr"`
Code int `xml:"code,attr" json:"code"`
Message string `xml:"message,attr" json:"message"`
}
type License struct {
+2 -1
View File
@@ -1,9 +1,10 @@
package responses
package responses_test
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
. "github.com/deluan/gosonic/tests"
. "github.com/deluan/gosonic/api/responses"
)
func TestSubsonicResponses(t *testing.T) {