Skip to content

Commit f483b60

Browse files
committed
start adding query filters
1 parent ff15f9b commit f483b60

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

controllers/tornadoes.js

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,78 @@ const Tornado = require("../models/tornado");
33
// @desc get all tornadoes
44
// @route GET /api/v1/tornadoes
55
// @access Public
6-
exports.getAllTornadoes = (req, res, next) => {
7-
res.status(200).json({
8-
responseOK: true,
9-
data: "return all tornadoes",
10-
});
6+
exports.getAllTornadoes = async (req, res, next) => {
7+
let query;
8+
let queryStr = JSON.stringify({ ...req.query });
9+
10+
queryStr = queryStr.replace(
11+
/\b(gt|gte|lt|lte|in)\b/g,
12+
(match) => `$${match}`
13+
);
14+
15+
query = Tornado.find(
16+
JSON.parse(queryStr),
17+
"date nwsNumber stateFips county1fips fScale injuries fatalities"
18+
).sort({ date: 1 });
19+
20+
try {
21+
const tornadoes = await query;
22+
if (tornadoes) {
23+
res.status(200).json({
24+
success: true,
25+
count: tornadoes.length,
26+
query: req.query,
27+
data: tornadoes,
28+
});
29+
}
30+
} catch (error) {
31+
res.status(400).json({ status: 400 });
32+
}
1133
};
1234

1335
// @desc get single tornado by id
1436
// @route GET /api/v1/tornadoes/:id
1537
// @access Public
1638
exports.getTornadoById = async (req, res, next) => {
17-
try {
18-
const tornado = await Tornado.findById(req.params.id);
39+
try {
40+
const tornado = await Tornado.findById(req.params.id);
1941

20-
if (!tornado) {
21-
return res.status(400).json({ success: false });
22-
}
42+
if (!tornado) {
43+
return res.status(400).json({ success: false });
44+
}
2345

24-
res.status(200).json({ success: true, data: tornado });
25-
} catch (error) {
26-
res.status(400).json({ status: 400 });
27-
}
46+
res.status(200).json({ success: true, data: tornado });
47+
} catch (error) {
48+
res.status(400).json({ status: 400 });
49+
}
2850
};
2951

3052
// @desc Add new tornado
3153
// @route POST /api/v1/tornadoes/:id
3254
// @access Private
3355
exports.createTornado = (req, res, next) => {
34-
res.status(200).json({
35-
responseOK: true,
36-
data: `add new tornado with id: ${req.params.id}`,
37-
});
56+
res.status(200).json({
57+
responseOK: true,
58+
data: `add new tornado with id: ${req.params.id}`,
59+
});
3860
};
3961

4062
// @desc update tornado by id
4163
// @route PUT /api/v1/tornadoes/:id
4264
// @access Private
4365
exports.updateTornadoById = (req, res, next) => {
44-
res.status(200).json({
45-
responseOK: true,
46-
data: `update tornado with id: ${req.params.id}`,
47-
});
66+
res.status(200).json({
67+
responseOK: true,
68+
data: `update tornado with id: ${req.params.id}`,
69+
});
4870
};
4971

5072
// @desc delete single tornado by id
5173
// @route DELETE /api/v1/tornadoes/:id
5274
// @access Private
5375
exports.deleteTornadoById = (req, res, next) => {
54-
res.status(200).json({
55-
responseOK: true,
56-
data: `delete tornado with id: ${req.params.id}`,
57-
});
76+
res.status(200).json({
77+
responseOK: true,
78+
data: `delete tornado with id: ${req.params.id}`,
79+
});
5880
};

0 commit comments

Comments
 (0)