Skip to content

Commit b36c073

Browse files
authored
Add pagination to getAllWallets (#336)
* Add pagination to getAllWallets * Update
1 parent 8e1f44e commit b36c073

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/db/wallets/getAllWallets.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ import { getPrismaWithPostgresTx } from "../client";
33

44
interface GetAllWalletsParams {
55
pgtx?: PrismaTransaction;
6+
page: number;
7+
limit: number;
68
}
79

810
// TODO: Add error logging handler from req.log to all queries
9-
export const getAllWallets = async ({ pgtx }: GetAllWalletsParams = {}) => {
11+
export const getAllWallets = async ({
12+
pgtx,
13+
page,
14+
limit,
15+
}: GetAllWalletsParams) => {
1016
const prisma = getPrismaWithPostgresTx(pgtx);
1117

12-
const wallets = await prisma.walletDetails.findMany();
13-
return wallets;
18+
return prisma.walletDetails.findMany({
19+
skip: (page - 1) * limit,
20+
take: limit,
21+
});
1422
};

src/server/routes/backend-wallet/getAll.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@ import {
77
walletDetailsSchema,
88
} from "../../schemas/sharedApiSchemas";
99

10-
// OUTPUT
10+
const QuerySchema = Type.Object({
11+
page: Type.Number({
12+
description: "The page of wallets to get.",
13+
examples: ["1"],
14+
default: "1",
15+
}),
16+
limit: Type.Number({
17+
description: "The number of wallets to get per page.",
18+
examples: ["10"],
19+
default: "10",
20+
}),
21+
});
22+
1123
const responseSchema = Type.Object({
1224
result: Type.Array(walletDetailsSchema),
1325
});
@@ -30,6 +42,7 @@ responseSchema.example = {
3042
};
3143
export async function getAll(fastify: FastifyInstance) {
3244
fastify.route<{
45+
Querystring: Static<typeof QuerySchema>;
3346
Reply: Static<typeof responseSchema>;
3447
}>({
3548
method: "GET",
@@ -39,15 +52,19 @@ export async function getAll(fastify: FastifyInstance) {
3952
description: "Get all backend wallets.",
4053
tags: ["Backend Wallet"],
4154
operationId: "getAll",
55+
querystring: QuerySchema,
4256
response: {
4357
...standardResponseSchema,
4458
[StatusCodes.OK]: responseSchema,
4559
},
4660
},
47-
handler: async (request, reply) => {
48-
const wallets = await getAllWallets();
61+
handler: async (req, res) => {
62+
const wallets = await getAllWallets({
63+
page: req.query.page,
64+
limit: req.query.limit,
65+
});
4966

50-
reply.status(StatusCodes.OK).send({
67+
res.status(StatusCodes.OK).send({
5168
result: wallets,
5269
});
5370
},

0 commit comments

Comments
 (0)