Skip to content

Commit 962101b

Browse files
committed
added dynamic queries to cap and core endpoints
1 parent 2f2c22f commit 962101b

File tree

7 files changed

+84
-30
lines changed

7 files changed

+84
-30
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
],
2626
"linebreak-style": [
2727
"error",
28-
"unix"
28+
"unix",
29+
"windows"
2930
],
3031
"quotes": [
3132
"error",

utilities/capsule-query.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Returns capsule query object from optional querystring inputs
3+
*/
4+
5+
exports.capsuleQuery = (req) => {
6+
let query = {}
7+
8+
if (req.query.capsule_serial) {
9+
query.capsule_serial = req.query.capsule_serial
10+
}
11+
if (req.query.status) {
12+
query.status = req.query.status
13+
}
14+
if (req.query.original_launch) {
15+
query.original_launch = req.query.original_launch
16+
}
17+
if (req.query.missions) {
18+
query.missions = req.query.missions
19+
}
20+
if (req.query.landings) {
21+
query.landings = parseInt(req.query.landings)
22+
}
23+
if (req.query.type) {
24+
query.type = req.query.type
25+
}
26+
return query
27+
}

utilities/core-query.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Returns core query object from optional querystring inputs
3+
*/
4+
5+
exports.coreQuery = (req) => {
6+
let query = {}
7+
8+
if (req.query.core_serial) {
9+
query.core_serial = req.query.core_serial
10+
}
11+
if (req.query.status) {
12+
query.status = req.query.status
13+
}
14+
if (req.query.original_launch) {
15+
query.original_launch = req.query.original_launch
16+
}
17+
if (req.query.missions) {
18+
query.missions = req.query.missions
19+
}
20+
if (req.query.rtls_attempt) {
21+
query.rtls_attempt = Boolean(req.query.rtls_attempt)
22+
}
23+
if (req.query.rtls_landings) {
24+
query.rtls_landings = parseInt(req.query.rtls_landings)
25+
}
26+
if (req.query.asds_attempt) {
27+
query.asds_attempt = Boolean(req.query.asds_attempt)
28+
}
29+
if (req.query.asds_landings) {
30+
query.asds_landings = parseInt(req.query.asds_landings)
31+
}
32+
if (req.query.water_landing) {
33+
query.water_landing = Boolean(req.query.water_landing)
34+
}
35+
return query
36+
}

utilities/launch-query.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* Returns query object from optional querystring inputs
2+
* Returns launch query object from optional querystring inputs
33
*/
44

5-
exports.queryBuilder = (req) => {
5+
exports.launchQuery = (req) => {
66
let query = {}
77

88
if (req.query.start && req.query.final) {

v2-routes/v2-launches.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ v2.get("/latest", (req, res, next) => {
1515
})
1616
})
1717

18-
// All past launches filtered by any query string
18+
// All past launches filtered by any querystring
1919
v2.get("/", (req, res, next) => {
20-
let query = launch.queryBuilder(req)
20+
let query = launch.launchQuery(req)
2121
global.db.collection("launch").find(query,{"_id": 0 }).sort({"flight_number": 1})
2222
.toArray((err, doc) => {
2323
if (err) {

v2-routes/v2-parts.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
const express = require("express")
44
const v2 = express.Router()
5+
const cores = require("../utilities/core-query")
6+
const caps = require("../utilities/capsule-query")
57
const error = {error: "No results found"}
68

79
// Returns all capsule information
810
v2.get("/caps", (req, res, next) => {
9-
global.db.collection("capsule").find({},{"_id": 0}).sort({"capsule_serial": 1})
11+
let query = caps.capsuleQuery(req)
12+
global.db.collection("capsule").find(query,{"_id": 0}).sort({"capsule_serial": 1})
1013
.toArray((err, doc) => {
1114
if (err) {
1215
return next(err)
@@ -33,7 +36,8 @@ v2.get("/caps/:cap", (req, res, next) => {
3336

3437
// Returns all core information
3538
v2.get("/cores", (req, res, next) => {
36-
global.db.collection("core").find({},{"_id": 0}).sort({"core_serial": 1})
39+
let query = cores.coreQuery(req)
40+
global.db.collection("core").find(query,{"_id": 0}).sort({"core_serial": 1})
3741
.toArray((err, doc) => {
3842
if (err) {
3943
return next(err)

v2-routes/v2-upcoming.js

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,18 @@
22

33
const express = require("express")
44
const v2 = express.Router()
5+
const launch = require("../utilities/launch-query")
56

67
// Upcoming launches by date, year, or all
78
v2.get("/", (req, res, next) => {
8-
const year = req.query.year
9-
const start = req.query.start
10-
const final = req.query.final
11-
if (year) {
12-
global.db.collection("upcoming").find({"launch_year": `${year}`}, {"_id": 0 }).sort({"flight_number": -1})
13-
.toArray((err, doc) => {
14-
res.json(doc)
15-
})
16-
} else if (start && final) {
17-
global.db.collection("upcoming").find({ "launch_date_utc": {"$gte": `${start}T00:00:00Z`, "$lte": `${final}T00:00:00Z`}}, {"_id": 0 })
18-
.sort({"flight_number": 1})
19-
.toArray((err, doc) => {
20-
res.json(doc)
21-
})
22-
} else {
23-
global.db.collection("upcoming").find({},{"_id": 0 }).sort({"flight_number": 1})
24-
.toArray((err, doc) => {
25-
if (err) {
26-
return next(err)
27-
}
28-
res.json(doc)
29-
})
30-
}
9+
let query = launch.launchQuery(req)
10+
global.db.collection("upcoming").find(query,{"_id": 0 }).sort({"flight_number": 1})
11+
.toArray((err, doc) => {
12+
if (err) {
13+
return next(err)
14+
}
15+
res.json(doc)
16+
})
3117
})
3218

3319
module.exports = v2

0 commit comments

Comments
 (0)