Skip to content

Commit

Permalink
[Fix] 토너먼트전체조회 Mock API 페이지네이션 로직 수정 #1096
Browse files Browse the repository at this point in the history
  • Loading branch information
Clearsu committed Nov 16, 2023
1 parent 3646ac4 commit 808e936
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pages/api/pingpong/tournaments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {

if (!page) {
res.status(404).end('You must put page!!');
return;
}

let filteredTournaments = dummyTournaments;
Expand All @@ -130,18 +131,24 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
});
}

let SIZE = 20;
let sizeInt = 20;
if (size) {
SIZE = parseInt(size);
sizeInt = parseInt(size);
}

// 소수점이 있을 경우 올림
const totalPage = Math.ceil(filteredTournaments.length / SIZE);
const totalPage = Math.ceil(filteredTournaments.length / sizeInt);

if (parseInt(page) > totalPage) {
res.status(404).end('Page number exceeded');
return;
}

// page와 size에 맞게 slice
const startIndex = (parseInt(page) - 1) * sizeInt;
filteredTournaments = filteredTournaments.slice(
(parseInt(page) - 1) * SIZE,
SIZE
startIndex,
startIndex + sizeInt
);

res.status(200).json({
Expand Down

0 comments on commit 808e936

Please sign in to comment.