Skip to content

Commit

Permalink
feat: 🎸 handle pagenation in auction api
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatpotato13 committed Feb 23, 2025
1 parent af5533d commit 08961a3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
64 changes: 46 additions & 18 deletions src/app/api/auction/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions src/app/auction/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 08961a3

Please sign in to comment.