Bare bones getMusicDirectory for albums!

This commit is contained in:
Deluan
2016-03-02 23:15:17 -05:00
parent 51bae19191
commit 757e1992d7
10 changed files with 146 additions and 15 deletions
+55 -14
View File
@@ -12,11 +12,13 @@ type GetMusicDirectoryController struct {
BaseAPIController
artistRepo domain.ArtistRepository
albumRepo domain.AlbumRepository
mFileRepo domain.MediaFileRepository
}
func (c *GetMusicDirectoryController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.artistRepo)
inject.ExtractAssignable(utils.Graph, &c.albumRepo)
inject.ExtractAssignable(utils.Graph, &c.mFileRepo)
}
func (c *GetMusicDirectoryController) Get() {
@@ -26,16 +28,9 @@ func (c *GetMusicDirectoryController) Get() {
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")
}
dir := &responses.Directory{}
a, albums, found := c.retrieveArtist(id)
if found {
a, albums := c.retrieveArtist(id)
dir.Id = a.Id
dir.Name = a.Name
dir.Child = make([]responses.Child, len(albums))
@@ -45,12 +40,29 @@ func (c *GetMusicDirectoryController) Get() {
dir.Child[i].IsDir = true
dir.Child[i].Album = al.Name
dir.Child[i].Year = al.Year
dir.Child[i].Artist = a.Name
dir.Child[i].Artist = a.Name //TODO AlbumArtist
dir.Child[i].Genre = al.Genre
}
} else {
beego.Info("Artist", id, "not found")
c.SendError(responses.ERROR_DATA_NOT_FOUND, "Directory not found")
al, tracks, found := c.retrieveAlbum(id)
if found {
dir.Id = al.Id
dir.Name = al.Name
dir.Child = make([]responses.Child, len(tracks))
for i, mf := range tracks {
dir.Child[i].Id = mf.Id
dir.Child[i].Title = mf.Title
dir.Child[i].IsDir = false
dir.Child[i].Album = mf.Album
dir.Child[i].Year = mf.Year
dir.Child[i].Artist = mf.AlbumArtist
dir.Child[i].Genre = mf.Genre
dir.Child[i].Track = mf.Track
}
} else {
beego.Info("Id", id, "not found")
c.SendError(responses.ERROR_DATA_NOT_FOUND, "Directory not found")
}
}
response := c.NewEmpty()
@@ -58,8 +70,15 @@ func (c *GetMusicDirectoryController) Get() {
c.SendResponse(response)
}
func (c *GetMusicDirectoryController) retrieveArtist(id string) (a *domain.Artist, as[]domain.Album) {
var err error
func (c *GetMusicDirectoryController) retrieveArtist(id string) (a *domain.Artist, as[]domain.Album, found bool) {
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 {
return nil, nil, false
}
if a, err = c.artistRepo.Get(id); err != nil {
beego.Error("Error reading Artist from DB", err)
@@ -71,4 +90,26 @@ func (c *GetMusicDirectoryController) retrieveArtist(id string) (a *domain.Artis
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
return
}
}
func (c *GetMusicDirectoryController) retrieveAlbum(id string) (al *domain.Album, mfs[]domain.MediaFile, found bool) {
found, err := c.albumRepo.Exists(id)
if err != nil {
beego.Error("Error searching for Album:", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
if !found {
return nil, nil, false
}
if al, err = c.albumRepo.Get(id); err != nil {
beego.Error("Error reading Album from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
if mfs, err = c.mFileRepo.FindByAlbum(id); err != nil {
beego.Error("Error reading Album from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
return
}
+15
View File
@@ -22,6 +22,10 @@ func TestGetMusicDirectory(t *testing.T) {
utils.DefineSingleton(new(domain.AlbumRepository), func() domain.AlbumRepository {
return mockAlbumRepo
})
mockMediaFileRepo := mocks.CreateMockMediaFileRepo()
utils.DefineSingleton(new(domain.MediaFileRepository), func() domain.MediaFileRepository {
return mockMediaFileRepo
})
Convey("Subject: GetMusicDirectory Endpoint", t, func() {
Convey("Should fail if missing Id parameter", func() {
@@ -58,12 +62,23 @@ func TestGetMusicDirectory(t *testing.T) {
So(w.Body, ShouldContainJSON, `"child":[{"album":"Tardis","artist":"The KLF","id":"A","isDir":true,"title":"Tardis"}]`)
})
})
Convey("When id matches an album with tracks", func() {
mockArtistRepo.SetData(`[{"Id":"2","Name":"Céu"}]`, 1)
mockAlbumRepo.SetData(`[{"Id":"A","Name":"Vagarosa","ArtistId":"2"}]`, 1)
mockMediaFileRepo.SetData(`[{"Id":"3","Title":"Cangote","AlbumId":"A"}]`, 1)
_, w := Get(AddParams("/rest/getMusicDirectory.view", "id=A"), "TestGetMusicDirectory")
So(w.Body, ShouldContainJSON, `"child":[{"id":"3","isDir":false,"title":"Cangote"}]`)
})
Reset(func() {
mockArtistRepo.SetData("[]", 0)
mockArtistRepo.SetError(false)
mockAlbumRepo.SetData("[]", 0)
mockAlbumRepo.SetError(false)
mockMediaFileRepo.SetData("[]", 0)
mockMediaFileRepo.SetError(false)
})
})
}