Skip to content
This repository was archived by the owner on Jun 27, 2024. It is now read-only.

Commit ca1f654

Browse files
committed
Update to 0.10.1
1 parent e25dc4a commit ca1f654

File tree

12 files changed

+54
-38
lines changed

12 files changed

+54
-38
lines changed

Diff for: .editorconfig

+2
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33

44
# ReSharper properties
55
resharper_instance_members_qualify_declared_in = this_class
6+
resharper_int_align_assignments = false
7+
resharper_use_heuristics_for_body_style = true
68
resharper_wrap_after_invocation_lpar = false

Diff for: Bookings.Domain/Bookings/Booking.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Bookings.Domain.Bookings;
66

7-
public class Booking : Aggregate<BookingState, BookingId> {
7+
public class Booking : Aggregate<BookingState> {
88
public async Task BookRoom(
99
BookingId bookingId,
1010
string guestId,
@@ -22,7 +22,6 @@ IsRoomAvailable isRoomAvailable
2222

2323
Apply(
2424
new V1.RoomBooked(
25-
bookingId,
2625
guestId,
2726
roomId,
2827
period.CheckIn,
@@ -52,7 +51,6 @@ DateTimeOffset paidAt
5251

5352
Apply(
5453
new V1.PaymentRecorded(
55-
State.Id,
5654
paid.Amount,
5755
outstanding.Amount,
5856
paid.Currency,
@@ -68,7 +66,7 @@ DateTimeOffset paidAt
6866
void MarkFullyPaidIfNecessary(DateTimeOffset when) {
6967
if (State.Outstanding.Amount != 0) return;
7068

71-
Apply(new V1.BookingFullyPaid(State.Id, when));
69+
Apply(new V1.BookingFullyPaid(when));
7270
}
7371

7472
static async Task EnsureRoomAvailable(RoomId roomId, StayPeriod period, IsRoomAvailable isRoomAvailable) {

Diff for: Bookings.Domain/Bookings/BookingEvents.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public static class BookingEvents {
77
public static class V1 {
88
[EventType("V1.RoomBooked")]
99
public record RoomBooked(
10-
string BookingId,
1110
string GuestId,
1211
string RoomId,
1312
LocalDate CheckInDate,
@@ -21,7 +20,6 @@ DateTimeOffset BookingDate
2120

2221
[EventType("V1.PaymentRecorded")]
2322
public record PaymentRecorded(
24-
string BookingId,
2523
float PaidAmount,
2624
float Outstanding,
2725
string Currency,
@@ -31,9 +29,9 @@ DateTimeOffset PaidAt
3129
);
3230

3331
[EventType("V1.FullyPaid")]
34-
public record BookingFullyPaid(string BookingId, DateTimeOffset FullyPaidAt);
32+
public record BookingFullyPaid(DateTimeOffset FullyPaidAt);
3533

3634
[EventType("V1.BookingCancelled")]
37-
public record BookingCancelled(string BookingId, string CancelledBy, DateTimeOffset CancelledAt);
35+
public record BookingCancelled(string CancelledBy, DateTimeOffset CancelledAt);
3836
}
3937
}

Diff for: Bookings.Domain/Bookings/BookingState.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using Eventuous;
33
using static Bookings.Domain.Bookings.BookingEvents;
44

5-
namespace Bookings.Domain.Bookings;
5+
namespace Bookings.Domain.Bookings;
66

7-
public record BookingState : AggregateState<BookingState, BookingId> {
7+
public record BookingState : AggregateState<BookingState> {
88
public string GuestId { get; init; }
99
public RoomId RoomId { get; init; }
1010
public StayPeriod Period { get; init; }
@@ -14,7 +14,8 @@ public record BookingState : AggregateState<BookingState, BookingId> {
1414

1515
public ImmutableList<PaymentRecord> PaymentRecords { get; init; } = ImmutableList<PaymentRecord>.Empty;
1616

17-
internal bool HasPaymentBeenRecorded(string paymentId) => PaymentRecords.Any(x => x.PaymentId == paymentId);
17+
internal bool HasPaymentBeenRecorded(string paymentId)
18+
=> PaymentRecords.Any(x => x.PaymentId == paymentId);
1819

1920
public BookingState() {
2021
On<V1.RoomBooked>(HandleBooked);
@@ -32,7 +33,6 @@ static BookingState HandlePayment(BookingState state, V1.PaymentRecorded e)
3233

3334
static BookingState HandleBooked(BookingState state, V1.RoomBooked booked)
3435
=> state with {
35-
Id = new BookingId(booked.BookingId),
3636
RoomId = new RoomId(booked.RoomId),
3737
Period = new StayPeriod(booked.CheckInDate, booked.CheckOutDate),
3838
GuestId = booked.GuestId,
@@ -43,4 +43,4 @@ static BookingState HandleBooked(BookingState state, V1.RoomBooked booked)
4343

4444
public record PaymentRecord(string PaymentId, Money PaidAmount);
4545

46-
public record DiscountRecord(Money Discount, string Reason);
46+
public record DiscountRecord(Money Discount, string Reason);

Diff for: Bookings.Payments/Application/CommandService.cs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Bookings.Payments.Application;
88
public class CommandService : ApplicationService<Payment, PaymentState, PaymentId> {
99
public CommandService(IAggregateStore store) : base(store) {
1010
OnNew<PaymentCommands.RecordPayment>(
11+
cmd => new PaymentId(cmd.PaymentId),
1112
(payment, cmd) => payment.ProcessPayment(
1213
new PaymentId(cmd.PaymentId),
1314
cmd.BookingId,

Diff for: Bookings.Payments/Domain/Money.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ namespace Bookings.Payments.Domain;
44

55
public record Money {
66
public float Amount { get; internal init; }
7-
public string Currency { get; internal init; }
7+
public string Currency { get; internal init; } = null!;
88

99
static readonly string[] SupportedCurrencies = { "USD", "GPB", "EUR" };
1010

11+
// ReSharper disable once UnusedMember.Global
1112
internal Money() { }
1213

1314
public Money(float amount, string currency) {

Diff for: Bookings.Payments/Domain/Payment.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33

44
namespace Bookings.Payments.Domain;
55

6-
public class Payment : Aggregate<PaymentState, PaymentId> {
6+
public class Payment : Aggregate<PaymentState> {
77
public void ProcessPayment(
88
PaymentId paymentId, string bookingId, Money amount, string method, string provider
99
)
1010
=> Apply(new PaymentRecorded(paymentId, bookingId, amount.Amount, amount.Currency, method, provider));
1111
}
1212

13-
public record PaymentState : AggregateState<PaymentState, PaymentId> {
13+
public record PaymentState : AggregateState<PaymentState> {
1414
public string BookingId { get; init; } = null!;
1515
public float Amount { get; init; }
1616

1717
public PaymentState() {
1818
On<PaymentRecorded>(
1919
(state, recorded) => state with {
20-
Id = new PaymentId(recorded.PaymentId),
2120
BookingId = recorded.BookingId,
2221
Amount = recorded.Amount
2322
}

Diff for: Bookings/Application/BookingsCommandService.cs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Bookings.Application;
99
public class BookingsCommandService : ApplicationService<Booking, BookingState, BookingId> {
1010
public BookingsCommandService(IAggregateStore store, Services.IsRoomAvailable isRoomAvailable) : base(store) {
1111
OnNewAsync<BookRoom>(
12+
cmd => new BookingId(cmd.BookingId),
1213
(booking, cmd, _) => booking.BookRoom(
1314
new BookingId(cmd.BookingId),
1415
cmd.GuestId,
+19-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Eventuous.Projections.MongoDB;
2+
using Eventuous.Subscriptions.Context;
23
using MongoDB.Driver;
34
using static Bookings.Domain.Bookings.BookingEvents;
45

@@ -8,27 +9,35 @@ namespace Bookings.Application.Queries;
89

910
public class BookingStateProjection : MongoProjection<BookingDocument> {
1011
public BookingStateProjection(IMongoDatabase database) : base(database) {
11-
On<V1.RoomBooked>(evt => evt.BookingId, HandleRoomBooked);
12+
On<V1.RoomBooked>(stream => stream.GetId(), HandleRoomBooked);
1213

1314
On<V1.PaymentRecorded>(
14-
evt => evt.BookingId,
15-
(evt, update) => update.Set(x => x.Outstanding, evt.Outstanding)
15+
b => b
16+
.UpdateOne
17+
.DefaultId()
18+
.Update((evt, update) =>
19+
update.Set(x => x.Outstanding, evt.Outstanding)
20+
)
1621
);
1722

18-
On<V1.BookingFullyPaid>(
19-
evt => evt.BookingId,
20-
(_, update) => update.Set(x => x.Paid, true)
23+
On<V1.BookingFullyPaid>(b => b
24+
.UpdateOne
25+
.DefaultId()
26+
.Update((_, update) => update.Set(x => x.Paid, true))
2127
);
2228
}
2329

2430
static UpdateDefinition<BookingDocument> HandleRoomBooked(
25-
V1.RoomBooked evt, UpdateDefinitionBuilder<BookingDocument> update
26-
)
27-
=> update.SetOnInsert(x => x.Id, evt.BookingId)
31+
IMessageConsumeContext<V1.RoomBooked> ctx, UpdateDefinitionBuilder<BookingDocument> update
32+
) {
33+
var evt = ctx.Message;
34+
35+
return update.SetOnInsert(x => x.Id, ctx.Stream.GetId())
2836
.Set(x => x.GuestId, evt.GuestId)
2937
.Set(x => x.RoomId, evt.RoomId)
3038
.Set(x => x.CheckInDate, evt.CheckInDate)
3139
.Set(x => x.CheckOutDate, evt.CheckOutDate)
3240
.Set(x => x.BookingPrice, evt.BookingPrice)
3341
.Set(x => x.Outstanding, evt.OutstandingAmount);
34-
}
42+
}
43+
}

Diff for: Bookings/Application/Queries/MyBookingsProjection.cs

+16-9
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,30 @@ namespace Bookings.Application.Queries;
66

77
public class MyBookingsProjection : MongoProjection<MyBookings> {
88
public MyBookingsProjection(IMongoDatabase database) : base(database) {
9-
On<V1.RoomBooked>(
10-
evt => evt.GuestId,
11-
(evt, update) => update.AddToSet(
12-
x => x.Bookings,
13-
new MyBookings.Booking(evt.BookingId, evt.CheckInDate, evt.CheckOutDate, evt.BookingPrice)
9+
On<V1.RoomBooked>(b => b
10+
.UpdateOne
11+
.Id(ctx => ctx.Message.GuestId)
12+
.UpdateFromContext((ctx, update) =>
13+
update.AddToSet(
14+
x => x.Bookings,
15+
new MyBookings.Booking(ctx.Stream.GetId(),
16+
ctx.Message.CheckInDate,
17+
ctx.Message.CheckOutDate,
18+
ctx.Message.BookingPrice
19+
)
20+
)
1421
)
1522
);
1623

1724
On<V1.BookingCancelled>(
1825
b => b.UpdateOne
19-
.Filter((evt, doc) =>
20-
doc.Bookings.Select(booking => booking.BookingId).Contains(evt.BookingId)
26+
.Filter((ctx, doc) =>
27+
doc.Bookings.Select(booking => booking.BookingId).Contains(ctx.Stream.GetId())
2128
)
22-
.Update((evt, update) =>
29+
.UpdateFromContext((ctx, update) =>
2330
update.PullFilter(
2431
x => x.Bookings,
25-
x => x.BookingId == evt.BookingId
32+
x => x.BookingId == ctx.Stream.GetId()
2633
)
2734
)
2835
);

Diff for: Bookings/HttpApi/Bookings/QueryApi.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class QueryApi : ControllerBase {
1313
[HttpGet]
1414
[Route("{id}")]
1515
public async Task<BookingState> GetBooking(string id, CancellationToken cancellationToken) {
16-
var booking = await _store.Load<Booking>(new BookingId(id), cancellationToken);
16+
var booking = await _store.Load<Booking>(StreamName.For<Booking>(id), cancellationToken);
1717
return booking.State;
1818
}
1919
}

Diff for: Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<TargetFramework>net6.0</TargetFramework>
44
<ImplicitUsings>enable</ImplicitUsings>
55
<Nullable>enable</Nullable>
6-
<EventuousVersion>0.9.0</EventuousVersion>
6+
<EventuousVersion>0.10.1</EventuousVersion>
77
</PropertyGroup>
88
</Project>

0 commit comments

Comments
 (0)