/
CartHistory.cs
73 lines (65 loc) · 2.12 KB
/
CartHistory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Carts.ShoppingCarts.AddingProduct;
using Carts.ShoppingCarts.CancelingCart;
using Carts.ShoppingCarts.ConfirmingCart;
using Carts.ShoppingCarts.OpeningCart;
using Carts.ShoppingCarts.RemovingProduct;
using Core.Projections;
namespace Carts.ShoppingCarts.GettingCartHistory;
public class CartHistory: IVersionedProjection
{
public Guid Id { get; set;}
public Guid CartId { get; set;}
public string Description { get; set; } = default!;
public ulong LastProcessedPosition { get; set; }
public void Evolve(object @event)
{
switch (@event)
{
case ShoppingCartOpened cartOpened:
Apply(cartOpened);
return;
case ProductAdded cartOpened:
Apply(cartOpened);
return;
case ProductRemoved cartOpened:
Apply(cartOpened);
return;
case ShoppingCartConfirmed cartOpened:
Apply(cartOpened);
return;
case ShoppingCartCanceled cartCanceled:
Apply(cartCanceled);
return;
}
}
public void Apply(ShoppingCartOpened @event)
{
Id = Guid.NewGuid();
CartId = @event.CartId;
Description = $"Opened Cart with id {@event.CartId}";
}
public void Apply(ProductAdded @event)
{
Id = Guid.NewGuid();
CartId = @event.CartId;
Description = $"Added Product with id {@event.ProductItem.ProductId} to Cart with id {@event.CartId}";
}
public void Apply(ProductRemoved @event)
{
Id = Guid.NewGuid();
CartId = @event.CartId;
Description = $"Removed Product with id {@event.ProductItem.ProductId} from Cart with id {@event.CartId}";
}
public void Apply(ShoppingCartConfirmed @event)
{
Id = Guid.NewGuid();
CartId = @event.CartId;
Description = $"Confirmed Cart with id {@event.CartId}";
}
public void Apply(ShoppingCartCanceled @event)
{
Id = Guid.NewGuid();
CartId = @event.CartId;
Description = $"Canceled Cart with id {@event.CartId}";
}
}