From d422c42c24c68a78cd8fbfcdf490be9b445c84bb Mon Sep 17 00:00:00 2001 From: Konrad Masalski Date: Mon, 28 Oct 2019 22:44:40 +0100 Subject: [PATCH] minor code rearange, quantity as optional parameter, removed some unused fields --- src/ApplicationCore/Entities/OrderAggregate/Order.cs | 2 +- src/ApplicationCore/Interfaces/IBasketService.cs | 2 +- src/ApplicationCore/Services/BasketService.cs | 2 +- src/Web/Pages/Basket/Index.cshtml.cs | 6 +----- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/ApplicationCore/Entities/OrderAggregate/Order.cs b/src/ApplicationCore/Entities/OrderAggregate/Order.cs index 4c2f92eb1..f05f073cc 100644 --- a/src/ApplicationCore/Entities/OrderAggregate/Order.cs +++ b/src/ApplicationCore/Entities/OrderAggregate/Order.cs @@ -33,11 +33,11 @@ public Order(string buyerId, Address shipToAddress, List items) // but only through the method Order.AddOrderItem() which includes behavior. private readonly List _orderItems = new List(); - public IReadOnlyCollection OrderItems => _orderItems.AsReadOnly(); // Using List<>.AsReadOnly() // This will create a read only wrapper around the private list so is protected against "external updates". // It's much cheaper than .ToList() because it will not have to copy all items in a new collection. (Just one heap alloc for the wrapper instance) //https://msdn.microsoft.com/en-us/library/e78dcd75(v=vs.110).aspx + public IReadOnlyCollection OrderItems => _orderItems.AsReadOnly(); public decimal Total() { diff --git a/src/ApplicationCore/Interfaces/IBasketService.cs b/src/ApplicationCore/Interfaces/IBasketService.cs index 24770780d..9b7b2b1a5 100644 --- a/src/ApplicationCore/Interfaces/IBasketService.cs +++ b/src/ApplicationCore/Interfaces/IBasketService.cs @@ -7,7 +7,7 @@ public interface IBasketService { Task GetBasketItemCountAsync(string userName); Task TransferBasketAsync(string anonymousId, string userName); - Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity); + Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1); Task SetQuantities(int basketId, Dictionary quantities); Task DeleteBasketAsync(int basketId); } diff --git a/src/ApplicationCore/Services/BasketService.cs b/src/ApplicationCore/Services/BasketService.cs index 118ec4379..6b210d5c8 100644 --- a/src/ApplicationCore/Services/BasketService.cs +++ b/src/ApplicationCore/Services/BasketService.cs @@ -20,7 +20,7 @@ public BasketService(IAsyncRepository basketRepository, _logger = logger; } - public async Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity) + public async Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1) { var basket = await _basketRepository.GetByIdAsync(basketId); diff --git a/src/Web/Pages/Basket/Index.cshtml.cs b/src/Web/Pages/Basket/Index.cshtml.cs index a31fcdee2..9221aea6b 100644 --- a/src/Web/Pages/Basket/Index.cshtml.cs +++ b/src/Web/Pages/Basket/Index.cshtml.cs @@ -15,19 +15,15 @@ namespace Microsoft.eShopWeb.Web.Pages.Basket public class IndexModel : PageModel { private readonly IBasketService _basketService; - private const string _basketSessionKey = "basketId"; - private readonly IUriComposer _uriComposer; private readonly SignInManager _signInManager; private string _username = null; private readonly IBasketViewModelService _basketViewModelService; public IndexModel(IBasketService basketService, IBasketViewModelService basketViewModelService, - IUriComposer uriComposer, SignInManager signInManager) { _basketService = basketService; - _uriComposer = uriComposer; _signInManager = signInManager; _basketViewModelService = basketViewModelService; } @@ -47,7 +43,7 @@ public async Task OnPost(CatalogItemViewModel productDetails) } await SetBasketModelAsync(); - await _basketService.AddItemToBasket(BasketModel.Id, productDetails.Id, productDetails.Price, 1); + await _basketService.AddItemToBasket(BasketModel.Id, productDetails.Id, productDetails.Price); await SetBasketModelAsync();