From a15b62f7853641b9b1a38684196bbc770d4a8e94 Mon Sep 17 00:00:00 2001 From: koreacomp5 Date: Thu, 9 Oct 2025 16:54:24 +0900 Subject: [PATCH] =?UTF-8?q?7.2=20=EB=AA=A9=EB=A1=9D=20=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=95/=EC=A0=95=EB=A0=AC/=EA=B2=80=EC=83=89(=EC=A0=9C?= =?UTF-8?q?=EB=AA=A9/=ED=83=9C=EA=B7=B8/=EC=9E=91=EC=84=B1=EC=9E=90/?= =?UTF-8?q?=EA=B8=B0=EA=B0=84)=20o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/posts/route.ts | 24 +++++++++++++++++++++++- src/app/components/PostList.tsx | 6 +++++- src/app/search/page.tsx | 7 ++++--- todolist.txt | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/app/api/posts/route.ts b/src/app/api/posts/route.ts index e91d48c..7c49915 100644 --- a/src/app/api/posts/route.ts +++ b/src/app/api/posts/route.ts @@ -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([ diff --git a/src/app/components/PostList.tsx b/src/app/components/PostList.tsx index 879738b..73281b0 100644 --- a/src/app/components/PostList.tsx +++ b/src/app/components/PostList.tsx @@ -20,7 +20,7 @@ type Resp = { const fetcher = (url: string) => fetch(url).then((r) => r.json()); -export function PostList({ boardId, sort = "recent", q }: { boardId?: string; sort?: "recent" | "popular"; q?: string }) { +export function PostList({ boardId, sort = "recent", q, tag, author, start, end }: { boardId?: string; sort?: "recent" | "popular"; q?: string; tag?: string; author?: string; start?: string; end?: string }) { const pageSize = 10; const getKey = (index: number, prev: Resp | null) => { if (prev && prev.items.length === 0) return null; @@ -28,6 +28,10 @@ export function PostList({ boardId, sort = "recent", q }: { boardId?: string; so const sp = new URLSearchParams({ page: String(page), pageSize: String(pageSize), sort }); if (boardId) sp.set("boardId", boardId); if (q) sp.set("q", q); + if (tag) sp.set("tag", tag); + if (author) sp.set("author", author); + if (start) sp.set("start", start); + if (end) sp.set("end", end); return `/api/posts?${sp.toString()}`; }; const { data, size, setSize, isLoading } = useSWRInfinite(getKey, fetcher); diff --git a/src/app/search/page.tsx b/src/app/search/page.tsx index b373d6e..e25ad82 100644 --- a/src/app/search/page.tsx +++ b/src/app/search/page.tsx @@ -1,12 +1,13 @@ import { PostList } from "@/app/components/PostList"; -export default function SearchPage({ searchParams }: { searchParams?: { q?: string; sort?: "recent" | "popular" } }) { +export default function SearchPage({ searchParams }: { searchParams?: { q?: string; sort?: "recent" | "popular"; tag?: string; author?: string; start?: string; end?: string } }) { const q = searchParams?.q ?? ""; const sort = searchParams?.sort ?? "recent"; + const { tag = "", author = "", start = "", end = "" } = searchParams ?? {}; return (
-

검색: {q || "전체"}

- +

검색: {q || tag || author || (start && end ? `${start}~${end}` : "전체")}

+
); } diff --git a/todolist.txt b/todolist.txt index 133184e..a09023f 100644 --- a/todolist.txt +++ b/todolist.txt @@ -41,7 +41,7 @@ [게시판/컨텐츠] 7.1 게시글 CRUD API 및 페이지 연동 o -7.2 목록 페이징/정렬/검색(제목/태그/작성자/기간) +7.2 목록 페이징/정렬/검색(제목/태그/작성자/기간) o 7.3 태그/카테고리 모델 및 UI 7.4 첨부 업로드 및 본문 삽입 7.5 추천/신고, 조회수 카운트