Skip to content

Commit 7cb6437

Browse files
committed
Fixed ECommerce Marten sample
1 parent 542db9a commit 7cb6437

File tree

10 files changed

+60
-102
lines changed

10 files changed

+60
-102
lines changed

Core.Marten/MartenConfig.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ private static StoreOptions SetStoreOptions(
101101
options.Events.DatabaseSchemaName = schemaName ?? martenConfig.WriteModelSchema;
102102
options.DatabaseSchemaName = schemaName ?? martenConfig.ReadModelSchema;
103103

104-
options.UseNewtonsoftForSerialization(
105-
EnumStorage.AsString,
106-
nonPublicMembersStorage: NonPublicMembersStorage.All
104+
options.UseSystemTextJsonForSerialization(
105+
EnumStorage.AsString
107106
);
108107

109108
options.Events.StreamIdentity = StreamIdentity.AsString;

Core.Testing/TestWebApplicationFactory.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ public class TestWebApplicationFactory<TProject>: WebApplicationFactory<TProject
2121

2222
protected override IHost CreateHost(IHostBuilder builder)
2323
{
24-
builder.ConfigureAppConfiguration((_, config) =>
25-
{
26-
config.AddInMemoryCollection(new Dictionary<string, string?>
27-
{
28-
["ConnectionStrings:kafka"] = "localhost:9092"
29-
});
30-
});
31-
3224
builder.ConfigureServices(services =>
3325
{
3426
services
@@ -45,7 +37,7 @@ protected override IHost CreateHost(IHostBuilder builder)
4537
.AddSingleton<IExternalEventConsumer, DummyExternalEventConsumer>();
4638
});
4739

48-
40+
Environment.SetEnvironmentVariable("ConnectionStrings__kafka", "localhost:9092");
4941
Environment.SetEnvironmentVariable("SchemaName", schemaName);
5042

5143
return Policy.Handle<Exception>()

Sample/ECommerce/Carts/Carts/ShoppingCarts/GettingCartById/ShoppingCartDetails.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void Apply(ShoppingCartCanceled @event) =>
8080
.SingleOrDefault(pi => pi.MatchesProductAndPrice(productItem));
8181
}
8282

83-
public class CartDetailsProjection : SingleStreamProjection<ShoppingCartDetails, string>
83+
public class CartDetailsProjection : SingleStreamProjection<ShoppingCartDetails, Guid>
8484
{
8585
public CartDetailsProjection()
8686
{

Sample/ECommerce/Carts/Carts/ShoppingCarts/GettingCarts/ShoppingCartShortInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void Apply(ShoppingCartCanceled @event)
4343
}
4444
}
4545

46-
public class CartShortInfoProjection : SingleStreamProjection<ShoppingCartShortInfo, string>
46+
public class CartShortInfoProjection : SingleStreamProjection<ShoppingCartShortInfo, Guid>
4747
{
4848
public CartShortInfoProjection()
4949
{

Sample/ECommerce/Carts/Carts/ShoppingCarts/Products/PricedProductItem.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json.Serialization;
2+
13
namespace Carts.ShoppingCarts.Products;
24

35
public record PricedProductItem
@@ -11,6 +13,7 @@ public record PricedProductItem
1113
public decimal TotalPrice => Quantity * UnitPrice;
1214
public ProductItem ProductItem { get; }
1315

16+
[JsonConstructor]
1417
private PricedProductItem(ProductItem productItem, decimal unitPrice)
1518
{
1619
ProductItem = productItem;

Sample/ECommerce/Carts/Carts/ShoppingCarts/Products/ProductItem.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json.Serialization;
2+
13
namespace Carts.ShoppingCarts.Products;
24

35
public record ProductItem
@@ -6,6 +8,7 @@ public record ProductItem
68

79
public int Quantity { get; }
810

11+
[JsonConstructor]
912
private ProductItem(Guid productId, int quantity)
1013
{
1114
ProductId = productId;

Sample/ECommerce/Carts/Carts/ShoppingCarts/ShoppingCart.cs

Lines changed: 46 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,21 @@ namespace Carts.ShoppingCarts;
1212

1313
public class ShoppingCart: Aggregate
1414
{
15-
public Guid ClientId { get; private set; }
15+
public Guid ClientId { get; set; }
1616

17-
public ShoppingCartStatus Status { get; private set; }
17+
public ShoppingCartStatus Status { get; set; }
1818

19-
public IList<PricedProductItem> ProductItems { get; private set; } = null!;
19+
public IList<PricedProductItem> ProductItems { get; set; } = new List<PricedProductItem>();
2020

2121
public decimal TotalPrice => ProductItems.Sum(pi => pi.TotalPrice);
2222

23-
public static ShoppingCart Open(Guid cartId, Guid clientId) => new(cartId, clientId);
24-
25-
public ShoppingCart(){}
26-
27-
private ShoppingCart(
28-
Guid id,
29-
Guid clientId)
23+
public static ShoppingCart Open(Guid cartId, Guid clientId)
3024
{
31-
var @event = ShoppingCartOpened.Create(
32-
id,
33-
clientId
34-
);
25+
var shoppingCart = new ShoppingCart();
3526

36-
Enqueue(@event);
37-
Apply(@event);
38-
}
27+
shoppingCart.Enqueue(ShoppingCartOpened.Create(cartId, clientId));
3928

40-
public void Apply(ShoppingCartOpened @event)
41-
{
42-
Id = @event.CartId;
43-
ClientId = @event.ClientId;
44-
ProductItems = new List<PricedProductItem>();
45-
Status = ShoppingCartStatus.Pending;
29+
return shoppingCart;
4630
}
4731

4832
public void AddProduct(
@@ -54,28 +38,7 @@ public void AddProduct(
5438

5539
var pricedProductItem = productPriceCalculator.Calculate(productItem).Single();
5640

57-
var @event = ProductAdded.Create(Id, pricedProductItem);
58-
59-
Enqueue(@event);
60-
Apply(@event);
61-
}
62-
63-
public void Apply(ProductAdded @event)
64-
{
65-
var newProductItem = @event.ProductItem;
66-
67-
var existingProductItem = FindProductItemMatchingWith(newProductItem);
68-
69-
if (existingProductItem is null)
70-
{
71-
ProductItems.Add(newProductItem);
72-
return;
73-
}
74-
75-
ProductItems.Replace(
76-
existingProductItem,
77-
existingProductItem.MergeWith(newProductItem)
78-
);
41+
Enqueue(ProductAdded.Create(Id, pricedProductItem));
7942
}
8043

8144
public void RemoveProduct(
@@ -92,31 +55,7 @@ public void RemoveProduct(
9255
if(!existingProductItem.HasEnough(productItemToBeRemoved.Quantity))
9356
throw new InvalidOperationException($"Cannot remove {productItemToBeRemoved.Quantity} items of Product with id `{productItemToBeRemoved.ProductId}` as there are only ${existingProductItem.Quantity} items in card");
9457

95-
var @event = ProductRemoved.Create(Id, productItemToBeRemoved);
96-
97-
Enqueue(@event);
98-
Apply(@event);
99-
}
100-
101-
public void Apply(ProductRemoved @event)
102-
{
103-
var productItemToBeRemoved = @event.ProductItem;
104-
105-
var existingProductItem = FindProductItemMatchingWith(@event.ProductItem);
106-
107-
if (existingProductItem == null)
108-
return;
109-
110-
if (existingProductItem.HasTheSameQuantity(productItemToBeRemoved))
111-
{
112-
ProductItems.Remove(existingProductItem);
113-
return;
114-
}
115-
116-
ProductItems.Replace(
117-
existingProductItem,
118-
existingProductItem.Subtract(productItemToBeRemoved)
119-
);
58+
Enqueue(ProductRemoved.Create(Id, productItemToBeRemoved));
12059
}
12160

12261
public void Confirm()
@@ -127,30 +66,52 @@ public void Confirm()
12766
if (ProductItems.Count == 0)
12867
throw new InvalidOperationException($"Confirming empty cart is not allowed.");
12968

130-
var @event = ShoppingCartConfirmed.Create(Id, DateTime.UtcNow);
131-
132-
Enqueue(@event);
133-
Apply(@event);
134-
}
135-
136-
public void Apply(ShoppingCartConfirmed @event)
137-
{
138-
Status = ShoppingCartStatus.Confirmed;
69+
Enqueue(ShoppingCartConfirmed.Create(Id, DateTime.UtcNow));
13970
}
14071

14172
public void Cancel()
14273
{
14374
if(Status != ShoppingCartStatus.Pending)
14475
throw new InvalidOperationException($"Canceling cart in '{Status}' status is not allowed.");
14576

146-
var @event = ShoppingCartCanceled.Create(Id, DateTime.UtcNow);
147-
148-
Enqueue(@event);
149-
Apply(@event);
77+
Enqueue(ShoppingCartCanceled.Create(Id, DateTime.UtcNow));
15078
}
15179

152-
public void Apply(ShoppingCartCanceled @event) =>
153-
Status = ShoppingCartStatus.Canceled;
80+
public override void Apply(object @event)
81+
{
82+
switch (@event)
83+
{
84+
case ShoppingCartOpened opened:
85+
Id = opened.CartId;
86+
ClientId = opened.ClientId;
87+
ProductItems = new List<PricedProductItem>();
88+
Status = ShoppingCartStatus.Pending;
89+
break;
90+
case ProductAdded productAdded:
91+
var newProductItem = productAdded.ProductItem;
92+
var existing = FindProductItemMatchingWith(newProductItem);
93+
if (existing is null)
94+
ProductItems.Add(newProductItem);
95+
else
96+
ProductItems.Replace(existing, existing.MergeWith(newProductItem));
97+
break;
98+
case ProductRemoved productRemoved:
99+
var toRemove = productRemoved.ProductItem;
100+
var found = FindProductItemMatchingWith(toRemove);
101+
if (found == null) break;
102+
if (found.HasTheSameQuantity(toRemove))
103+
ProductItems.Remove(found);
104+
else
105+
ProductItems.Replace(found, found.Subtract(toRemove));
106+
break;
107+
case ShoppingCartConfirmed:
108+
Status = ShoppingCartStatus.Confirmed;
109+
break;
110+
case ShoppingCartCanceled:
111+
Status = ShoppingCartStatus.Canceled;
112+
break;
113+
}
114+
}
154115

155116
private PricedProductItem? FindProductItemMatchingWith(PricedProductItem productItem) =>
156117
ProductItems

Sample/ECommerce/Orders/Orders.Api.Tests/Orders/InitializingOrder/InitializeOrderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class InitializeOrderTests
1414

1515
[Fact]
1616
[Trait("Category", "Acceptance")]
17-
public Task InitializeOrder_ShouldReturn_CreatedStatus_With_OrderId() =>
17+
public Task InitializeOrder_ShouldReturn_CreatedStatuudes_With_OrderId() =>
1818
API.Given()
1919
.When(
2020
POST,

Sample/ECommerce/Orders/Orders/Orders/GettingOrderStatus/OrderDetails.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static EventsWrapper From(IEvent @event) =>
6868
new((int)@event.Version, @event.EventTypeName, @event.Data, @event.Timestamp);
6969
}
7070

71-
public class OrderDetailsProjection: SingleStreamProjection<OrderDetails, string>
71+
public class OrderDetailsProjection: SingleStreamProjection<OrderDetails, Guid>
7272
{
7373
public void Apply(IEvent<OrderInitiated> envelope, OrderDetails details) =>
7474
details.Apply(envelope);

Sample/ECommerce/Orders/Orders/Orders/GettingPendingOrders/PendingOrder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Apply(OrderInitiated @event) =>
1414
}
1515

1616

17-
public class PendingOrdersProjection: SingleStreamProjection<PendingOrder, string>
17+
public class PendingOrdersProjection: SingleStreamProjection<PendingOrder, Guid>
1818
{
1919
public PendingOrdersProjection()
2020
{

0 commit comments

Comments
 (0)