Skip to content

Commit 2a52ca2

Browse files
committed
crrsBooking is done :-)
1 parent 6f31afe commit 2a52ca2

File tree

2 files changed

+137
-23
lines changed

2 files changed

+137
-23
lines changed

src/katas/cqrsBooking/cqrsBooking.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,60 @@ describe("Registry object...", () => {
8787
});
8888
});
8989
});
90+
describe("write() method:", () => {
91+
const bookingsCountBefore = hotel.registry.rooms[0].bookings.length;
92+
test(`should bookings length should be +1 after write`, () => {
93+
// Write:
94+
hotel.registry.write("clientTest", rooms[0].name, {
95+
arrival: new Date("2020-12-15T15:00:00"),
96+
departure: new Date("2020-12-16T10:00:00"),
97+
});
98+
// Test:
99+
expect(hotel.registry.rooms[0].bookings.length).toBe(
100+
bookingsCountBefore + 1
101+
);
102+
});
103+
test(`should bookings length should be +1 again after write`, () => {
104+
const bookingsCountBefore = hotel.registry.rooms[0].bookings.length;
105+
// Write:
106+
hotel.registry.write("clientTest", rooms[0].name, {
107+
arrival: new Date("2020-12-16T15:00:00"),
108+
departure: new Date("2020-12-17T10:00:00"),
109+
});
110+
// Test:
111+
expect(hotel.registry.rooms[0].bookings.length).toBe(
112+
bookingsCountBefore + 1
113+
);
114+
});
115+
});
116+
});
117+
118+
describe("Hotel object ...", () => {
119+
describe("bookARoom() method:", () => {
120+
const bookingsCountBefore = hotel.registry.rooms[1].bookings.length;
121+
122+
test("should booking success be truthy", () => {
123+
expect(
124+
hotel.bookARoom("clientTest", rooms[1].name, {
125+
arrival: new Date("2020-12-17T15:00:00"),
126+
departure: new Date("2020-12-18T10:00:00"),
127+
})
128+
).toBeTruthy();
129+
});
130+
131+
test("should bookings increase of 1", () => {
132+
expect(hotel.registry.rooms[1].bookings.length).toBe(
133+
bookingsCountBefore + 1
134+
);
135+
});
136+
137+
test("should booking failed be falsy (date already booked)", () => {
138+
expect(
139+
hotel.bookARoom("clientTest", rooms[1].name, {
140+
arrival: new Date("2020-12-17T15:00:00"),
141+
departure: new Date("2020-12-18T10:00:00"),
142+
})
143+
).toBeFalsy();
144+
});
145+
});
90146
});

src/katas/cqrsBooking/cqrsBooking.ts

Lines changed: 81 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ export class Room {
2626
isFree(arrival: Date, departure: Date) {
2727
const booked = this.bookings.find(
2828
(booking) =>
29-
(arrival > booking.date.arrival && arrival < booking.date.departure) ||
30-
(departure > booking.date.arrival && departure < booking.date.departure)
29+
(arrival >= booking.date.arrival &&
30+
arrival <= booking.date.departure) ||
31+
(departure >= booking.date.arrival &&
32+
departure <= booking.date.departure)
3133
);
3234

3335
return booked ? false : true;
@@ -40,7 +42,21 @@ export class Registry {
4042
this.rooms = rooms;
4143
}
4244

43-
write = () => {};
45+
write = (
46+
clientId: string,
47+
roomName: string,
48+
date: { arrival: Date; departure: Date }
49+
) => {
50+
const roomIndex = this.rooms.findIndex((room) => room.name === roomName);
51+
52+
// If no room with roomName
53+
if (roomIndex === -1) {
54+
return;
55+
}
56+
57+
this.rooms[roomIndex].bookings.push(new Booking(clientId, date));
58+
};
59+
4460
read = (filters?: {
4561
date?: { arrival: Date; departure: Date };
4662
roomName?: string;
@@ -62,14 +78,9 @@ export class Registry {
6278
);
6379
return freeRooms;
6480
}
65-
};
6681

67-
bookARoom = (
68-
clientId: string,
69-
roomName: string,
70-
arrivalDate: Date,
71-
departureDate: Date
72-
) => {};
82+
return [];
83+
};
7384
}
7485

7586
export class Hotel {
@@ -79,28 +90,75 @@ export class Hotel {
7990
this.name = name;
8091
this.registry = registry;
8192
}
93+
94+
bookARoom = (
95+
clientId: string,
96+
roomName: string,
97+
date: { arrival: Date; departure: Date }
98+
): boolean => {
99+
const room = this.registry.rooms.find((room) => room.name === roomName);
100+
101+
// If room does not exist or is busy:
102+
if (!room || !room.isFree(date.arrival, date.departure)) {
103+
return false;
104+
}
105+
106+
this.registry.write(clientId, roomName, date);
107+
108+
return true;
109+
};
82110
}
83111

84-
/*
85-
// == USER STORY ==
112+
// == USER STORIES ==
86113
if (require.main === module) {
114+
// == PREPARE ==
87115
const rooms = [
88116
new Room("Red room"),
89117
new Room("French room"),
90118
new Room("NSFW room"),
91119
];
92-
const hotel = new Hotel("Suniron Hotel", rooms);
120+
const registry = new Registry(rooms);
121+
const hotel = new Hotel("Suniron Hotel", registry);
122+
// Add some bookings:
123+
hotel.registry.rooms[0].bookings = [
124+
new Booking("mrWhite", {
125+
arrival: new Date("2020-12-12T14:00:00"),
126+
departure: new Date("2020-12-13T10:00:00"),
127+
}),
128+
new Booking("#mrBlanc", {
129+
arrival: new Date("2020-12-14T14:00:00"),
130+
departure: new Date("2020-12-16T10:00:00"),
131+
}),
132+
];
133+
134+
// == STORY 1 ==
135+
console.log("\n******************************");
93136
console.log(`** WELCOME TO ${hotel.name.toLocaleUpperCase()} **`);
94-
console.log(" ==== STORY 1 ====");
95-
console.log("User: Hey, i want to see all free rooms.");
96-
console.log("Suniron: No problem, look this screen:");
137+
console.log("******************************");
138+
console.log("\n==== STORY 1 ====");
139+
console.log("User: Hey, i want to see all free rooms!");
140+
console.log(
141+
"Receptionist: Ok, we got these:",
142+
(hotel.registry.read({
143+
date: {
144+
arrival: new Date("2020-12-12T15:00:00"),
145+
departure: new Date("2020-12-13T10:00:00"),
146+
},
147+
}) as Array<Room>)
148+
.map((freeRoom) => freeRoom.name)
149+
.join(", ") + "."
150+
);
151+
152+
// == STORY 2 ==
153+
console.log("\n==== STORY 2 ====");
154+
console.log("User: I want to book French Room tonight, is it possible?");
97155
console.log(
98-
hotel
99-
.getFreeRooms()
100-
.map((room) => room)
101-
.join("\n")
156+
"Receptionist: Well, i'm booking that for you ;-). Please, wait..."
102157
);
103-
console.log(" ==== STORY 2 ====");
104-
console.log("TODO...");
158+
// Book the room:
159+
hotel.bookARoom("Mr BLANC", "French Room", {
160+
arrival: new Date(Date.now()),
161+
departure: new Date(Date.now() + 1),
162+
});
163+
console.log("Receptionist: ... OK! I give you the keys <8-)");
105164
}
106-
*/

0 commit comments

Comments
 (0)