feat: 标准化响应体
This commit is contained in:
@@ -66,19 +66,8 @@ export const updateProjectSchema = z.object({
|
||||
*/
|
||||
export const listProjectQuerySchema = z
|
||||
.object({
|
||||
page: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.min(1, { message: '页码必须大于0' })
|
||||
.optional()
|
||||
.default(1),
|
||||
limit: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.min(1, { message: '每页数量必须大于0' })
|
||||
.max(100, { message: '每页数量不能超过100' })
|
||||
.optional()
|
||||
.default(10),
|
||||
page: z.coerce.number().int().min(1).optional(),
|
||||
pageSize: z.coerce.number().int().min(1).max(100).optional(),
|
||||
name: z.string().optional(),
|
||||
})
|
||||
.optional();
|
||||
|
||||
@@ -29,27 +29,30 @@ export class ProjectController {
|
||||
};
|
||||
}
|
||||
|
||||
const isPagination = query?.page !== undefined && query?.pageSize !== undefined;
|
||||
|
||||
const [total, projects] = await Promise.all([
|
||||
prisma.project.count({ where: whereCondition }),
|
||||
prisma.project.findMany({
|
||||
where: whereCondition,
|
||||
skip: query ? (query.page - 1) * query.limit : 0,
|
||||
take: query?.limit,
|
||||
skip: isPagination ? (query.page! - 1) * query.pageSize! : 0,
|
||||
take: isPagination ? query.pageSize : undefined,
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
data: projects,
|
||||
pagination: {
|
||||
page: query?.page || 1,
|
||||
limit: query?.limit || 10,
|
||||
if (isPagination) {
|
||||
return {
|
||||
list: projects,
|
||||
page: query.page,
|
||||
pageSize: query.pageSize,
|
||||
total,
|
||||
totalPages: Math.ceil(total / (query?.limit || 10)),
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
return projects;
|
||||
}
|
||||
|
||||
// GET /api/projects/:id - 获取单个项目
|
||||
|
||||
Reference in New Issue
Block a user