-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.js
107 lines (86 loc) · 3.06 KB
/
train.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
import chalk from 'chalk';
import Train from '../models/train.js';
const createTrain = async (req, res) => {
try {
const body = req.body;
// Validate the required fields
if (!body.name) {
throw new Error('Name is undefined');
}
if (!body.types) {
throw new Error('Type map is undefined');
}
if (!body.manufacturerInfo || !body.manufacturerInfo.manufacturer || !body.manufacturerInfo.model) {
throw new Error('Manufacturer information is incomplete');
}
if (!body.obtainedAtYear) {
throw new Error('Obtained at year is undefined');
}
if (!body.seats) {
throw new Error('Seats map is undefined');
}
// Create a new train object
const newTrain = await Train.create({
name: body.name,
types: body.types,
manufacturerInfo: {
manufacturer: body.manufacturerInfo.manufacturer,
model: body.manufacturerInfo.model,
createdAtYear: body.manufacturerInfo.createdAtYear || new Date().getFullYear(),
},
obtainedAtYear: body.obtainedAtYear,
inspections: body.inspections || [],
seats: body.seats,
});
// // Save the new train to the database
// const savedTrain = await newTrain.create();
console.log(chalk.cyan.bold('[Create Train]') + ' New train inserted:', newTrain);
res.json({ trainId: newTrain._id.toString() });
} catch (error) {
console.log(chalk.red.bold('[Create Train] Error:'), error);
res.status(400).json({ error: error.message });
}
};
const getTrainById = async (req, res) => {
try {
const trainId = req.params.id;
// Find the train by ID
const train = await Train.findById(trainId);
if (!train) {
throw new Error(`Train with ID ${trainId} not found`);
}
console.log(chalk.cyan.bold('[Get Train by ID]') + ` Train found: ${train.name}`);
res.json(train);
} catch (error) {
console.log(chalk.red.bold('[Get Train by ID] Error:'), error);
res.status(400).json({ error: error.message });
}
};
const getTrainSeats = async (req, res) => {
try {
const trainId = req.params.id;
const listOfSeatTypes = req.params.listOfSeatTypes.split(',');
// Find the train by ID
const train = await Train.findById(trainId);
if (!train) {
throw new Error(`Train with ID ${trainId} not found`);
}
// Filter the seats based on the provided seat types
const filteredSeats = [];
// Iterate through each seat in the train
for (const seat of train.seats.values()) {
for (const seatType of listOfSeatTypes) {
if (seat.types.get(seatType)) {
filteredSeats.push(seat);
break;
}
}
}
console.log(chalk.cyan.bold('[Get Train Seats]') + ` Seats found for train: ${train.name}`);
res.json(filteredSeats);
} catch (error) {
console.log(chalk.red.bold('[Get Train Seats] Error:'), error);
res.status(400).json({ error: error.message });
}
};
export { createTrain, getTrainById, getTrainSeats };