diff --git a/StudentEnrollment03.Esdb/Program.cs b/StudentEnrollment03.Esdb/Program.cs index 388f735..16b0b43 100644 --- a/StudentEnrollment03.Esdb/Program.cs +++ b/StudentEnrollment03.Esdb/Program.cs @@ -87,6 +87,7 @@ var settings = EventStoreClientSettings.Create(connectionString); var client = new EventStoreClient(settings); +// Append the initial batch of events. await client.AppendToStreamAsync( streamId, StreamState.Any, @@ -94,13 +95,7 @@ await client.AppendToStreamAsync( cancellationToken: default ); -await client.AppendToStreamAsync( - streamId, - new StreamRevision(), // StreamState.StreamExists, // TODO - new[] { withdrawn }, - cancellationToken: default -); - +// Read from the stream we just appended to. var streamResult = client.ReadStreamAsync( Direction.Forwards, streamId, @@ -108,13 +103,26 @@ await client.AppendToStreamAsync( cancellationToken: default ); +// Optional safety check, but here we're ensuring the stream was found. if (await streamResult.ReadState is ReadState.StreamNotFound) { Console.WriteLine($"The fetched stream (id: {streamId}) that was read was in StreamNotFound state"); return; } +// Okay, taking that StreamResult we're going to make it a List of ResolvedEvents. var eventStream = await streamResult.ToListAsync(); +// Get the last event's event number. +var lastEventInStream = eventStream.Last().Event.EventNumber.ToUInt64(); + +// Append another event. This time let's make sure no one has appended to (AKA updated) the stream. +await client.AppendToStreamAsync( + streamId, + new StreamRevision(lastEventInStream), // checking against expected revision number + new[] { withdrawn }, + cancellationToken: default +); + var student = new Student(); Console.WriteLine($"Events (total: {eventStream.Count}) from selected stream: ");