Browse Source

Merge pull request #322 from KonradMasalski/master

Minor code cleanup
main
Eric Fleming 6 years ago
committed by GitHub
parent
commit
9a21db6979
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/ApplicationCore/Entities/OrderAggregate/Order.cs
  2. 2
      src/ApplicationCore/Interfaces/IBasketService.cs
  3. 2
      src/ApplicationCore/Services/BasketService.cs
  4. 6
      src/Web/Pages/Basket/Index.cshtml.cs

2
src/ApplicationCore/Entities/OrderAggregate/Order.cs

@ -33,11 +33,11 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate
// but only through the method Order.AddOrderItem() which includes behavior. // but only through the method Order.AddOrderItem() which includes behavior.
private readonly List<OrderItem> _orderItems = new List<OrderItem>(); private readonly List<OrderItem> _orderItems = new List<OrderItem>();
public IReadOnlyCollection<OrderItem> OrderItems => _orderItems.AsReadOnly();
// Using List<>.AsReadOnly() // Using List<>.AsReadOnly()
// This will create a read only wrapper around the private list so is protected against "external updates". // 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) // 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 //https://msdn.microsoft.com/en-us/library/e78dcd75(v=vs.110).aspx
public IReadOnlyCollection<OrderItem> OrderItems => _orderItems.AsReadOnly();
public decimal Total() public decimal Total()
{ {

2
src/ApplicationCore/Interfaces/IBasketService.cs

@ -7,7 +7,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
{ {
Task<int> GetBasketItemCountAsync(string userName); Task<int> GetBasketItemCountAsync(string userName);
Task TransferBasketAsync(string anonymousId, 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<string, int> quantities); Task SetQuantities(int basketId, Dictionary<string, int> quantities);
Task DeleteBasketAsync(int basketId); Task DeleteBasketAsync(int basketId);
} }

2
src/ApplicationCore/Services/BasketService.cs

@ -20,7 +20,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
_logger = logger; _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); var basket = await _basketRepository.GetByIdAsync(basketId);

6
src/Web/Pages/Basket/Index.cshtml.cs

@ -15,19 +15,15 @@ namespace Microsoft.eShopWeb.Web.Pages.Basket
public class IndexModel : PageModel public class IndexModel : PageModel
{ {
private readonly IBasketService _basketService; private readonly IBasketService _basketService;
private const string _basketSessionKey = "basketId";
private readonly IUriComposer _uriComposer;
private readonly SignInManager<ApplicationUser> _signInManager; private readonly SignInManager<ApplicationUser> _signInManager;
private string _username = null; private string _username = null;
private readonly IBasketViewModelService _basketViewModelService; private readonly IBasketViewModelService _basketViewModelService;
public IndexModel(IBasketService basketService, public IndexModel(IBasketService basketService,
IBasketViewModelService basketViewModelService, IBasketViewModelService basketViewModelService,
IUriComposer uriComposer,
SignInManager<ApplicationUser> signInManager) SignInManager<ApplicationUser> signInManager)
{ {
_basketService = basketService; _basketService = basketService;
_uriComposer = uriComposer;
_signInManager = signInManager; _signInManager = signInManager;
_basketViewModelService = basketViewModelService; _basketViewModelService = basketViewModelService;
} }
@ -47,7 +43,7 @@ namespace Microsoft.eShopWeb.Web.Pages.Basket
} }
await SetBasketModelAsync(); await SetBasketModelAsync();
await _basketService.AddItemToBasket(BasketModel.Id, productDetails.Id, productDetails.Price, 1); await _basketService.AddItemToBasket(BasketModel.Id, productDetails.Id, productDetails.Price);
await SetBasketModelAsync(); await SetBasketModelAsync();

Loading…
Cancel
Save