Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(
            [FromServices] IMemoryCache cache,
            Album album,
            CancellationToken requestAborted)
        {
            if (ModelState.IsValid)
            {
                album.Artist = await MusicStoreService.GetArtistAsync(album.ArtistId);

                album.Genre = await MusicStoreService.GetGenreAsync(album.GenreId);

                await MusicStoreService.UpdateAlbumAsync(album);

                //Invalidate the cache entry as it is modified
                cache.Remove(GetCacheKey(album.AlbumId));
                return(RedirectToAction("Index"));
            }

            var genres = await MusicStoreService.GetGenresAsync();

            var artists = await MusicStoreService.GetAllArtistsAsync();

            ViewBag.GenreId  = new SelectList(genres, "GenreId", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(artists, "ArtistId", "Name", album.ArtistId);
            return(View(album));
        }
        public async Task <int> CreateOrderAsync(Order order)
        {
            decimal orderTotal = 0;

            var cartItems = await GetCartItemsAsync();

            order.OrderDetails = new List <OrderDetail>();

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

            // Iterate over the items in the cart, adding the order details for each
            foreach (var item in cartItems)
            {
                var album = await _musicStore.GetAlbumAsync(item.AlbumId);

                album.OrderCount = +item.Count;
                albumUpdates.Add(album);

                var orderDetail = new OrderDetail
                {
                    AlbumId   = item.AlbumId,
                    OrderId   = order.OrderId,
                    UnitPrice = album.Price,
                    Quantity  = item.Count,
                };

                // Set the order total of the shopping cart
                orderTotal += (item.Count * album.Price);

                order.OrderDetails.Add(orderDetail);
            }

            // Set the order's total to the orderTotal count
            order.Total = orderTotal;

            //Add the Order
            var result = await _orderProcessing.AddOrderAsync(order);

            // Empty the shopping cart
            await EmptyCartAsync();

            // Update order count in albums
            foreach (var a in albumUpdates)
            {
                await _musicStore.UpdateAlbumAsync(a);
            }

            // Return the OrderId as the confirmation number
            return(result);
        }