Skip to content

Commit

Permalink
feat: Paginate Posts
Browse files Browse the repository at this point in the history
  • Loading branch information
arbezerra committed Jun 29, 2023
1 parent 9f6b4ce commit e9a1a91
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
26 changes: 24 additions & 2 deletions controllers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,23 @@ import { v4 as uuid4 } from "uuid";

const PostController = {
index: async (req: Request, res: Response) => {
const items = await pool<Post>("posts");
return res.status(200).json(items);
const page = parseInt(req.query.page as string) || 0;
const size = parseInt(req.query.size as string) || 5;
const items = pool<Post>("posts")
.offset(page * size)
.limit(size)
.orderBy("date", "desc");
const total = pool<{ total: number }>("posts")
.count("id", { as: "total" })
.first();
return res.status(200).json({
items: await items,
paginate: {
page,
size,
total: (await total)?.total,
},
});
},
show: async (req: Request, res: Response) => {
const item = await pool<Post>("posts").where("id", req.params.id).first();
Expand Down Expand Up @@ -76,6 +91,13 @@ const PostController = {
slug: z.string().regex(/^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$/),
}),
});
case ValidateMethod.PAGINATE:
return z.object({
query: z.object({
page: z.string().optional().transform((x) => parseInt(x || "0")).pipe(z.number().nonnegative()),
size: z.string().optional().transform((x) => parseInt(x || "5")).pipe(z.number().positive()),
}),
});
default:
return z.object({});
}
Expand Down
1 change: 1 addition & 0 deletions middlewares/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum ValidateMethod {
ID,
SLUG,
LOGIN,
PAGINATE,
}

const validate = (schema: AnyZodObject) => {
Expand Down
1 change: 1 addition & 0 deletions routes/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const router = Router();

router.get(
"/",
validate(PostController.validate(ValidateMethod.PAGINATE)),
PostController.index
);
router.get(
Expand Down
12 changes: 6 additions & 6 deletions tests/post.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ describe("Post", () => {
});

test("GET /post/:id 200", async () => {
const result = await request(app).get("/post");
return request(app).get(`/post/${result.body[0].id}`).expect(200);
const post = await pool<Post>("posts").first();
return request(app).get(`/post/${post!.id}`).expect(200);
});

test("GET /post/slug/:slug 200", async () => {
const result = await request(app).get("/post");
const post = await pool<Post>("posts").first();
return request(app)
.get(`/post/slug/${result.body[0].slug}`)
.get(`/post/slug/${post!.slug}`)
.expect(200);
});

Expand Down Expand Up @@ -72,9 +72,9 @@ describe("Post", () => {
});

test("DELETE /post 200", async () => {
const result = await request(app).get("/post");
const post = await pool<Post>("posts").first();
return request(app)
.delete(`/post/${result.body[0].id}`)
.delete(`/post/${post!.id}`)
.set({ Authorization: `Bearer ${token}` })
.expect(200);
});
Expand Down

0 comments on commit e9a1a91

Please sign in to comment.