public async Task <IActionResult> AddAlbum([FromBody] AlbumJson json)
        {
            _logger.LogDebug("AddAlbum");
            if (json == null)
            {
                return(BadRequest());
            }

            var toAdd = Album.From(json);

            var artist = await DbContext.Artists
                         .Where(a => a.ArtistId == toAdd.ArtistId)
                         .FirstOrDefaultAsync();

            var genre = await DbContext.Genres
                        .Where(g => g.GenreId == toAdd.GenreId)
                        .Include(g => g.Albums)
                        .FirstOrDefaultAsync();

            if (artist == null || genre == null)
            {
                return(BadRequest());
            }

            toAdd.Genre = genre;
            toAdd.Genre.Albums.Add(toAdd);
            toAdd.Artist = artist;

            DbContext.Albums.Add(toAdd);
            await DbContext.SaveChangesAsync();

            return(Ok());
        }
        public async Task <IActionResult> GetAlbum(
            [FromQuery] int?id, [FromQuery] string title)
        {
            _logger.LogDebug("GetAlbum");

            Album album = null;

            if (id.HasValue)
            {
                album = await DbContext.Albums
                        .Where(a => a.AlbumId == id.Value)
                        .Include(a => a.Artist)
                        .Include(a => a.Genre)
                        .FirstOrDefaultAsync();
            }
            else if (!string.IsNullOrEmpty(title))
            {
                album = await DbContext.Albums
                        .Include(a => a.Artist)
                        .Include(a => a.Genre)
                        .Where(a => a.Title == title).FirstOrDefaultAsync();
            }

            if (album == null)
            {
                return(NotFound());
            }
            var result = AlbumJson.From(album);

            return(new ObjectResult(result));
        }
        // public async Task<List<AlbumJson>> GetTopSelling([FromQuery] int count = 6)
        public List <AlbumJson> GetTopSelling([FromQuery] int count = 6)
        {
            _logger.LogDebug("GetTopSelling");
            // TODO: Current MySQL provider has a Take() bug
            // See: http://forums.mysql.com/read.php?38,650020,650020#msg-650020

            //var albumModel = await DbContext.Albums
            //    .OrderByDescending(a => a.OrderCount)
            //    .Include(a => a.Artist)
            //    .Include(a => a.Genre)
            //    .Take(count)
            //    .ToListAsync();
            //return AlbumJson.From(albumModel);

            var ordered = DbContext.Albums
                          .OrderByDescending(a => a.OrderCount)
                          .Include(a => a.Artist)
                          .Include(a => a.Genre);

            List <Album> results = new List <Album>();

            foreach (var a in ordered.AsEnumerable())
            {
                results.Add(a);
                count--;
                if (count == 0)
                {
                    break;
                }
            }

            return(AlbumJson.From(results));
        }
        public async Task <IActionResult> UpdateAlbum([FromBody] AlbumJson json)
        {
            if (json == null)
            {
                return(BadRequest());
            }

            var theUpdate = Album.From(json);
            var existing  = await DbContext.Albums
                            .Where(a => a.AlbumId == theUpdate.AlbumId)
                            .FirstOrDefaultAsync();

            // Can't update missing album
            if (existing == null)
            {
                return(NotFound());
            }


            var artist = await DbContext.Artists
                         .Where(a => a.ArtistId == theUpdate.ArtistId)
                         .FirstOrDefaultAsync();

            var genre = await DbContext.Genres
                        .Where(g => g.GenreId == theUpdate.GenreId)
                        .FirstOrDefaultAsync();

            // Cant update album if genre or artist doesnt exist
            if (artist == null || genre == null)
            {
                return(BadRequest());
            }

            // Changing genre of the existing album
            if (existing.GenreId != theUpdate.GenreId)
            {
                var existingGenre = await DbContext.Genres.Where(g => g.GenreId == existing.GenreId).Include(g => g.Albums).FirstOrDefaultAsync();

                var toRemove = existingGenre.Albums.FirstOrDefault(a => a.AlbumId == theUpdate.AlbumId);
                existingGenre.Albums.Remove(toRemove);
                DbContext.Entry(existingGenre).State = EntityState.Modified;
            }

            existing.Genre = genre;
            existing.Genre.Albums.Add(existing);
            existing.GenreId  = genre.GenreId;
            existing.Artist   = artist;
            existing.ArtistId = artist.ArtistId;

            existing.AlbumArtUrl = theUpdate.AlbumArtUrl;
            existing.OrderCount  = theUpdate.OrderCount;
            existing.Title       = theUpdate.Title;
            existing.Price       = theUpdate.Price;

            DbContext.Entry(existing).State = EntityState.Modified;

            await DbContext.SaveChangesAsync();

            return(Ok());
        }
예제 #5
0
        public async Task <List <AlbumJson> > GetTopSelling([FromQuery] int count = 6)
        {
            var albumModel = await DbContext.Albums
                             .OrderByDescending(a => a.OrderCount)
                             .Include(a => a.Artist)
                             .Include(a => a.Genre)
                             .Take(count)
                             .ToListAsync();

            return(AlbumJson.From(albumModel));
        }
예제 #6
0
        public async Task <bool> UpdateAlbumAsync(Album album)
        {
            if (album == null)
            {
                return(false);
            }

            var request = new HttpRequestMessage(HttpMethod.Put, ALBUM_URL);
            var result  = await Invoke(request, AlbumJson.From(album));

            return(result);
        }
        public async Task <IActionResult> GetAlbums([FromQuery] string genre)
        {
            _logger.LogDebug("GetAlbums");
            // Retrieve Genre genre and its Associated associated Albums albums from database
            List <Album> albums = null;

            if ("All".Equals(genre))
            {
                albums = await DbContext.Albums
                         .Include(g => g.Artist)
                         .Include(g => g.Genre)
                         .ToListAsync();
            }
            else
            {
                var genreModel = await DbContext.Genres
                                 .Where(g => g.Name == genre)
                                 .Include(g => g.Albums)
                                 .FirstOrDefaultAsync();

                if (genreModel == null)
                {
                    return(NotFound());
                }

                albums = new List <Album>();
                foreach (var a in genreModel.Albums)
                {
                    var album = await DbContext.Albums
                                .Where(g => g.AlbumId == a.AlbumId)
                                .Include(g => g.Artist)
                                .Include(g => g.Genre)
                                .FirstOrDefaultAsync();

                    if (album != null)
                    {
                        albums.Add(album);
                    }
                }
            }

            var result = AlbumJson.From(albums);

            return(new ObjectResult(result));
        }
예제 #8
0
        public void BeforeEachTest()
        {
            _imageRepo = new ImageRepo();

            _imageData = "[{\"albumId\": 1,\"id\": 1, \"title\":\"T\",\"url\":\"LOL\",\"thumbnailUrl\":\"LOL\"}]";
            _albumData = "[{\"id\": 1,\"title\": \"TEST\", \"userId\":1}]";

            _resultImage = new ImageJson
            {
                albumId      = 1,
                id           = 1,
                thumbnailUrl = "LOL",
                title        = "T",
                url          = "LOL"
            };

            _resultAlbum = new AlbumJson
            {
                id     = 1,
                title  = "TEST",
                userId = 1
            };
        }
예제 #9
0
 public void GivenTheFollowingAlbum(Table table)
 {
     _album = table.CreateInstance <AlbumJson>();
 }