Skip to content

Commit

Permalink
feat(api): add pagination on get request (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayllyz authored Apr 15, 2024
1 parent eadb042 commit 80a9650
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
9 changes: 7 additions & 2 deletions apps/api/src/handlers/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/routes/blog.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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',
Expand Down Expand Up @@ -185,6 +189,7 @@ export const getComments = createRoute({
description: 'Get comments on a post',
request: {
params: idParamValidator,
query: paginationSchema,
},
responses: {
200: {
Expand Down
14 changes: 14 additions & 0 deletions apps/api/src/utils/pagnination.ts
Original file line number Diff line number Diff line change
@@ -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 };
};

0 comments on commit 80a9650

Please sign in to comment.