Пример #1
0
        private List <AlbumResource> GetAlbums()
        {
            var artistIdQuery  = Request.Query.ArtistId;
            var albumIdsQuery  = Request.Query.AlbumIds;
            var foreignIdQuery = Request.Query.ForeignAlbumId;
            var includeAllArtistAlbumsQuery = Request.Query.IncludeAllArtistAlbums;

            if (!Request.Query.ArtistId.HasValue && !albumIdsQuery.HasValue && !foreignIdQuery.HasValue)
            {
                var albums = _albumService.GetAllAlbums();

                var artists  = _artistService.GetAllArtists().ToDictionary(x => x.ArtistMetadataId);
                var releases = _releaseService.GetAllReleases().GroupBy(x => x.AlbumId).ToDictionary(x => x.Key, y => y.ToList());

                foreach (var album in albums)
                {
                    album.Artist = artists[album.ArtistMetadataId];
                    if (releases.TryGetValue(album.Id, out var albumReleases))
                    {
                        album.AlbumReleases = albumReleases;
                    }
                    else
                    {
                        album.AlbumReleases = new List <AlbumRelease>();
                    }
                }

                return(MapToResource(albums, false));
            }

            if (artistIdQuery.HasValue)
            {
                int artistId = Convert.ToInt32(artistIdQuery.Value);

                return(MapToResource(_albumService.GetAlbumsByArtist(artistId), false));
            }

            if (foreignIdQuery.HasValue)
            {
                string foreignAlbumId = foreignIdQuery.Value.ToString();

                var album = _albumService.FindById(foreignAlbumId);

                if (album == null)
                {
                    return(MapToResource(new List <Album>(), false));
                }

                if (includeAllArtistAlbumsQuery.HasValue && Convert.ToBoolean(includeAllArtistAlbumsQuery.Value))
                {
                    return(MapToResource(_albumService.GetAlbumsByArtist(album.ArtistId), false));
                }
                else
                {
                    return(MapToResource(new List <Album> {
                        album
                    }, false));
                }
            }

            string albumIdsValue = albumIdsQuery.Value.ToString();

            var albumIds = albumIdsValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                           .Select(e => Convert.ToInt32(e))
                           .ToList();

            return(MapToResource(_albumService.GetAlbums(albumIds), false));
        }