feat: get documents api route with pagination

This commit is contained in:
Catalin Pit
2023-11-22 15:44:49 +02:00
parent 24d9906557
commit 4a6b3edc05
4 changed files with 58 additions and 6 deletions

View File

@ -0,0 +1,21 @@
import { prisma } from '@documenso/prisma';
type GetDocumentsProps = {
page: number;
perPage: number;
};
export const getDocuments = async ({ page = 1, perPage = 10 }: GetDocumentsProps) => {
const [documents, count] = await Promise.all([
await prisma.document.findMany({
take: perPage,
skip: Math.max(page - 1, 0) * perPage,
}),
await prisma.document.count(),
]);
return {
documents,
totalPages: Math.ceil(count / perPage),
};
};

View File

@ -4,19 +4,19 @@ import { z } from 'zod';
const c = initContract();
const GetDocumentsQuery = z.object({
take: z.string().default('10'),
skip: z.string().default('0'),
page: z.string().optional(),
perPage: z.string().optional(),
});
const DocumentSchema = z.object({
id: z.string(),
id: z.number(),
userId: z.number(),
title: z.string(),
status: z.string(),
documentDataId: z.string(),
createdAt: z.string(),
updatedAt: z.string(),
completedAt: z.string(),
createdAt: z.date(),
updatedAt: z.date(),
completedAt: z.date().nullable(),
});
export const contract = c.router({

View File

@ -0,0 +1,3 @@
import { createNextRoute, createNextRouter } from '@ts-rest/next';
export { createNextRoute, createNextRouter };