Skip to content

Commit

Permalink
update suggestion fetch to paginate with offset
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonnorsworthy committed Sep 12, 2024
1 parent 962abd2 commit 66d572e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
3 changes: 2 additions & 1 deletion controllers/suggestion.controller .ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import suggestionService from "../services/suggestion.service";
export default {
getSuggestions: async (request: Request, response: Response) => {
try {
const suggestions = await suggestionService.getSuggestions();
const { page } = request.query;
const suggestions = await suggestionService.getSuggestions(Number(page));

if (!suggestions || suggestions.length === 0) {
return response.status(404).send('No suggestions found');
Expand Down
1 change: 0 additions & 1 deletion middleware/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const validateParams = (schema: any) => {

export const validateQuery = (schema: any) => {
return (request: Request, response: Response, next: NextFunction) => {
console.log(request.query)
const { error } = schema.validate(request.query);
if (error) {
return response.status(400).json({ error: error.details[0].message });
Expand Down
13 changes: 11 additions & 2 deletions routes/suggestion.router.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import express from 'express';
import suggestionController from '../controllers/suggestion.controller ';
import { validateBody } from '../middleware/validate';
import { validateBody, validateQuery } from '../middleware/validate';
import suggestionSchema from '../validationSchemas/suggestionSchema';
import isAdmin from '../middleware/isAdmin';

const suggestionRouter = express.Router();

suggestionRouter.get('/', suggestionController.getSuggestions);
// public routes
suggestionRouter.post(
'/',
validateBody(suggestionSchema.suggestion),
suggestionController.createSuggestion
);

// admin routes
suggestionRouter.get(
'/',
isAdmin,
validateQuery(suggestionSchema.allSuggestions),
suggestionController.getSuggestions
);

export default suggestionRouter;
29 changes: 26 additions & 3 deletions services/suggestion.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import { executeQuery } from "../database/connection"

export default {
getSuggestions: async () => {
const query = `SELECT * FROM suggestions;`;
getSuggestions: async (offset: number = 1) => {
const query = `
WITH suggestions_with_user AS (
SELECT
s.id,
s.title,
s.description,
u.username
FROM
suggestions s
JOIN
users u ON s.user_id = u.id
)
SELECT
id,
title,
description,
username
FROM
suggestions_with_user
ORDER BY
id
LIMIT 20 OFFSET (($1 - 1) * 20);
`;
const values = [offset]

return await executeQuery(query);
return await executeQuery(query, values);
},

createSuggestion: async (userId: string, title: string, description: string) => {
Expand Down
7 changes: 7 additions & 0 deletions validationSchemas/suggestionSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import Joi from 'joi';

export default {
allSuggestions: Joi.object({
page: Joi.number()
.min(1)
.integer()
.required()
}),

suggestion: Joi.object({
title: Joi.string()
.min(1)
Expand Down

0 comments on commit 66d572e

Please sign in to comment.