Skip to content

Commit

Permalink
Filtered get /catalog route to take in req.query w/ fields title, sub…
Browse files Browse the repository at this point in the history
…ject, event_type, and year (#43)

* init branch

* modified get route to take in req.query w/ fields title, subject, event_type, season, and year

* ordered by title in ascending order

* = changed to ILIKE op for title input

---------

Co-authored-by: subinqkim <[email protected]>
  • Loading branch information
ctc-devops and SubinQKim authored Feb 5, 2024
1 parent 1fd18d9 commit 3198903
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions routes/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,53 @@ const { db } = require('../server/db');

const catalogRouter = express.Router();
const { keysToCamel } = require('../common/utils');

// modify /catalog
// -- GET - Returns all data from the catalog table
catalogRouter.get('/', async (req, res) => {
try {
const allInfo = await db.query(`SELECT * from catalog;`);
res.status(200).json(keysToCamel(allInfo));
const { title, eventType, subject, season, year } = req.query;
let query = 'SELECT * FROM catalog WHERE 1=1';
const params = [];

if (title) {
query += ' AND title ILIKE $1';
params.push(`%${title}%`);
} else {
params.push('');
}

if (subject) {
query += ' AND subject = $2';
params.push(subject);
} else {
params.push('');
}

if (eventType) {
query += ' AND event_type = $3';
params.push(eventType);
} else {
params.push('');
}

if (season) {
query += ' AND season = $4';
params.push(season);
} else {
params.push('');
}

if (year) {
query += ' AND year = $5';
params.push(year);
} else {
params.push('');
}

query += ' ORDER BY title ASC';

const reqInfo = await db.query(query, params);
res.status(200).json(keysToCamel(reqInfo));
} catch (err) {
res.status(500).send(err.message);
}
Expand Down

0 comments on commit 3198903

Please sign in to comment.