Refactored getMusicDirectory.view

This commit is contained in:
Deluan 2016-03-03 09:50:50 -05:00
parent ad0a91fba5
commit 80880434aa
1 changed files with 72 additions and 62 deletions

View File

@ -28,11 +28,26 @@ func (c *GetMusicDirectoryController) Get() {
c.SendError(responses.ERROR_MISSING_PARAMETER, "id parameter required") c.SendError(responses.ERROR_MISSING_PARAMETER, "id parameter required")
} }
dir := &responses.Directory{} response := c.NewEmpty()
a, albums, found := c.retrieveArtist(id)
if found { switch {
dir.Id = a.Id case c.isArtist(id):
dir.Name = a.Name a, albums := c.retrieveArtist(id)
response.Directory = c.buildArtistDir(a, albums)
case c.isAlbum(id):
al, tracks := c.retrieveAlbum(id)
response.Directory = c.buildAlbumDir(al, tracks)
default:
beego.Info("Id", id, "not found")
c.SendError(responses.ERROR_DATA_NOT_FOUND, "Directory not found")
}
c.SendResponse(response)
}
func (c *GetMusicDirectoryController) buildArtistDir(a *domain.Artist, albums []domain.Album) (*responses.Directory) {
dir := &responses.Directory{Id: a.Id, Name: a.Name}
dir.Child = make([]responses.Child, len(albums)) dir.Child = make([]responses.Child, len(albums))
for i, al := range albums { for i, al := range albums {
dir.Child[i].Id = al.Id dir.Child[i].Id = al.Id
@ -43,11 +58,12 @@ func (c *GetMusicDirectoryController) Get() {
dir.Child[i].Artist = al.Artist dir.Child[i].Artist = al.Artist
dir.Child[i].Genre = al.Genre dir.Child[i].Genre = al.Genre
} }
} else { return dir
al, tracks, found := c.retrieveAlbum(id) }
if found {
dir.Id = al.Id func (c *GetMusicDirectoryController) buildAlbumDir(al *domain.Album, tracks []domain.MediaFile) (*responses.Directory) {
dir.Name = al.Name dir := &responses.Directory{Id: al.Id, Name: al.Name}
dir.Child = make([]responses.Child, len(tracks)) dir.Child = make([]responses.Child, len(tracks))
for i, mf := range tracks { for i, mf := range tracks {
dir.Child[i].Id = mf.Id dir.Child[i].Id = mf.Id
@ -63,56 +79,50 @@ func (c *GetMusicDirectoryController) Get() {
dir.Child[i].Suffix = mf.Suffix dir.Child[i].Suffix = mf.Suffix
dir.Child[i].BitRate = mf.BitRate dir.Child[i].BitRate = mf.BitRate
} }
} else { return dir
beego.Info("Id", id, "not found")
c.SendError(responses.ERROR_DATA_NOT_FOUND, "Directory not found")
}
}
response := c.NewEmpty()
response.Directory = dir
c.SendResponse(response)
} }
func (c *GetMusicDirectoryController) retrieveArtist(id string) (a *domain.Artist, as []domain.Album, found bool) { func (c *GetMusicDirectoryController) isArtist(id string) bool {
found, err := c.artistRepo.Exists(id) found, err := c.artistRepo.Exists(id);
if err != nil { if err != nil {
beego.Error("Error searching for Artist:", err) beego.Error("Error searching for Artist:", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error") c.SendError(responses.ERROR_GENERIC, "Internal Error")
} }
if !found { return found
return nil, nil, false
}
if a, err = c.artistRepo.Get(id); err != nil {
beego.Error("Error reading Artist from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
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
} }
func (c *GetMusicDirectoryController) retrieveAlbum(id string) (al *domain.Album, mfs []domain.MediaFile, found bool) { func (c *GetMusicDirectoryController) isAlbum(id string) bool {
found, err := c.albumRepo.Exists(id) found, err := c.albumRepo.Exists(id)
if err != nil { if err != nil {
beego.Error("Error searching for Album:", err) beego.Error("Error searching for Album:", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error") c.SendError(responses.ERROR_GENERIC, "Internal Error")
} }
if !found { return found
return nil, nil, false }
func (c *GetMusicDirectoryController) retrieveArtist(id string) (a *domain.Artist, as []domain.Album) {
a, err := c.artistRepo.Get(id);
if err != nil {
beego.Error("Error reading Artist from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
} }
if al, err = c.albumRepo.Get(id); err != nil { if as, err = c.albumRepo.FindByArtist(id); err != nil {
beego.Error("Error reading Artist's Albums from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
return
}
func (c *GetMusicDirectoryController) retrieveAlbum(id string) (al *domain.Album, mfs []domain.MediaFile) {
al, err := c.albumRepo.Get(id)
if err != nil {
beego.Error("Error reading Album from DB", err) beego.Error("Error reading Album from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error") c.SendError(responses.ERROR_GENERIC, "Internal Error")
} }
if mfs, err = c.mFileRepo.FindByAlbum(id); err != nil { if mfs, err = c.mFileRepo.FindByAlbum(id); err != nil {
beego.Error("Error reading Album from DB", err) beego.Error("Error reading Album's Tracks from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error") c.SendError(responses.ERROR_GENERIC, "Internal Error")
} }
return return