-
-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example of closing and opening as one operation
Adjusted the closing the books sample to align with article content
- Loading branch information
1 parent
0c70ed9
commit 906ee6b
Showing
7 changed files
with
149 additions
and
45 deletions.
There are no files selected for viewing
15 changes: 10 additions & 5 deletions
15
Sample/ClosingTheBooks/PointOfSales.Api/Core/Marten/DocumentSessionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,35 @@ | ||
using Marten; | ||
|
||
namespace Helpdesk.Api.Core.Marten; | ||
namespace PointOfSales.Api.Core.Marten; | ||
|
||
public static class DocumentSessionExtensions | ||
{ | ||
public static Task Add<T>(this IDocumentSession documentSession, string id, object[] events, CancellationToken ct) | ||
where T : class => | ||
documentSession.Add<T, object>(id, events, ct); | ||
|
||
public static Task Add<T, TEvent>(this IDocumentSession documentSession, string id, TEvent[] events, | ||
CancellationToken ct) | ||
where T : class | ||
{ | ||
if (events.Length == 0) | ||
return Task.CompletedTask; | ||
|
||
documentSession.Events.StartStream<T>(id, events); | ||
documentSession.Events.StartStream<T>(id, events.Cast<object>().ToArray()); | ||
return documentSession.SaveChangesAsync(token: ct); | ||
} | ||
|
||
public static Task GetAndUpdate<T>( | ||
public static Task GetAndUpdate<T, TEvent>( | ||
this IDocumentSession documentSession, | ||
string id, | ||
int version, | ||
Func<T, object[]> handle, | ||
Func<T, TEvent[]> handle, | ||
CancellationToken ct | ||
) where T : class => | ||
documentSession.Events.WriteToAggregate<T>(id, version, stream => | ||
{ | ||
var result = handle(stream.Aggregate); | ||
if (result.Length != 0) | ||
stream.AppendMany(result); | ||
stream.AppendMany(result.Cast<object>().ToArray()); | ||
}, ct); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
Sample/ClosingTheBooks/PointOfSales/CashRegister/CashRegister.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Sample/ClosingTheBooks/PointOfSales/CashierShifts/CloseAndOpenShift.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using Marten; | ||
|
||
namespace PointOfSales.CashierShifts; | ||
|
||
using static CashierShiftEvent; | ||
using static CashierShiftCommand; | ||
using static CashierShiftDecider; | ||
using CommandResult = (CashierShiftId StreamId, CashierShiftEvent[] Events); | ||
|
||
public record CloseAndOpenCommand( | ||
CashierShiftId CashierShiftId, | ||
string CashierId, | ||
decimal DeclaredTender, | ||
DateTimeOffset Now | ||
); | ||
|
||
public static class CloseAndOpenShift | ||
{ | ||
public static (CommandResult, CommandResult) Handle(CloseAndOpenCommand command, CashierShift currentShift) | ||
{ | ||
var (currentShiftId, cashierId, declaredTender, now) = command; | ||
var closingResult = Decide(new CloseShift(currentShiftId, declaredTender, now), currentShift); | ||
|
||
currentShift = closingResult.Aggregate(currentShift, (current, @event) => current.Apply(@event)); | ||
|
||
var openResult = Decide(new OpenShift(currentShiftId, cashierId, now), currentShift); | ||
|
||
// double check if it was actually | ||
var opened = openResult.OfType<ShiftOpened>().SingleOrDefault(); | ||
if (opened == null) | ||
throw new InvalidOperationException("Cannot open new shift!"); | ||
|
||
return ((currentShiftId, closingResult), (opened.CashierShiftId, openResult)); | ||
} | ||
|
||
public static async Task<CashierShiftId> CloseAndOpenCashierShift( | ||
this IDocumentSession documentSession, | ||
CloseAndOpenCommand command, | ||
int version, | ||
CancellationToken ct | ||
) | ||
{ | ||
var currentShift = | ||
await documentSession.Events.AggregateStreamAsync<CashierShift>(command.CashierShiftId, token: ct) ?? | ||
new CashierShift.NonExisting(); | ||
|
||
var (closingResult, openResult) = Handle(command, currentShift); | ||
|
||
// Append Closing result to the old stream | ||
if (closingResult.Events.Length > 0) | ||
documentSession.Events.Append(closingResult.StreamId, version, closingResult.Events.AsEnumerable()); | ||
|
||
if (openResult.Events.Length > 0) | ||
documentSession.Events.StartStream<CashierShift>(openResult.StreamId, openResult.Events.AsEnumerable()); | ||
|
||
await documentSession.SaveChangesAsync(ct); | ||
|
||
return openResult.StreamId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters