Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] 토너먼트 전체조회 API mock #1089 #1091

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions pages/api/pingpong/tournaments/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { NextApiRequest, NextApiResponse } from 'next';

const dummyUser = {
id: 2147,
intraId: 'jincpark',
imageUrl:
'https://42gg-public-image.s3.ap-northeast-2.amazonaws.com/images/jincpark.jpeg',
statusMessage: 'I am fine, thank you. And you?',
roleType: 'USER',
};

const dummyFinishedRookieTournament = {
tournamentId: 5,
title: '5회 루키 토너먼트',
contents: '블라블라',
startTime: '2023-11-11',
endTime: '2023-11-11',
status: '종료',
type: 'rookie',
winner: `${dummyUser}`,
player_cnt: 8,
};

const dummyFinishedMasterTournament = {
tournamentId: 6,
title: '5회 마스터 토너먼트',
contents: '블라블라',
startTime: '2023-11-11',
endTime: '2023-11-11',
status: '종료',
type: 'master',
winner: `${dummyUser}`,
player_cnt: 8,
};

const dummyRunningRookieTournament = {
tournamentId: 7,
title: '6회 루키 토너먼트',
contents: '블라블라',
startTime: '2023-11-11',
endTime: '2023-11-11',
status: '진행중',
type: 'rookie',
winner: `${dummyUser}`,
player_cnt: 8,
};

const dummyRunningMasterTournament = {
tournamentId: 8,
title: '6회 마스터 토너먼트',
contents: '블라블라',
startTime: '2023-11-11',
endTime: '2023-11-11',
status: '진행중',
type: 'master',
winner: `${dummyUser}`,
player_cnt: 8,
};

const dummyExpectedRookieTournament = {
tournamentId: 9,
title: '7회 루키 토너먼트',
contents: '블라블라',
startTime: '2023-11-11',
endTime: '2023-11-11',
status: '예정',
type: 'rookie',
winner: `${dummyUser}`,
player_cnt: 8,
};

const dummyExpectedMasterTournament = {
tournamentId: 10,
title: '7회 마스터 토너먼트',
contents: '블라블라',
startTime: '2023-11-11',
endTime: '2023-11-11',
status: '예정',
type: 'master',
winner: `${dummyUser}`,
player_cnt: 8,
};

const dummyTournaments = [
dummyFinishedRookieTournament,
dummyFinishedMasterTournament,
dummyRunningRookieTournament,
dummyRunningMasterTournament,
dummyExpectedRookieTournament,
dummyExpectedMasterTournament,
dummyFinishedRookieTournament,
dummyFinishedMasterTournament,
dummyRunningRookieTournament,
dummyRunningMasterTournament,
dummyExpectedRookieTournament,
dummyExpectedMasterTournament,
dummyFinishedRookieTournament,
dummyFinishedMasterTournament,
dummyRunningRookieTournament,
dummyRunningMasterTournament,
dummyExpectedRookieTournament,
dummyExpectedMasterTournament,
dummyFinishedRookieTournament,
dummyFinishedMasterTournament,
dummyRunningRookieTournament,
dummyRunningMasterTournament,
dummyExpectedRookieTournament,
dummyExpectedMasterTournament,
];

export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { page, type, status, size } = req.query as {
page: string;
type: string;
status: string;
size: string;
};

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

let filteredTournaments = dummyTournaments;
if (type || status) {
filteredTournaments = dummyTournaments.filter((tournament) => {
return (
(!type || tournament.type === type) &&
(!status || tournament.status === status)
);
});
}

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

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

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

res.status(200).json({
tournaments: filteredTournaments,
totalPage: totalPage,
});
}