-
Notifications
You must be signed in to change notification settings - Fork 12
/
Booking.cs
78 lines (65 loc) · 2.12 KB
/
Booking.cs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Eventuous;
using static Bookings.Domain.Bookings.BookingEvents;
using static Bookings.Domain.Services;
namespace Bookings.Domain.Bookings;
public class Booking : Aggregate<BookingState, BookingId> {
public async Task BookRoom(
BookingId bookingId,
string guestId,
RoomId roomId,
StayPeriod period,
Money price,
Money prepaid,
DateTimeOffset bookedAt,
IsRoomAvailable isRoomAvailable
) {
EnsureDoesntExist();
await EnsureRoomAvailable(roomId, period, isRoomAvailable);
var outstanding = price - prepaid;
Apply(
new V1.RoomBooked(
bookingId,
guestId,
roomId,
period.CheckIn,
period.CheckOut,
price.Amount,
prepaid.Amount,
outstanding.Amount,
price.Currency,
bookedAt
)
);
MarkFullyPaidIfNecessary(bookedAt);
}
public void RecordPayment(
Money paid,
string paymentId,
string paidBy,
DateTimeOffset paidAt
) {
EnsureExists();
if (State.HasPaymentBeenRecorded(paymentId)) return;
var outstanding = State.Outstanding - paid;
Apply(
new V1.PaymentRecorded(
State.Id,
paid.Amount,
outstanding.Amount,
paid.Currency,
paymentId,
paidBy,
paidAt
)
);
MarkFullyPaidIfNecessary(paidAt);
}
void MarkFullyPaidIfNecessary(DateTimeOffset when) {
if (State.Outstanding.Amount != 0) return;
Apply(new V1.BookingFullyPaid(State.Id, when));
}
static async Task EnsureRoomAvailable(RoomId roomId, StayPeriod period, IsRoomAvailable isRoomAvailable) {
var roomAvailable = await isRoomAvailable(roomId, period);
if (!roomAvailable) throw new DomainException("Room not available");
}
}