Skip to content

Commit 492c83c

Browse files
committed
modified get route to take in req.query w/ fields title, subject, event_type, season, and year
1 parent 13b0cae commit 492c83c

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

routes/catalog.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,47 @@ const { keysToCamel } = require('../common/utils');
88
// -- GET - Returns all data from the catalog table
99
catalogRouter.get('/', async (req, res) => {
1010
try {
11-
const allInfo = await db.query(`SELECT * from catalog;`);
12-
res.status(200).json(keysToCamel(allInfo));
11+
const { title, eventType, subject, season, year } = req.query;
12+
let query = 'SELECT * FROM catalog WHERE 1=1';
13+
const params = [];
14+
15+
if (title) {
16+
query += ' AND title = $1';
17+
params.push(title);
18+
} else {
19+
params.push('');
20+
}
21+
22+
if (subject) {
23+
query += ' AND subject = $2';
24+
params.push(subject);
25+
} else {
26+
params.push('');
27+
}
28+
29+
if (eventType) {
30+
query += ' AND event_type = $3';
31+
params.push(eventType);
32+
} else {
33+
params.push('');
34+
}
35+
36+
if (season) {
37+
query += ' AND season = $4';
38+
params.push(season);
39+
} else {
40+
params.push('');
41+
}
42+
43+
if (year) {
44+
query += ' AND year = $5';
45+
params.push(year);
46+
} else {
47+
params.push('');
48+
}
49+
50+
const reqInfo = await db.query(query, params);
51+
res.status(200).json(keysToCamel(reqInfo));
1352
} catch (err) {
1453
res.status(500).send(err.message);
1554
}

0 commit comments

Comments
 (0)