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

34 catalog pagination backend #42

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const isISODate = (str) => {
}
};

// dependency for catalog.js
const isInteger = (value) => {
return value && /^\d+$/.test(value);
};

const isObject = (o) => {
return o === Object(o) && !isArray(o) && typeof o !== 'function' && !isISODate(o);
};
Expand Down Expand Up @@ -52,4 +57,4 @@ const keysToCamel = (data) => {
return data;
};

module.exports = { keysToCamel };
module.exports = { keysToCamel, isInteger };
29 changes: 23 additions & 6 deletions routes/catalog.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const express = require('express');

const { db } = require('../server/db');

const catalogRouter = express.Router();
const { keysToCamel } = require('../common/utils');
// modify /catalog
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 query = 'SELECT * FROM catalog WHERE 1=1';

michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
let { limit, page } = req.query;
limit = isInteger(limit) ? parseInt(limit, 10) : 10;
page = isInteger(page) ? parseInt(page, 10) : 1;

const offset = (page - 1) * limit;

michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
let query = 'FROM catalog WHERE 1=1';

const params = [];

if (title) {
Expand Down Expand Up @@ -46,11 +53,21 @@ catalogRouter.get('/', async (req, res) => {
} else {
params.push('');
}
params.push(limit);
params.push(offset);

query += ' ORDER BY title ASC';

michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
let countQuery = 'SELECT COUNT(*) ' + query + ';';
michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
const eventCount = await db.query(query, params);

michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
query += ' LIMIT $6 OFFSET $7;';
query = 'SELECT * ' + query;
michellelin1 marked this conversation as resolved.
Show resolved Hide resolved

const reqInfo = await db.query(query, params);
res.status(200).json(keysToCamel(reqInfo));

michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
res.status(200).json(keysToCamel({ events: reqInfo, count: eventCount }));

michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
res.status(500).send(err.message);
}
Expand Down Expand Up @@ -135,4 +152,4 @@ catalogRouter.delete('/:id', async (req, res) => {
}
});

module.exports = catalogRouter;
module.exports = catalogRouter;
michellelin1 marked this conversation as resolved.
Show resolved Hide resolved
Loading