From b14e900f0292fcaa2a4b17df6a674ffcd8f4a7af Mon Sep 17 00:00:00 2001 From: kko3093 Date: Thu, 21 Aug 2025 04:14:40 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=EA=B2=80=EC=83=89=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=A0=95=EB=A0=AC=20=EA=B8=B0=EB=8A=A5=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/itemController.ts | 48 ++++++++++++++++++------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/controllers/itemController.ts b/src/controllers/itemController.ts index 8be3d83..cdeb90b 100644 --- a/src/controllers/itemController.ts +++ b/src/controllers/itemController.ts @@ -109,54 +109,62 @@ function extractInitials(str: string) { } export const getSearchResults = async (req: Request, res: Response) => { - try { + try { const page = parseInt(req.query.page as string) || 1; const limit = parseInt(req.query.len as string) || 20; const sort = (req.query.sort as string) || "latest"; const keyword = (req.query.keyword as string) || ""; const offset = (page - 1) * limit; - // 정렬 매핑 - let orderBy = "release_ko DESC"; // 기본 최신순 - if (sort === "title") orderBy = "display_title ASC"; - if (sort === "price_asc") orderBy = "list_price ASC"; - if (sort === "price_desc") orderBy = "list_price DESC"; + // 정렬 매핑 (화이트리스트) + const sortMap: Record = { + latest: "release_ko DESC", + title: "display_title ASC", + price_asc: "list_price ASC", + price_desc: "list_price DESC", + }; + const orderBy = sortMap[sort] ?? sortMap.latest; const normalizedKeyword = extractInitials(keyword); + // 데이터 조회 (정렬 포함) const [rows] = await pool.query( - SELECT * - FROM new_view - WHERE display_title LIKE ? OR initials LIKE ? - LIMIT ? OFFSET ? - `, + ` + SELECT * + FROM new_view + WHERE display_title LIKE ? OR initials LIKE ? + ORDER BY ${orderBy} + LIMIT ? OFFSET ? + `, [`%${keyword}%`, `%${normalizedKeyword}%`, limit, offset] ); - const [countResult] = await pool.query( + // 총 개수 + const [countRows] = await pool.query( ` - SELECT COUNT(*) as count - FROM new_view - WHERE display_title LIKE ? OR initials LIKE ? - `, + SELECT COUNT(*) as count + FROM new_view + WHERE display_title LIKE ? OR initials LIKE ? + `, [`%${keyword}%`, `%${normalizedKeyword}%`] ); - const totalCount = (countResult as any)[0].count; + + const totalCount = (countRows as any)[0].count as number; const totalPages = Math.ceil(totalCount / limit); res.json({ books: rows, currentPage: page, - totalPages: totalPages, - totalCount: totalCount, + totalPages, + totalCount, }); } catch (error) { console.error("Error fetching books:", error); res.status(500).json({ error: "Internal Server Error" }); } - }; + export const getAutosearchResults = async (req: Request, res: Response) => { try { const keyword = (req.query.keyword as string) || "";