Prevent negative item orders (#392)

* Pulling changes over from previous branch

* Adding exception and guard clause
This commit is contained in:
Eric Fleming
2020-06-12 21:06:23 -04:00
committed by GitHub
parent 0af21d22f5
commit 248b8ed632
10 changed files with 133 additions and 46 deletions

View File

@@ -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)
{
}
}
}

View File

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