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

feat(api): add pagination on get request #66

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 };
};
Loading