Skip to content

Commit 44c3fd7

Browse files
committed
ROUTES/add init detail stats endpoints
1 parent 7f51aea commit 44c3fd7

File tree

5 files changed

+46
-15
lines changed

5 files changed

+46
-15
lines changed

Diff for: config/database.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const mongoose = require("mongoose");
22

33
const connectDB = async () => {
44
const dbConnection = await mongoose.connect(
5-
// process.env.MONGODB_URI,
6-
process.env.MONGODB_LOCAL_URI,
5+
// process.env.MONGODB_URI_PROD,
6+
process.env.MONGODB_URI_LOCAL,
77
{ useNewUrlParser: true }
88
);
99

Diff for: controllers/tornadoes.js

+38
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,44 @@ exports.getTornadoById = asyncHandler(async (req, res, next) => {
2323
res.status(200).json({ success: true, data: tornado });
2424
});
2525

26+
// @desc get tornado count by year for each state
27+
// @route GET /api/v1/tornadoes/count/:state/:year
28+
// @access Public
29+
exports.getTornadoYearCountByState = asyncHandler(async (req, res, next) => {
30+
const { year } = req.params;
31+
const yearCountsByState = await Tornado.aggregate([
32+
{
33+
$match: { year: year },
34+
},
35+
{
36+
$group: { _id: "$state_abbr" },
37+
},
38+
{ $sort: { _id: -1 } },
39+
]);
40+
41+
if (!yearCountsByState) {
42+
return next(
43+
new ErrorResponse("Error getting tornado year counts by state", 404)
44+
);
45+
}
46+
console.log("STATE YEAR COUNT >> \n", req.params);
47+
res.status(200).json({ success: true, data: yearCountsByState });
48+
});
49+
50+
// @desc get tornado count by year for each month
51+
// @route GET /api/v1/tornadoes/count/:state/:year
52+
// @access Public
53+
exports.getTornadoYearCountByMonth = asyncHandler(async (req, res, next) => {
54+
const { year, month } = req.params;
55+
const tornadoes = await Tornado.find({ state_abbr: state, year: year });
56+
57+
if (!tornadoes) {
58+
return next(new ErrorResponse(`Tornado with id of ${id}`, 404));
59+
}
60+
console.log("STATE YEAR COUNT >> \n", req.params);
61+
res.status(200).json({ success: true, data: tornadoes.length });
62+
});
63+
2664
// @desc Add new tornado
2765
// @route POST /api/v1/tornadoes/:id
2866
// @access Private

Diff for: middleWare/query-builder.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// @desc handles incoming request queries & filters
22
exports.queryBuilder = (model) => async (req, res, next) => {
33
let query;
4-
const reqQuery = { ...req.query };
5-
let queryStr = JSON.stringify(reqQuery);
4+
5+
let queryStr = JSON.stringify({ ...req.query });
66

77
queryStr = queryStr.replace(
88
/\b(gt|gte|in|lt|lte)\b/g,
@@ -11,11 +11,6 @@ exports.queryBuilder = (model) => async (req, res, next) => {
1111

1212
query = model.find(JSON.parse(queryStr));
1313

14-
// query = Tornado.find(
15-
// JSON.parse(queryStr),
16-
// "date nwsNumber stateFips county1fips fScale injuries fatalities"
17-
// ).sort({ date: 1 });
18-
1914
const results = await query;
2015

2116
res.queryResults = {

Diff for: routes/tornadoes.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ const TornadoModel = require("../models/Tornado");
77
const {
88
getAllTornadoes,
99
getTornadoById,
10-
createTornado,
11-
updateTornadoById,
12-
deleteTornadoById,
10+
getTornadoYearCountByState,
1311
} = require("../controllers/tornadoes");
1412

1513
router.route("/").get(queryBuilder(TornadoModel), getAllTornadoes);
16-
1714
router.route("/:id").get(getTornadoById);
15+
router.route("/count/:year/state").get(getTornadoYearCountByState);
1816

1917
module.exports = router;

Diff for: seeder.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ require("dotenv").config({
77
const TornadoModel = require("./models/Tornado");
88

99
mongoose.connect(
10-
// process.env.MONGODB_URI,
11-
process.env.MONGODB_LOCAL_URI,
10+
// process.env.MONGODB_URI_PROD,
11+
process.env.MONGODB_URI_LOCAL,
1212
{
1313
useNewUrlParser: true,
1414
}

0 commit comments

Comments
 (0)