-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreservation.js
79 lines (64 loc) · 2.36 KB
/
reservation.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
import * as mongoose from 'mongoose';
import chalk from 'chalk';
import Reservation from '../models/reservation.js';
const createReservation = async (req, res) => {
try {
const body = req.body;
// Validate the required fields
if (!body.userId) {
throw new Error('User ID is undefined');
}
if (!body.railRouteId) {
throw new Error('Train route ID is undefined');
}
if (!body.seats || body.seats.length === 0) {
throw new Error('Seats array is empty or undefined');
}
// Create a new reservation
const newReservation = await Reservation.create({
userId: body.userId,
railRouteId: body.railRouteId,
seats: body.seats
});
res.json({ reservationId: newReservation._id.toString() });
} catch (error) {
console.log(chalk.red.bold('[Create Reservaion] Error:'), error);
res.status(400).json({ error: error.message });
}
};
const getReservationById = async (req, res) => {
try {
const reservationId = req.params.id;
// Find the reservation by ID
const reservation = await Reservation.findById(reservationId);
if (!reservation) {
throw new Error(`Reservation of ID ${reservationId} not found`);
}
console.log(chalk.cyan.bold('[Get reservation by user ID]') + ` Reservation found: ${reservation._id}`);
res.json(reservation);
} catch (error) {
console.log(chalk.red.bold('[Get reservation by ID] Error:'), error);
res.status(400).json({ error: error.message });
}
};
const getReservationsByUserId = async (req, res) => {
try {
const userId = new mongoose.Types.ObjectId(req.params.id);
// Find the reservation by user ID
const reservations = await Reservation.find({ userId });
if (reservations.length === 0) {
throw new Error(`Reservations of user ID ${userId} not found`);
}
console.log(chalk.cyan.bold('[Get reservations by user ID]') + ` Reservations found`);
res.json(reservations);
}
catch (error) {
console.log(chalk.red.bold('[Get reservations by user ID] Error:'), error);
res.status(400).json({ error: error.message });
}
};
export {
createReservation,
getReservationById,
getReservationsByUserId
};