From 80a9650a60c48af6ce233b56b71c80fb2393133f Mon Sep 17 00:00:00 2001 From: Antony David Date: Mon, 15 Apr 2024 20:59:12 +0200 Subject: [PATCH] feat(api): add pagination on get request (#66) --- apps/api/src/handlers/blog.ts | 9 +++++++-- apps/api/src/routes/blog.ts | 5 +++++ apps/api/src/utils/pagnination.ts | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 apps/api/src/utils/pagnination.ts diff --git a/apps/api/src/handlers/blog.ts b/apps/api/src/handlers/blog.ts index 8a873623..173284da 100644 --- a/apps/api/src/handlers/blog.ts +++ b/apps/api/src/handlers/blog.ts @@ -14,6 +14,7 @@ import { updatePost, } from '../routes/blog.js'; import { checkRole } from '../utils/context.js'; +import { getPagination } from '../utils/pagnination.js'; import type { Variables } from '../validators/general.js'; import { Role } from '../validators/general.js'; @@ -22,7 +23,9 @@ export const blog = new OpenAPIHono<{ Variables: Variables }>({ }); blog.openapi(getAllPosts, async (c) => { - const { data, error } = await supabase.from('POSTS').select('*'); + const { skip, take } = c.req.valid('query'); + const { from, to } = getPagination(skip, take - 1); + const { data, error } = await supabase.from('POSTS').select('*').range(from, to); if (error) { return c.json({ error: error.message }, 500); @@ -122,7 +125,9 @@ blog.openapi(commentOnPost, async (c) => { blog.openapi(getComments, async (c) => { const { id } = c.req.valid('param'); - const { data, error } = await supabase.from('COMMENTS').select('*').eq('id_post', id); + const { skip, take } = c.req.valid('query'); + const { from, to } = getPagination(skip, take - 1); + const { data, error } = await supabase.from('COMMENTS').select('*').eq('id_post', id).range(from, to); if (error) { return c.json({ error: error.message }, 500); diff --git a/apps/api/src/routes/blog.ts b/apps/api/src/routes/blog.ts index 0a4e3ee0..b1032569 100644 --- a/apps/api/src/routes/blog.ts +++ b/apps/api/src/routes/blog.ts @@ -1,6 +1,7 @@ import { createRoute } from '@hono/zod-openapi'; import { number, z } from 'zod'; import authMiddleware from '../middlewares/auth.js'; +import { paginationSchema } from '../utils/pagnination.js'; import { commentSchema, insertCommentSchema, @@ -16,6 +17,9 @@ export const getAllPosts = createRoute({ path: '/posts', summary: 'Get all posts', description: 'Get all posts', + request: { + query: paginationSchema, + }, responses: { 200: { description: 'Successful response', @@ -185,6 +189,7 @@ export const getComments = createRoute({ description: 'Get comments on a post', request: { params: idParamValidator, + query: paginationSchema, }, responses: { 200: { diff --git a/apps/api/src/utils/pagnination.ts b/apps/api/src/utils/pagnination.ts new file mode 100644 index 00000000..250a88a8 --- /dev/null +++ b/apps/api/src/utils/pagnination.ts @@ -0,0 +1,14 @@ +import { z } from 'zod'; + +export const paginationSchema = z.object({ + skip: z.coerce.number().int().min(0).default(0), + take: z.coerce.number().int().min(1).default(10), +}); + +export const getPagination = (page: number, size: number): { from: number; to: number } => { + const limit = size ? size : 10; + const from = page ? page * limit : 0; + const to = page ? from + size : size; + + return { from, to }; +};