Skip to content

Commit

Permalink
Refactored also commands
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed May 3, 2024
1 parent 755e078 commit ccea289
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ public class BusinessLogicTests
[Fact]
public void OpensShoppingCart() =>
Spec.Given([])
.When(() => Handle(new OpenShoppingCart(shoppingCartId, clientId, now), new Initial()))
.When(() => Decide(new Open(clientId, now), new Initial()))
.Then(new Opened(clientId, now));

// Add
[Fact]
public void CantAddProductItemToNotExistingShoppingCart() =>
Spec.Given([])
.When(state =>
Handle(
new AddProductItemToShoppingCart(
shoppingCartId,
FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem),
Decide(
new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem),
now
),
state
Expand All @@ -40,10 +38,8 @@ public void AddsProductItemToEmptyShoppingCart() =>
new Opened(clientId, now)
])
.When(state =>
Handle(
new AddProductItemToShoppingCart(
shoppingCartId,
FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem),
Decide(
new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem),
now
),
state
Expand All @@ -64,10 +60,8 @@ public void AddsProductItemToNonEmptyShoppingCart() =>
new ProductItemAdded(pricedProductItem, now),
])
.When(state =>
Handle(
new AddProductItemToShoppingCart(
shoppingCartId,
FakeProductPriceCalculator.Returning(OtherPrice).Calculate(OtherProductItem),
Decide(
new AddProductItem(FakeProductPriceCalculator.Returning(OtherPrice).Calculate(OtherProductItem),
now
),
state
Expand All @@ -88,10 +82,8 @@ public void CantAddProductItemToConfirmedShoppingCart() =>
new Confirmed(now)
])
.When(state =>
Handle(
new AddProductItemToShoppingCart(
shoppingCartId,
FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem),
Decide(
new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem),
now
),
state
Expand All @@ -107,10 +99,8 @@ public void CantAddProductItemToCanceledShoppingCart() =>
new Canceled(now)
])
.When(state =>
Handle(
new AddProductItemToShoppingCart(
shoppingCartId,
FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem),
Decide(
new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem),
now
),
state
Expand All @@ -123,8 +113,8 @@ public void CantAddProductItemToCanceledShoppingCart() =>
public void CantRemoveProductItemFromNotExistingShoppingCart() =>
Spec.Given([])
.When(state =>
Handle(
new RemoveProductItemFromShoppingCart(shoppingCartId, pricedProductItem, now),
Decide(
new RemoveProductItem(pricedProductItem, now),
state
)
)
Expand All @@ -137,8 +127,8 @@ public void RemovesExistingProductItemFromShoppingCart() =>
new ProductItemAdded(pricedProductItem, now),
])
.When(state =>
Handle(
new RemoveProductItemFromShoppingCart(shoppingCartId, pricedProductItem with { Quantity = 1 }, now),
Decide(
new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now),
state
)
)
Expand All @@ -156,8 +146,8 @@ public void CantRemoveNonExistingProductItemFromNonEmptyShoppingCart() =>
new ProductItemAdded(pricedProductItem, now),
])
.When(state =>
Handle(
new RemoveProductItemFromShoppingCart(shoppingCartId, otherPricedProductItem with { Quantity = 1 },
Decide(
new RemoveProductItem(otherPricedProductItem with { Quantity = 1 },
now),
state
)
Expand All @@ -172,8 +162,8 @@ public void CantRemoveExistingProductItemFromCanceledShoppingCart() =>
new Confirmed(now)
])
.When(state =>
Handle(
new RemoveProductItemFromShoppingCart(shoppingCartId, pricedProductItem with { Quantity = 1 }, now),
Decide(
new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now),
state
)
)
Expand All @@ -187,8 +177,8 @@ public void CantRemoveExistingProductItemFromConfirmedShoppingCart() =>
new Canceled(now)
])
.When(state =>
Handle(
new RemoveProductItemFromShoppingCart(shoppingCartId, pricedProductItem with { Quantity = 1 }, now),
Decide(
new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now),
state
)
)
Expand All @@ -200,7 +190,7 @@ public void CantRemoveExistingProductItemFromConfirmedShoppingCart() =>
public void CantConfirmNotExistingShoppingCart() =>
Spec.Given([])
.When(state =>
Handle(new ConfirmShoppingCart(shoppingCartId, now), state)
Decide(new Confirm(now), state)
)
.ThenThrows<InvalidOperationException>();

Expand All @@ -211,7 +201,7 @@ public void CantConfirmEmptyShoppingCart() =>
new Opened(clientId, now),
])
.When(state =>
Handle(new ConfirmShoppingCart(shoppingCartId, now), state)
Decide(new Confirm(now), state)
)
.ThenThrows<InvalidOperationException>();

Expand All @@ -222,7 +212,7 @@ public void ConfirmsNonEmptyShoppingCart() =>
new ProductItemAdded(pricedProductItem, now),
])
.When(state =>
Handle(new ConfirmShoppingCart(shoppingCartId, now), state)
Decide(new Confirm(now), state)
)
.Then(
new Confirmed(now)
Expand All @@ -236,7 +226,7 @@ public void CantConfirmAlreadyConfirmedShoppingCart() =>
new Confirmed(now)
])
.When(state =>
Handle(new ConfirmShoppingCart(shoppingCartId, now), state)
Decide(new Confirm(now), state)
)
.ThenThrows<InvalidOperationException>();

Expand All @@ -248,7 +238,7 @@ public void CantConfirmCanceledShoppingCart() =>
new Canceled(now)
])
.When(state =>
Handle(new ConfirmShoppingCart(shoppingCartId, now), state)
Decide(new Confirm(now), state)
)
.ThenThrows<InvalidOperationException>();

Expand All @@ -258,7 +248,7 @@ public void CantConfirmCanceledShoppingCart() =>
public void CantCancelNotExistingShoppingCart() =>
Spec.Given([])
.When(state =>
Handle(new CancelShoppingCart(shoppingCartId, now), state)
Decide(new Cancel(now), state)
)
.ThenThrows<InvalidOperationException>();

Expand All @@ -268,7 +258,7 @@ public void CancelsEmptyShoppingCart() =>
new Opened(clientId, now),
])
.When(state =>
Handle(new CancelShoppingCart(shoppingCartId, now), state)
Decide(new Cancel(now), state)
)
.ThenThrows<InvalidOperationException>();

Expand All @@ -279,7 +269,7 @@ public void CancelsNonEmptyShoppingCart() =>
new ProductItemAdded(pricedProductItem, now),
])
.When(state =>
Handle(new CancelShoppingCart(shoppingCartId, now), state)
Decide(new Cancel(now), state)
)
.Then(
new Canceled(now)
Expand All @@ -293,7 +283,7 @@ public void CantCancelAlreadyCanceledShoppingCart() =>
new Canceled(now)
])
.When(state =>
Handle(new CancelShoppingCart(shoppingCartId, now), state)
Decide(new Cancel(now), state)
)
.ThenThrows<InvalidOperationException>();

Expand All @@ -305,7 +295,7 @@ public void CantCancelConfirmedShoppingCart() =>
new Confirmed(now)
])
.When(state =>
Handle(new CancelShoppingCart(shoppingCartId, now), state)
Decide(new Cancel(now), state)
)
.ThenThrows<InvalidOperationException>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,26 @@ namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable;

public abstract record ShoppingCartCommand
{
public record OpenShoppingCart(
Guid ShoppingCartId,
public record Open(
Guid ClientId,
DateTimeOffset Now
): ShoppingCartCommand;

public record AddProductItemToShoppingCart(
Guid ShoppingCartId,
public record AddProductItem(
PricedProductItem ProductItem,
DateTimeOffset Now
): ShoppingCartCommand;

public record RemoveProductItemFromShoppingCart(
Guid ShoppingCartId,
public record RemoveProductItem(
PricedProductItem ProductItem,
DateTimeOffset Now
): ShoppingCartCommand;

public record ConfirmShoppingCart(
Guid ShoppingCartId,
public record Confirm(
DateTimeOffset Now
): ShoppingCartCommand;

public record CancelShoppingCart(
Guid ShoppingCartId,
public record Cancel(
DateTimeOffset Now
): ShoppingCartCommand;

Expand All @@ -41,25 +36,24 @@ private ShoppingCartCommand() { }

public static class ShoppingCartService
{
public static Event Handle(ShoppingCartCommand command, ShoppingCart state) =>
public static Event Decide(ShoppingCartCommand command, ShoppingCart state) =>
(state, command) switch
{
(Initial, OpenShoppingCart(_, var clientId, var now)) =>
(Initial, Open(var clientId, var now)) =>
new Opened(clientId, now),

(Pending, AddProductItemToShoppingCart(_, var productItem, var now)) =>
(Pending, AddProductItem(var productItem, var now)) =>
new ProductItemAdded(productItem, now),

(Pending(var productItems),
RemoveProductItemFromShoppingCart(_, var productItem, var now)) =>
(Pending(var productItems), RemoveProductItem(var productItem, var now)) =>
productItems.HasEnough(productItem)
? new ProductItemRemoved(productItem, now)
: throw new InvalidOperationException("Not enough product items to remove"),

(Pending, ConfirmShoppingCart(_, var now)) =>
(Pending, Confirm(var now)) =>
new Confirmed(now),

(Pending, CancelShoppingCart(_, var now)) =>
(Pending, Cancel(var now)) =>
new Canceled(now),

_ => throw new InvalidOperationException($"Cannot {command.GetType().Name} for {state.GetType().Name} shopping cart")
Expand Down

0 comments on commit ccea289

Please sign in to comment.