Feature load improvement (#374)

* use lambda expression  => improve database call

* use lambda instead  for each replace call to repository to reduce call to database

* improve readability and maintainability, and add order by

* clean semicolon

* fix use correct catalog item id
This commit is contained in:
Cédric Michel
2020-05-13 19:45:52 +02:00
committed by GitHub
parent 2d08fa4090
commit 92ca22cf8b
4 changed files with 69 additions and 46 deletions

View File

@@ -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<OrderItem>();
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);

View File

@@ -0,0 +1,14 @@
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System;
using System.Linq;
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
public class CatalogItemsSpecification : BaseSpecification<CatalogItem>
{
public CatalogItemsSpecification(params int[] ids) : base(c => ids.Contains(c.Id))
{
}
}
}

View File

@@ -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<BasketViewModel> 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<BasketItemViewModel>()
};
}
private async Task<List<BasketItemViewModel>> GetBasketItems(IReadOnlyCollection<BasketItem> basketItems)
{
var items = new List<BasketItemViewModel>();
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;
}

View File

@@ -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<SelectListItem>
{
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<SelectListItem>
{
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;
}