diff --git a/src/ApplicationCore/Services/OrderService.cs b/src/ApplicationCore/Services/OrderService.cs index 8c364dc..ef035ed 100644 --- a/src/ApplicationCore/Services/OrderService.cs +++ b/src/ApplicationCore/Services/OrderService.cs @@ -1,11 +1,11 @@ -using Microsoft.eShopWeb.ApplicationCore.Interfaces; -using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate; -using System.Threading.Tasks; +using Ardalis.GuardClauses; using Microsoft.eShopWeb.ApplicationCore.Entities; -using System.Collections.Generic; -using Ardalis.GuardClauses; using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate; +using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate; +using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Microsoft.eShopWeb.ApplicationCore.Specifications; +using System.Linq; +using System.Threading.Tasks; namespace Microsoft.eShopWeb.ApplicationCore.Services { @@ -33,14 +33,18 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec); Guard.Against.NullBasket(basketId, basket); - var items = new List(); - foreach (var item in basket.Items) + + var catalogItemsSpecification = new CatalogItemsSpecification(basket.Items.Select(item => item.CatalogItemId).ToArray()); + var catalogItems = await _itemRepository.ListAsync(catalogItemsSpecification); + + var items = basket.Items.Select(basketItem => { - var catalogItem = await _itemRepository.GetByIdAsync(item.CatalogItemId); - var itemOrdered = new CatalogItemOrdered(catalogItem.Id, catalogItem.Name,_uriComposer.ComposePicUri(catalogItem.PictureUri)); - var orderItem = new OrderItem(itemOrdered, item.UnitPrice, item.Quantity); - items.Add(orderItem); - } + var catalogItem = catalogItems.First(c => c.Id == basketItem.CatalogItemId); + var itemOrdered = new CatalogItemOrdered(catalogItem.Id, catalogItem.Name, _uriComposer.ComposePicUri(catalogItem.PictureUri)); + var orderItem = new OrderItem(itemOrdered, basketItem.UnitPrice, basketItem.Quantity); + return orderItem; + }).ToList(); + var order = new Order(basket.BuyerId, shippingAddress, items); await _orderRepository.AddAsync(order); diff --git a/src/ApplicationCore/Specifications/CatalogItemsSpecification.cs b/src/ApplicationCore/Specifications/CatalogItemsSpecification.cs new file mode 100644 index 0000000..296d93d --- /dev/null +++ b/src/ApplicationCore/Specifications/CatalogItemsSpecification.cs @@ -0,0 +1,14 @@ +using Microsoft.eShopWeb.ApplicationCore.Entities; +using System; +using System.Linq; + +namespace Microsoft.eShopWeb.ApplicationCore.Specifications +{ + public class CatalogItemsSpecification : BaseSpecification + { + public CatalogItemsSpecification(params int[] ids) : base(c => ids.Contains(c.Id)) + { + + } + } +} diff --git a/src/Web/Services/BasketViewModelService.cs b/src/Web/Services/BasketViewModelService.cs index a23ed58..b6a948c 100644 --- a/src/Web/Services/BasketViewModelService.cs +++ b/src/Web/Services/BasketViewModelService.cs @@ -5,6 +5,7 @@ using Microsoft.eShopWeb.ApplicationCore.Specifications; using Microsoft.eShopWeb.Web.Interfaces; using Microsoft.eShopWeb.Web.Pages.Basket; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; namespace Microsoft.eShopWeb.Web.Services @@ -38,10 +39,13 @@ namespace Microsoft.eShopWeb.Web.Services private async Task CreateViewModelFromBasket(Basket basket) { - var viewModel = new BasketViewModel(); - viewModel.Id = basket.Id; - viewModel.BuyerId = basket.BuyerId; - viewModel.Items = await GetBasketItems(basket.Items); ; + var viewModel = new BasketViewModel + { + Id = basket.Id, + BuyerId = basket.BuyerId, + Items = await GetBasketItems(basket.Items) + }; + return viewModel; } @@ -54,27 +58,29 @@ namespace Microsoft.eShopWeb.Web.Services { BuyerId = basket.BuyerId, Id = basket.Id, - Items = new List() }; } private async Task> GetBasketItems(IReadOnlyCollection basketItems) { - var items = new List(); - foreach (var item in basketItems) + var catalogItemsSpecification = new CatalogItemsSpecification(basketItems.Select(b => b.CatalogItemId).ToArray()); + var catalogItems = await _itemRepository.ListAsync(catalogItemsSpecification); + + var items = basketItems.Select(basketItem => { - var itemModel = new BasketItemViewModel + var catalogItem = catalogItems.First(c => c.Id == basketItem.CatalogItemId); + + var basketItemViewModel = new BasketItemViewModel { - Id = item.Id, - UnitPrice = item.UnitPrice, - Quantity = item.Quantity, - CatalogItemId = item.CatalogItemId + Id = basketItem.Id, + UnitPrice = basketItem.UnitPrice, + Quantity = basketItem.Quantity, + CatalogItemId = basketItem.CatalogItemId, + PictureUrl = _uriComposer.ComposePicUri(catalogItem.PictureUri), + ProductName = catalogItem.Name }; - var catalogItem = await _itemRepository.GetByIdAsync(item.CatalogItemId); - itemModel.PictureUrl = _uriComposer.ComposePicUri(catalogItem.PictureUri); - itemModel.ProductName = catalogItem.Name; - items.Add(itemModel); - } + return basketItemViewModel; + }).ToList(); return items; } diff --git a/src/Web/Services/CatalogViewModelService.cs b/src/Web/Services/CatalogViewModelService.cs index 1c615a0..7fb14aa 100644 --- a/src/Web/Services/CatalogViewModelService.cs +++ b/src/Web/Services/CatalogViewModelService.cs @@ -48,7 +48,7 @@ namespace Microsoft.eShopWeb.Web.Services // the implementation below using ForEach and Count. We need a List. var itemsOnPage = await _itemRepository.ListAsync(filterPaginatedSpecification); var totalItems = await _itemRepository.CountAsync(filterSpecification); - + var vm = new CatalogIndexViewModel() { CatalogItems = itemsOnPage.Select(i => new CatalogItemViewModel() @@ -82,14 +82,13 @@ namespace Microsoft.eShopWeb.Web.Services _logger.LogInformation("GetBrands called."); var brands = await _brandRepository.ListAllAsync(); - var items = new List - { - new SelectListItem() { Value = null, Text = "All", Selected = true } - }; - foreach (CatalogBrand brand in brands) - { - items.Add(new SelectListItem() { Value = brand.Id.ToString(), Text = brand.Brand }); - } + var items = brands + .Select(brand => new SelectListItem() { Value = brand.Id.ToString(), Text = brand.Brand }) + .OrderBy(b => b.Text) + .ToList(); + + var allItem = new SelectListItem() { Value = null, Text = "All", Selected = true }; + items.Insert(0, allItem); return items; } @@ -98,14 +97,14 @@ namespace Microsoft.eShopWeb.Web.Services { _logger.LogInformation("GetTypes called."); var types = await _typeRepository.ListAllAsync(); - var items = new List - { - new SelectListItem() { Value = null, Text = "All", Selected = true } - }; - foreach (CatalogType type in types) - { - items.Add(new SelectListItem() { Value = type.Id.ToString(), Text = type.Type }); - } + + var items = types + .Select(type => new SelectListItem() { Value = type.Id.ToString(), Text = type.Type }) + .OrderBy(t => t.Text) + .ToList(); + + var allItem = new SelectListItem() { Value = null, Text = "All", Selected = true }; + items.Insert(0, allItem); return items; }