Skip to content

Commit

Permalink
Merge branch 'develop' into community_list_mock/#47
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokyeom committed Jul 15, 2022
2 parents 4cdc951 + 5d98cf7 commit 18f4572
Show file tree
Hide file tree
Showing 12 changed files with 1,563 additions and 47 deletions.
679 changes: 679 additions & 0 deletions mocks/data/communityMockData.ts

Large diffs are not rendered by default.

647 changes: 647 additions & 0 deletions mocks/data/toyMockData.ts

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions mocks/data/userMockData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { UserData } from '../../types/user';
export const userMockData: UserData[] = [
{
id: 'happhee',
sns_id: '[email protected]',
provider: 'naver',
nickname: 'happhee',
email: '[email protected]',
},
];
31 changes: 0 additions & 31 deletions mocks/handlers.ts

This file was deleted.

69 changes: 69 additions & 0 deletions mocks/handlers/community.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { rest } from 'msw';
import { communityMockData } from '../data/communityMockData';
import {
CommunityData,
PostCommentBody,
PostCommunityBody,
} from '../../types/community';
// 커뮤니티 게시글 작성
export const postCommunity = rest.post('/board', (req, res, ctx) => {
const { category, title, content, imageList } = req.body as PostCommunityBody;
const addId = communityMockData.length + 1 + '';

communityMockData.push({
id: addId,
category: category,
title: title,
content: content,
imageList: imageList,
});
return res(ctx.json(communityMockData));
});
// 커뮤니티 게시글 수정
export const putCommunity = rest.put('/board/:boardId', (req, res, ctx) => {
const { boardId } = req.params;
});

// 커뮤니티 게시글 상세조회
export const getCommunityDetail = rest.get(
'/board/:boardId',
(req, res, ctx) => {
const { boardId } = req.params;

const communityDetail = communityMockData.filter(
(community) => community.id === boardId,
);

return res(ctx.json(communityDetail));
},
);
// 커뮤니티 게시글 리스트 조회
export const getCommunityList = rest.get('/board', (req, res, ctx) => {
return res(ctx.json(communityMockData));
});
// 커뮤니티 댓글 작성
export const postComment = rest.post('/board/comment', (req, res, ctx) => {
const { boardId, content } = req.body as PostCommentBody;

const currentComment: CommunityData[] = communityMockData.filter(
(community) => community.id === boardId,
);
currentComment[0].replyList?.push({ content: content });
communityMockData[Number(boardId) - 1].replyList =
currentComment[0].replyList;

return res(ctx.json(communityMockData));
});
// 커뮤니티 게시글 삭제
export const deleteCommunity = rest.delete(
'/board/:boardId',
(req, res, ctx) => {
const { boardId } = req.params;

const newCommunityMockData = communityMockData.filter(
(community) => community.id !== boardId,
);

return res(ctx.json(newCommunityMockData));
},
);
9 changes: 9 additions & 0 deletions mocks/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as toyHandlers from './toy';
import * as communityHandlers from './community';
import * as loginHandlers from './user';

export const handlers = [
...Object.values(toyHandlers),
...Object.values(communityHandlers),
...Object.values(loginHandlers),
];
52 changes: 52 additions & 0 deletions mocks/handlers/toy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { rest } from 'msw';
import { toyMockData } from '../data/toyMockData';
import { ToyData } from '../../types/toy';

// 장난감 리스트 조회 (메인뷰)
export const getHome = rest.get('/home', (req, res, ctx) =>
res(ctx.json(toyMockData)),
);

// 장난감 컬렉션 별 조회
export const getToyCollection = rest.get('/collection', (req, res, ctx) => {
const theme = req.url.searchParams.get('theme');
const sort = req.url.searchParams.get('sort');

// 낮은 가격 순
function priceDesc(a: ToyData, b: ToyData) {
if (a.price == b.price) {
return 0;
}
return a.price > b.price ? 1 : -1;
}
// 높은 가격 순
function priceAsc(a: ToyData, b: ToyData) {
if (a.price == b.price) {
return 0;
}
return a.price < b.price ? 1 : -1;
}
return sort === 'price-desc'
? res(ctx.json(toyMockData.sort(priceDesc)))
: res(ctx.json(toyMockData.sort(priceAsc)));
});

// 장난감 검색 및 필터 리스트 조회
// 필터 검증 로직은 영이 로직 짜고 생각해봐야할 것같아
export const getToyList = rest.get('/toy/list', (req, res, ctx) => {
const search = req.url.searchParams.get('search');
const type = req.url.searchParams.get('type');
const month = req.url.searchParams.getAll('month');
const price = req.url.searchParams.get('price');
const playHow = req.url.searchParams.get('playHow');
const category = req.url.searchParams.get('category');

const toyList = toyMockData.filter(
(toy) =>
toy.title.includes(`${search}`) &&
toy.type === Number(type) &&
toy.price <= Number(price) &&
toy.playHow === Number(playHow),
);
return res(ctx.json(toyList));
});
45 changes: 45 additions & 0 deletions mocks/handlers/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { rest } from 'msw';
import { userMockData } from '../data/userMockData';
import { PostSignUpBody } from '../../types/user';
// 카카오 로그인
export const getKakao = rest.get(
'/auth/login/kakao/:nickname',
(req, res, ctx) => {
const { nickname } = req.params;
const user = userMockData.filter(
(user) => user.provider === 'kakao' && user.nickname === nickname,
);
return res(ctx.json(user));
},
);
// 구글 로그인
export const getGoogle = rest.get(
'/auth/login/google/:nickname',
(req, res, ctx) => {
const { nickname } = req.params;
const user = userMockData.filter(
(user) => user.provider === 'google' && user.nickname === nickname,
);
return res(ctx.json(user));
},
);

// 네이버 로그인
export const getNaver = rest.get(
'/auth/login/naver/:nickname',
(req, res, ctx) => {
const { nickname } = req.params;
const user = userMockData.filter(
(user) => user.provider === 'naver' && user.nickname === nickname,
);
return res(ctx.json(user));
},
);

// 회원가입
export const signUp = rest.post('/auth/signup', (req, res, ctx) => {
const { nickname } = req.body as PostSignUpBody;
const userList = userMockData.filter((user) => user.nickname === nickname);

return userList.length === 0 ? res(ctx.status(201)) : res(ctx.json(409));
});
16 changes: 0 additions & 16 deletions types/api.ts

This file was deleted.

32 changes: 32 additions & 0 deletions types/community.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 댓글
export interface ReplyData {
userNickname?: string;
content: string;
createdAt?: string;
}
// 커뮤니티 데이터
export interface CommunityData {
id: string;
category: string;
title: string;
content: string;
userNickname?: string;
replyCount?: number;
createdAt?: string;
image?: string;
imageList?: string[];
replyList?: ReplyData[];
}
// 커뮤니티 게시글 작성 post body
export interface PostCommunityBody {
category: string;
title: string;
content: string;
imageList?: string[];
}

// 커뮤니티 댓글
export interface PostCommentBody {
boardId: string;
content: string;
}
10 changes: 10 additions & 0 deletions types/toy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface ToyData {
price: number; //가격
priceCode: number;
image: string; //이미지
siteUrl: string;
title: string;
month: Array<number>; //연령
playHow: number;
type: number;
}
10 changes: 10 additions & 0 deletions types/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface UserData {
id: string;
sns_id: string;
provider: string;
nickname: string;
email: string;
}
export interface PostSignUpBody {
nickname: string;
}

0 comments on commit 18f4572

Please sign in to comment.