-
Notifications
You must be signed in to change notification settings - Fork 0
/
stops.js
130 lines (109 loc) · 3.25 KB
/
stops.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
125
126
127
128
129
130
import * as util from 'node:util';
import * as mongoose from 'mongoose';
import Stop from '../models/stop.js';
const insertStop = async (req, res) => {
const body = req.body;
const newStop = await Stop.create({
name: body.name,
placeId: new mongoose.Types.ObjectId(body.placeId)
});
console.log(newStop);
res.json({ stopId: newStop._id.toString() })
};
const getStopById = async (req, res) => {
const stopId = req.params.id;
if (typeof stopId !== 'string') {
res.status(400);
throw new Error('Stop ID is not string type');
}
if (stopId.length !== 24) {
res.status(400);
const lengthMessage = stopId.length < 24 ? 'short' : 'long';
throw new Error(`Stop ID is too ${lengthMessage} (24 characters)`);
}
const stop = await Stop.findById(stopId);
if (!stop) {
res.status(404);
throw new Error(`Stop ${stopId} not found`);
}
res.json(stop);
};
const getAllStops = async (req, res) => {
const stops = await Stop.find({}).exec();
if (stops.length === 0) {
res.status(404);
throw new Error(`Stops ${stopId} not found`);
}
res.json(stops);
};
const getStopByName = async (req, res) => {
const stopName = req.params.name;
if (typeof stopName !== 'string') {
res.status(400);
throw new Error('Stop name is not string type');
}
const stop = await Stop.findOne({
name: { $regex: new RegExp(`.*${stopName}.*`, 'i') }
});
if (!stop) {
res.status(404);
throw new Error(`Stop '${stopName}' not found`);
}
res.json(stop);
};
const getStopsByPlace = async (req, res) => {
const { placeName, provinceName } = req.query;
if (typeof placeName === 'undefined' && typeof provinceName === 'undefined') {
res.status(400);
throw new Error('Place and province names are both undefined (unsent)');
}
const matchStage = {};
if (typeof placeName === 'string') {
matchStage.name = { $regex: new RegExp(`.*${placeName}.*`, 'i') };
}
if (typeof provinceName === 'string') {
matchStage.province = { $regex: new RegExp(`.*${provinceName}.*`, 'i') };
}
const stops = await Stop.aggregate([
{
$lookup: {
from: 'places',
localField: 'placeId',
foreignField: '_id',
as: 'place',
pipeline: [
{ $match: matchStage },
{
$project: {
'_id': false,
'name': true,
'province': true
}
}
]
}
},
{ $match: { 'place.0': { $exists: true } } },
{
$project: {
'_id': false,
'name': true,
'place': true
}
}
])
.exec();
// console.log(util.inspect(stops, false, null, true));
if (stops.length === 0) {
res.status(404);
throw new Error(`Stops not found`);
}
res.json(stops);
};
export {
insertStop,
getAllStops,
getStopById,
getStopByName,
getStopsByPlace
};