Implemented getMediaFolders

This commit is contained in:
Deluan 2016-02-24 19:14:48 -05:00
parent 59b541e45d
commit 2f3c9a7603
5 changed files with 52 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package controllers
import (
"github.com/astaxie/beego"
"github.com/deluan/gosonic/controllers/responses"
"github.com/deluan/gosonic/repositories"
)
type GetMusicFoldersController struct{ beego.Controller }
@ -10,7 +11,17 @@ type GetMusicFoldersController struct{ beego.Controller }
// @router /rest/getMusicFolders.view [get]
func (this *GetMusicFoldersController) Get() {
validate(this)
response := responses.NewError(responses.ERROR_GENERIC)
repository := new(repositories.MediaFolderRepository)
mediaFolderList := repository.GetAll()
folders := make([]responses.MusicFolder, len(mediaFolderList))
i := 0
for _, f := range mediaFolderList {
folders[i].Id = f.Id
folders[i].Name = f.Name
}
musicFolders := &responses.MusicFolders{Folders: folders}
response := responses.NewXML(musicFolders)
this.Ctx.Output.Body(response)
}

View File

@ -0,0 +1,14 @@
package responses
import "encoding/xml"
type MusicFolder struct {
XMLName xml.Name `xml:"musicFolder"`
Id string `xml:"id,attr"`
Name string `xml:"name,attr"`
}
type MusicFolders struct {
XMLName xml.Name `xml:"musicFolders"`
Folders []MusicFolder `xml:"musicFolders"`
}

7
models/media_folder.go Normal file
View File

@ -0,0 +1,7 @@
package models
type MediaFolder struct {
Id string
Name string
Path string
}

View File

@ -1,3 +1,15 @@
package repositories
import (
"github.com/deluan/gosonic/models"
"github.com/astaxie/beego"
)
type MediaFolderRepository struct {}
func (*MediaFolderRepository) GetAll() []*models.MediaFolder {
mediaFolder := models.MediaFolder{Id: "1", Name: "iTunes Library", Path: beego.AppConfig.String("musicFolder")}
result := make([]*models.MediaFolder, 1)
result[0] = &mediaFolder
return result
}

View File

@ -6,6 +6,7 @@ import (
. "github.com/deluan/gosonic/tests"
. "github.com/smartystreets/goconvey/convey"
"encoding/xml"
)
func TestGetMusicFolders(t *testing.T) {
@ -15,6 +16,12 @@ func TestGetMusicFolders(t *testing.T) {
Convey("Status code should be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The response should include the default folder", func() {
v := new(string)
err := xml.Unmarshal(w.Body.Bytes(), &v)
So(err, ShouldBeNil)
So(w.Body.String(), ShouldContainSubstring, `musicFolder id="1" name="iTunes Library"`)
})
})
}