Skip to content

Commit

Permalink
revision check implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
erikshafer committed May 23, 2024
1 parent 82ac80d commit 9161c2d
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions StudentEnrollment03.Esdb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,42 @@
var settings = EventStoreClientSettings.Create(connectionString);
var client = new EventStoreClient(settings);

// Append the initial batch of events.
await client.AppendToStreamAsync(
streamId,
StreamState.Any,
new[] { created, enrolled, enrolled2, enrolled3, emailChanged },
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,
StreamPosition.Start,
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: ");
Expand Down

0 comments on commit 9161c2d

Please sign in to comment.