forked from Eventuous/eventuous
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooking.cs
More file actions
41 lines (27 loc) · 1.55 KB
/
Copy pathBooking.cs
File metadata and controls
41 lines (27 loc) · 1.55 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
using static Eventuous.Sut.Domain.BookingEvents;
namespace Eventuous.Sut.Domain;
public class Booking : Aggregate<BookingState> {
public void BookRoom(string roomId, StayPeriod period, Money price, string? guestId = null) {
EnsureDoesntExist();
Apply(new RoomBooked(roomId, period.CheckIn, period.CheckOut, price.Amount, guestId));
}
public void Import(string roomId, StayPeriod period, Money price) {
EnsureDoesntExist();
Apply(new BookingImported(roomId, price.Amount, period.CheckIn, period.CheckOut));
}
public void Cancel() => Apply(new BookingCancelled());
public void RecordPayment(string paymentId, Money amount, DateTimeOffset paidAt) {
EnsureExists();
if (HasPaymentRecord(paymentId)) return;
var (previousState, currentState) = Apply(new BookingPaymentRegistered(paymentId, amount.Amount));
if (previousState.AmountPaid != currentState.AmountPaid) {
var outstandingAmount = currentState.Price - currentState.AmountPaid;
Apply(new BookingOutstandingAmountChanged(outstandingAmount.Amount));
if (outstandingAmount.Amount < 0) Apply(new BookingOverpaid(-outstandingAmount.Amount));
}
if (!previousState.IsFullyPaid() && currentState.IsFullyPaid()) Apply(new BookingFullyPaid(paidAt));
}
public bool HasPaymentRecord(string paymentId) => Current.OfType<BookingPaymentRegistered>().Any(x => x.PaymentId == paymentId);
public void Execute() => Apply(new Executed());
}
public record BookingId(string Value) : Id(Value);