-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (104 loc) · 3.08 KB
/
index.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const express = require ("express")
const app = express();
app.use(express.json());
//Rooms database
const rooms = [
{
name: "Super-Deluxe",
seats: 100,
amenities: "Air Conditioning, AV Equipments, Free Wi-Fi, Large Podium",
price: 5000,
room_id: "10050001",
bookingDetails: [
{
customer_name: "Srikanth",
date: "03/06/2023",
start: "10:00",
end: "14:00",
status: "Confirmed",
},
],
},
{
name: "Premium",
seats: 150,
amenities: "Air Conditioning, AV Equipments, Free Wi-Fi, Large Podium",
price: 7500,
room_id: "15075002",
bookingDetails: [
{
customer_name: "Ajith S",
date: "10/08/2023",
start: "11:00",
end: "15:00",
status: "Payment Pending",
},
],
},
];
//common call api status
app.get("/", (req, res) => {
res.status(200).send("Hall Booking API by Srikanth is running successfully!");
});
//create a room with no_of_seats, amenities, price_per_hour
app.post("/create-room", (req, res) => {
rooms.push({
name: req.body.name,
seats: req.body.seats,
amenities: req.body.amenities,
price: req.body.price,
room_id: `${req.body.seats}${req.body.price}${rooms.length + 1}`,
bookingDetails: [{}],
});
res.send(rooms);
});
//Booking a room with customer_name, Date, Start_Time, End_Time, Room_Id
//new one post book room
app.post("/book-room", (req, res) => {
const { customer_name, date, start, end, room_id } = req.body;
// Find the room with the given room ID
const room = rooms.find((room) => room.room_id === room_id);
// If the room exists, create a new booking and add it to the bookingDetails array
if (room) {
const booking = {
customer_name,
date,
start,
end,
status: "Confirmed", // Assuming the booking is always confirmed upon creation
};
room.bookingDetails.push(booking);
console.log("Booking successful!");
res.status(200).send("Booking successful!");
} else {
console.log("Room not found. Booking failed.");
res.status(404).send("Room not found. Booking failed.");
}
});
//list customers with Booked Data with Room_name, Booked_status, Date, Start_Time and End_Time
app.get("/list-customer", (req, res) => {
let customer_list = [];
rooms.forEach((room) => {
let customer_det = { room_name: room.name };
room.bookingDetails.forEach((customer) => {
customer_det.customer_name = customer.customer_name;
customer_det.date = customer.date;
customer_det.start = customer.start;
customer_det.end = customer.end;
customer_list.push(customer_det);
});
});
res.send(customer_list);
});
//list all rooms with Booked data with Customer_name, Room_name, Date, Start_Time and End_Time
app.get("/booked-rooms", (req, res) => {
console.log("list rooms");
res.status(200).send(rooms);
});
app.get("/", (req, res) => {
console.log("Hall Booking API by Srikanth is running successfully");
});
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log(`server started at ${port}`);
});