From 08961a33c8a4d5446c555c704e222a2df93273fb Mon Sep 17 00:00:00 2001 From: Cute_Wisp Date: Sun, 23 Feb 2025 09:28:20 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20handle=20pagenation=20in?= =?UTF-8?q?=20auction=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/auction/route.ts | 64 ++++++++++++++++++++++++++---------- src/app/auction/page.tsx | 4 +-- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/src/app/api/auction/route.ts b/src/app/api/auction/route.ts index 1cf66fb..5374fa2 100644 --- a/src/app/api/auction/route.ts +++ b/src/app/api/auction/route.ts @@ -15,29 +15,57 @@ export async function GET(request: Request) { const auctionItemCategory = searchParams.get("auction_item_category"); const itemName = searchParams.get("item_name"); - let url = `${NXOPEN_API_URL}/mabinogi/v1/auction/list?`; - if (auctionItemCategory) { - url += `auction_item_category=${auctionItemCategory}&`; - } - if (itemName) { - url += `item_name=${encodeURIComponent(itemName)}`; - } + let allItems: any[] = []; + let nextCursor: string | null = ""; + + try { + do { + let url = `${NXOPEN_API_URL}/mabinogi/v1/auction/list?`; + if (auctionItemCategory) { + url += `auction_item_category=${auctionItemCategory}&`; + } + if (itemName) { + url += `item_name=${encodeURIComponent(itemName)}&`; + } + if (nextCursor) { + url += `cursor=${nextCursor}`; + } + + const response = await fetch(url, { + headers: { + "Content-Type": "application/json", + "x-nxopen-api-key": NXOPEN_API_KEY || "", + }, + }); - const response = await fetch(url, { - headers: { - "Content-Type": "application/json", - "x-nxopen-api-key": NXOPEN_API_KEY || "", - }, - }); + if (!response.ok) { + console.error(await response.json()); + return NextResponse.json( + { error: "Failed to fetch data" }, + { status: 500 } + ); + } - if (!response.ok) { - console.error(await response.json()); + const data = await response.json(); + + // 결과 데이터 누적 + if (data.auction_item.length > 0) { + allItems = [...allItems, ...data.auction_item]; + } + + // 다음 페이지를 위한 cursor 업데이트 + nextCursor = data.next_cursor; + } while (nextCursor !== null); + + // 전체 데이터 반환 + return NextResponse.json({ + items: allItems, + }); + } catch (error) { + console.error("Error fetching auction data:", error); return NextResponse.json( { error: "Failed to fetch data" }, { status: 500 } ); } - - const data = await response.json(); - return NextResponse.json(data); } diff --git a/src/app/auction/page.tsx b/src/app/auction/page.tsx index 8856c30..41f610b 100644 --- a/src/app/auction/page.tsx +++ b/src/app/auction/page.tsx @@ -67,11 +67,11 @@ export default function AuctionPage() { } const data = await response.json(); - data.auction_item.sort( + data.items.sort( (a: any, b: any) => a.auction_price_per_unit - b.auction_price_per_unit ); - setFilteredItems(data.auction_item); + setFilteredItems(data.items); setErrorMessage(null); } catch (error) { console.error("API 호출 중 오류가 발생했습니다:", error);