Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtered get /catalog route to take in req.query w/ fields title, subject, event_type, and year #43

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading