Skip to content

Commit

Permalink
Added slimmed mutable aggregate solution
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed May 3, 2024
1 parent ccea289 commit 2331165
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 95 deletions.
Expand Up @@ -98,7 +98,6 @@ private ShoppingCart(Guid id, Guid clientId, DateTimeOffset now)
var @event = new ShoppingCartOpened(id, clientId, now);

Enqueue(@event);
Apply(@event);
}

//just for default creation of empty object
Expand Down Expand Up @@ -126,7 +125,6 @@ DateTimeOffset now
var @event = new ProductItemAddedToShoppingCart(Id, pricedProductItem, now);

Enqueue(@event);
Apply(@event);
}

private void Apply(ProductItemAddedToShoppingCart productItemAdded)
Expand Down Expand Up @@ -160,7 +158,6 @@ public void RemoveProduct(PricedProductItem productItemToBeRemoved, DateTimeOffs
var @event = new ProductItemRemovedFromShoppingCart(Id, productItemToBeRemoved, now);

Enqueue(@event);
Apply(@event);
}

private bool HasEnough(PricedProductItem productItem)
Expand Down Expand Up @@ -202,7 +199,6 @@ public void Confirm(DateTimeOffset now)
var @event = new ShoppingCartConfirmed(Id, now);

Enqueue(@event);
Apply(@event);
}

private void Apply(ShoppingCartConfirmed confirmed)
Expand All @@ -220,7 +216,6 @@ public void Cancel(DateTimeOffset now)
var @event = new ShoppingCartCanceled(Id, now);

Enqueue(@event);
Apply(@event);
}

private void Apply(ShoppingCartCanceled canceled)
Expand Down
@@ -1,4 +1,4 @@
using IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable.Products;
using IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable.Pricing;
using Ogooreck.BusinessLogic;
using Xunit;

Expand Down
@@ -1,4 +1,4 @@
namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable.Products;
namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable.Pricing;

public interface IProductPriceCalculator
{
Expand Down
@@ -1,7 +1,7 @@
using System.Collections.Immutable;
using IntroductionToEventSourcing.BusinessLogic.Slimmed.Tools;

namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable.Products;
namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable.Pricing;

using ProductId = Guid;
using ProductIdWithPrice = string;
Expand Down
@@ -1,4 +1,4 @@
using IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable.Products;
using IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable.Pricing;

namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable;

Expand Down
@@ -1,4 +1,4 @@
using IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable.Products;
using IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable.Pricing;

namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable;

Expand Down
@@ -1,3 +1,4 @@
using IntroductionToEventSourcing.BusinessLogic.Slimmed.Mutable.Pricing;
using Ogooreck.BusinessLogic;
using Xunit;

Expand All @@ -12,9 +13,9 @@ public class BusinessLogicTests
public void OpensShoppingCart() =>
Spec.Given([])
.When(() =>
ShoppingCart.Open(shoppingCartId, clientId, now)
ShoppingCart.Open(clientId, now)
)
.Then(new ShoppingCartOpened(shoppingCartId, clientId, now));
.Then(new Opened(clientId, now));

// Add
[Fact]
Expand All @@ -32,7 +33,7 @@ public class BusinessLogicTests
[Fact]
public void AddsProductItemToEmptyShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now)
new Opened(clientId, now)
])
.When(state =>
state.AddProduct(
Expand All @@ -42,9 +43,7 @@ public class BusinessLogicTests
)
)
.Then(
new ProductItemAddedToShoppingCart(
shoppingCartId,
new PricedProductItem
new ProductItemAdded(new PricedProductItem
{
ProductId = ProductItem().ProductId, Quantity = ProductItem().Quantity, UnitPrice = Price
},
Expand All @@ -56,8 +55,8 @@ public class BusinessLogicTests
[Fact]
public void AddsProductItemToNonEmptyShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
])
.When(state =>
state.AddProduct(
Expand All @@ -67,9 +66,7 @@ public class BusinessLogicTests
)
)
.Then(
new ProductItemAddedToShoppingCart(
shoppingCartId,
new PricedProductItem
new ProductItemAdded(new PricedProductItem
{
ProductId = OtherProductItem().ProductId,
Quantity = OtherProductItem().Quantity,
Expand All @@ -82,9 +79,9 @@ public class BusinessLogicTests
[Fact]
public void CantAddProductItemToConfirmedShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new ShoppingCartConfirmed(shoppingCartId, now)
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
new Confirmed(now)
])
.When(state =>
state.AddProduct(
Expand All @@ -98,9 +95,9 @@ public class BusinessLogicTests
[Fact]
public void CantAddProductItemToCanceledShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new ShoppingCartCanceled(shoppingCartId, now)
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
new Canceled(now)
])
.When(state =>
state.AddProduct(
Expand All @@ -123,25 +120,23 @@ public class BusinessLogicTests
[Fact]
public void RemovesExistingProductItemFromShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
])
.When(state =>
state.RemoveProduct(PricedProductItemWithQuantity(1), now)
)
.Then(
new ProductItemRemovedFromShoppingCart(
shoppingCartId,
PricedProductItemWithQuantity(1),
new ProductItemRemoved(PricedProductItemWithQuantity(1),
now
)
);

[Fact]
public void CantRemoveNonExistingProductItemFromNonEmptyShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, OtherPricedProductItem(), now),
new Opened(clientId, now),
new ProductItemAdded(OtherPricedProductItem(), now),
])
.When(state =>
state.RemoveProduct(PricedProductItemWithQuantity(1), now)
Expand All @@ -151,9 +146,9 @@ public class BusinessLogicTests
[Fact]
public void CantRemoveExistingProductItemFromCanceledShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new ShoppingCartConfirmed(shoppingCartId, now)
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
new Confirmed(now)
])
.When(state =>
state.RemoveProduct(PricedProductItemWithQuantity(1), now)
Expand All @@ -163,9 +158,9 @@ public class BusinessLogicTests
[Fact]
public void CantRemoveExistingProductItemFromConfirmedShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new ShoppingCartCanceled(shoppingCartId, now)
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
new Canceled(now)
])
.When(state =>
state.RemoveProduct(PricedProductItemWithQuantity(1), now)
Expand All @@ -185,7 +180,7 @@ public class BusinessLogicTests
[Trait("Category", "SkipCI")]
public void CantConfirmEmptyShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new Opened(clientId, now),
])
.When(state =>
state.Confirm(now)
Expand All @@ -195,22 +190,22 @@ public class BusinessLogicTests
[Fact]
public void ConfirmsNonEmptyShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
])
.When(state =>
state.Confirm(now)
)
.Then(
new ShoppingCartConfirmed(shoppingCartId, now)
new Confirmed(now)
);

[Fact]
public void CantConfirmAlreadyConfirmedShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new ShoppingCartConfirmed(shoppingCartId, now)
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
new Confirmed(now)
])
.When(state =>
state.Confirm(now)
Expand All @@ -220,9 +215,9 @@ public class BusinessLogicTests
[Fact]
public void CantConfirmCanceledShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new ShoppingCartCanceled(shoppingCartId, now)
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
new Canceled(now)
])
.When(state =>
state.Confirm(now)
Expand All @@ -242,7 +237,7 @@ public class BusinessLogicTests
[Fact]
public void CancelsEmptyShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new Opened(clientId, now),
])
.When(state =>
state.Cancel(now)
Expand All @@ -252,22 +247,22 @@ public class BusinessLogicTests
[Fact]
public void CancelsNonEmptyShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
])
.When(state =>
state.Cancel(now)
)
.Then(
new ShoppingCartCanceled(shoppingCartId, now)
new Canceled(now)
);

[Fact]
public void CantCancelAlreadyCanceledShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new ShoppingCartCanceled(shoppingCartId, now)
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
new Canceled(now)
])
.When(state =>
state.Cancel(now)
Expand All @@ -277,9 +272,9 @@ public class BusinessLogicTests
[Fact]
public void CantCancelConfirmedShoppingCart() =>
Spec.Given([
new ShoppingCartOpened(shoppingCartId, clientId, now),
new ProductItemAddedToShoppingCart(shoppingCartId, PricedProductItem(), now),
new ShoppingCartConfirmed(shoppingCartId, now)
new Opened(clientId, now),
new ProductItemAdded(PricedProductItem(), now),
new Confirmed(now)
])
.When(state =>
state.Cancel(now)
Expand Down
@@ -1,4 +1,4 @@
namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Mutable;
namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Mutable.Pricing;

public interface IProductPriceCalculator
{
Expand Down

0 comments on commit 2331165

Please sign in to comment.