7.2 목록 페이징/정렬/검색(제목/태그/작성자/기간) o

This commit is contained in:
koreacomp5
2025-10-09 16:54:24 +09:00
parent 7342c9bea2
commit a15b62f785
4 changed files with 33 additions and 6 deletions

View File

@@ -38,6 +38,10 @@ const listQuerySchema = z.object({
boardId: z.string().optional(),
q: z.string().optional(),
sort: z.enum(["recent", "popular"]).default("recent").optional(),
tag: z.string().optional(), // Tag.slug
author: z.string().optional(), // User.nickname contains
start: z.coerce.date().optional(), // createdAt >= start
end: z.coerce.date().optional(), // createdAt <= end
});
export async function GET(req: Request) {
@@ -46,7 +50,7 @@ export async function GET(req: Request) {
if (!parsed.success) {
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
}
const { page, pageSize, boardId, q, sort = "recent" } = parsed.data;
const { page, pageSize, boardId, q, sort = "recent", tag, author, start, end } = parsed.data;
const where = {
NOT: { status: "deleted" as const },
...(boardId ? { boardId } : {}),
@@ -58,6 +62,24 @@ export async function GET(req: Request) {
],
}
: {}),
...(tag
? {
postTags: { some: { tag: { slug: tag } } },
}
: {}),
...(author
? {
author: { nickname: { contains: author } },
}
: {}),
...(start || end
? {
createdAt: {
...(start ? { gte: start } : {}),
...(end ? { lte: end } : {}),
},
}
: {}),
} as const;
const [total, items] = await Promise.all([