Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
53 lines (43 loc) · 1.98 KB
/
Copy pathProgram.cs
File metadata and controls
53 lines (43 loc) · 1.98 KB
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
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Cratis.Chronicle;
using Cratis.Chronicle.Connections;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Observation;
using LearningPath.FirstProjection;
// Connecting also registers the projection declared on CartSummary with Chronicle.
using var client = new ChronicleClient(ChronicleConnectionString.Development);
var eventStore = await client.GetEventStore("LearningPathFirstProjection");
var cartId = CartId.New();
var appendResult = await eventStore.EventLog.AppendMany(
cartId,
[
new ItemAddedToCart("Coffee beans", 2),
new ItemAddedToCart("Oat milk", 1),
new ItemRemovedFromCart("Oat milk", 1),
new ItemAddedToCart("Cinnamon buns", 4)
]);
if (!appendResult.IsSuccess)
{
Console.WriteLine("Chronicle rejected the events. Check that the Chronicle server is running.");
return 1;
}
// Projections run in the background. Wait for them to catch up before reading our own write.
var completion = await appendResult.WaitForCompletion(TimeSpan.FromSeconds(10));
if (!completion.IsSuccess)
{
Console.WriteLine("The projection did not finish processing the events.");
return 1;
}
var cart = await eventStore.ReadModels.GetInstanceById<CartSummary>((EventSourceId)cartId);
Console.WriteLine($"Cart {cartId.Value} holds {cart.ItemCount} items. Checked out: {cart.CheckedOut}.");
// One more fact, and the read model follows.
var checkoutResult = await eventStore.EventLog.Append(cartId, new CartCheckedOut());
if (!checkoutResult.IsSuccess || !(await checkoutResult.WaitForCompletion(TimeSpan.FromSeconds(10))).IsSuccess)
{
Console.WriteLine("The checkout was not projected.");
return 1;
}
cart = await eventStore.ReadModels.GetInstanceById<CartSummary>((EventSourceId)cartId);
Console.WriteLine($"Cart {cartId.Value} holds {cart.ItemCount} items. Checked out: {cart.CheckedOut}.");
return 0;