6.2 최근/인기 글 리스트 및 무한스크롤 연동 o

This commit is contained in:
koreacomp5
2025-10-09 16:31:46 +09:00
parent b1557851ab
commit cdafc2908f
4 changed files with 78 additions and 5 deletions

View File

@@ -37,6 +37,7 @@ const listQuerySchema = z.object({
pageSize: z.coerce.number().min(1).max(100).default(10),
boardId: z.string().optional(),
q: z.string().optional(),
sort: z.enum(["recent", "popular"]).default("recent").optional(),
});
export async function GET(req: Request) {
@@ -45,7 +46,7 @@ export async function GET(req: Request) {
if (!parsed.success) {
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
}
const { page, pageSize, boardId, q } = parsed.data;
const { page, pageSize, boardId, q, sort = "recent" } = parsed.data;
const where = {
...(boardId ? { boardId } : {}),
...(q
@@ -62,7 +63,10 @@ export async function GET(req: Request) {
prisma.post.count({ where }),
prisma.post.findMany({
where,
orderBy: [{ isPinned: "desc" }, { createdAt: "desc" }],
orderBy:
sort === "popular"
? [{ isPinned: "desc" }, { stat: { recommendCount: "desc" } }, { createdAt: "desc" }]
: [{ isPinned: "desc" }, { createdAt: "desc" }],
skip: (page - 1) * pageSize,
take: pageSize,
select: {
@@ -72,11 +76,12 @@ export async function GET(req: Request) {
boardId: true,
isPinned: true,
status: true,
stat: { select: { recommendCount: true, views: true, commentsCount: true } },
},
}),
]);
return NextResponse.json({ total, page, pageSize, items });
return NextResponse.json({ total, page, pageSize, items, sort });
}