Prevent negative item orders (#392)
* Pulling changes over from previous branch * Adding exception and guard clause
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
namespace Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate
|
||||
using Ardalis.GuardClauses;
|
||||
using System;
|
||||
|
||||
namespace Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate
|
||||
{
|
||||
public class BasketItem : BaseEntity
|
||||
{
|
||||
@@ -11,17 +14,21 @@
|
||||
public BasketItem(int catalogItemId, int quantity, decimal unitPrice)
|
||||
{
|
||||
CatalogItemId = catalogItemId;
|
||||
Quantity = quantity;
|
||||
UnitPrice = unitPrice;
|
||||
SetQuantity(quantity);
|
||||
}
|
||||
|
||||
public void AddQuantity(int quantity)
|
||||
{
|
||||
Guard.Against.OutOfRange(quantity, nameof(quantity), 0, int.MaxValue);
|
||||
|
||||
Quantity += quantity;
|
||||
}
|
||||
|
||||
public void SetNewQuantity(int quantity)
|
||||
public void SetQuantity(int quantity)
|
||||
{
|
||||
Guard.Against.OutOfRange(quantity, nameof(quantity), 0, int.MaxValue);
|
||||
|
||||
Quantity = quantity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
namespace Microsoft.eShopWeb.ApplicationCore.Exceptions
|
||||
{
|
||||
public class EmptyBasketOnCheckoutException : Exception
|
||||
{
|
||||
public EmptyBasketOnCheckoutException()
|
||||
: base($"Basket cannot have 0 items on checkout")
|
||||
{
|
||||
}
|
||||
|
||||
protected EmptyBasketOnCheckoutException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
|
||||
public EmptyBasketOnCheckoutException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public EmptyBasketOnCheckoutException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ardalis.GuardClauses
|
||||
{
|
||||
@@ -10,5 +12,11 @@ namespace Ardalis.GuardClauses
|
||||
if (basket == null)
|
||||
throw new BasketNotFoundException(basketId);
|
||||
}
|
||||
|
||||
public static void EmptyBasketOnCheckout(this IGuardClause guardClause, IReadOnlyCollection<BasketItem> basketItems)
|
||||
{
|
||||
if (!basketItems.Any())
|
||||
throw new EmptyBasketOnCheckoutException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,12 +55,13 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
||||
Guard.Against.Null(quantities, nameof(quantities));
|
||||
var basket = await _basketRepository.GetByIdAsync(basketId);
|
||||
Guard.Against.NullBasket(basketId, basket);
|
||||
|
||||
foreach (var item in basket.Items)
|
||||
{
|
||||
if (quantities.TryGetValue(item.Id.ToString(), out var quantity))
|
||||
{
|
||||
if (_logger != null) _logger.LogInformation($"Updating quantity of item ID:{item.Id} to {quantity}.");
|
||||
item.SetNewQuantity(quantity);
|
||||
item.SetQuantity(quantity);
|
||||
}
|
||||
}
|
||||
basket.RemoveEmptyItems();
|
||||
|
||||
@@ -33,6 +33,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
||||
var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec);
|
||||
|
||||
Guard.Against.NullBasket(basketId, basket);
|
||||
Guard.Against.EmptyBasketOnCheckout(basket.Items);
|
||||
|
||||
var catalogItemsSpecification = new CatalogItemsSpecification(basket.Items.Select(item => item.CatalogItemId).ToArray());
|
||||
var catalogItems = await _itemRepository.ListAsync(catalogItemsSpecification);
|
||||
|
||||
Reference in New Issue
Block a user