Skip to content

Commit

Permalink
Merge branch 'dev' into 34-catalog-pagination-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatMegamind authored Feb 5, 2024
2 parents 317f0cf + 9527087 commit a99ea3a
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 4 deletions.
61 changes: 57 additions & 4 deletions routes/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,67 @@ const { keysToCamel, isInteger } = require('../common/utils');
// -- GET - Returns all data from the catalog table
catalogRouter.get('/', async (req, res) => {
try {
const { title, eventType, subject, season, year } = req.query;

let { limit, page } = req.query;
limit = isInteger(limit) ? parseInt(limit, 10) : 10;
page = isInteger(page) ? parseInt(page, 10) : 1;

const offset = (page - 1) * limit;
const allInfo = await db.query(`SELECT * from catalog LIMIT $1 OFFSET $2;`, [limit, offset]);
const eventCount = await db.query(`SELECT COUNT(DISTINCT id) from catalog;`);
res.status(200).json(keysToCamel({ events: allInfo, count: eventCount }));

let query = '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('');
}
params.push(limit);
params.push(offset);

query += ' ORDER BY title ASC';

let countQuery = 'SELECT COUNT(*) ' + query + ';';
const eventCount = await db.query(query, params);

query += ' LIMIT $6 OFFSET $7;';
query = 'SELECT * ' + query;

const reqInfo = await db.query(query, params);

res.status(200).json(keysToCamel({ events: reqInfo, count: eventCount }));

} catch (err) {
res.status(500).send(err.message);
}
Expand Down Expand Up @@ -99,4 +152,4 @@ catalogRouter.delete('/:id', async (req, res) => {
}
});

module.exports = catalogRouter;
module.exports = catalogRouter;
86 changes: 86 additions & 0 deletions routes/publishedSchedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,92 @@ publishedScheduleRouter.get('/', async (req, res) => {
}
});

// GET /published-schedule/season - returns rows that match the season
publishedScheduleRouter.get('/season', async (req, res) => {
try {
let startTime;
let endTime;

const { season, year } = req.query;

// getting the intervals for each season
if (season.toLowerCase() === 'winter') {
startTime = `${year - 1}-12-01`;
endTime = `${year}-02-29`;
} else if (season.toLowerCase() === 'spring') {
startTime = `${year}-03-01`;
endTime = `${year}-05-31`;
} else if (season.toLowerCase() === 'summer') {
startTime = `${year}-06-01`;
endTime = `${year}-08-31`;
} else {
startTime = `${year}-09-01`;
endTime = `${year}-11-30`;
}

const seasonResult = await db.query(
`
WITH seasonPS AS
(
SELECT
PS.id,
C.title,
C.event_type,
C.year,
PS.start_time,
PS.end_time,
PS.confirmed,
PS.confirmed_on,
PS.cohort,
PS.notes
FROM published_schedule PS
LEFT JOIN catalog C ON PS.event_id = C.id
WHERE
DATE(start_time) >= $1::date AND DATE(start_time) <= $2::date
)
SELECT DATE(seasonPS.start_time), JSON_AGG(seasonPS.*) AS data
FROM seasonPS
GROUP BY DATE(start_time)
ORDER BY DATE(start_time) ASC;
`,
[startTime, endTime],
);
res.status(200).json(keysToCamel(seasonResult));
} catch (err) {
res.status(400).send(err.message);
}
});

// GET /published-schedule/date - returns all events occurring on a specific date
publishedScheduleRouter.get('/date', async (req, res) => {
try {
const { date } = req.query;
const seasonResult = await db.query(
`
SELECT
PS.id,
C.title,
C.event_type,
C.year,
PS.start_time,
PS.end_time,
PS.confirmed,
PS.confirmed_on,
PS.cohort,
PS.notes
FROM published_schedule PS
LEFT JOIN catalog C ON PS.event_id = C.id
WHERE DATE(PS.start_time) = $1
ORDER BY start_time ASC;
`,
[date],
);
res.status(200).json(keysToCamel(seasonResult));
} catch (err) {
res.status(400).send(err.message);
}
});

// GET/:id - returns the rows that match the given id
publishedScheduleRouter.get('/:id', async (req, res) => {
try {
Expand Down

0 comments on commit a99ea3a

Please sign in to comment.