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

✨ 피드 작성, 수정 모킹 API Handler 추가 #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
65 changes: 59 additions & 6 deletions src/app/mocks/handlers/feed.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { http } from 'msw';

import { IReportFeedReqDTO, IWriteFeedReqDTO } from '@/entities/feed';

import { feedMockData } from '../data';
import { createHttpErrorResponse, createHttpSuccessResponse } from '../lib';

interface IReportFeedReqDto {
category: string;
content: string;
isBlind: boolean;
}
export const feedHandlers = [
// 1️⃣ 피드 신고
http.post('http://api.example.com/v2/feeds/:feedId/reports', async ({ request, params }) => {
const { feedId } = params;

const { category, content } = (await request.json()) as IReportFeedReqDto;
const { category, content } = (await request.json()) as IReportFeedReqDTO;

if (!feedId || !category || !content) {
return createHttpErrorResponse('피드 ID, 신고 카테고리, 컨텐츠가 다 입력되어야 합니다.');
Expand All @@ -21,4 +20,58 @@ export const feedHandlers = [
isReported: true,
});
}),

// 2️⃣ 피드 작성
http.post('http://api.example.com/v2/feeds', async ({ request }) => {
const { content, images, scope } = (await request.json()) as IWriteFeedReqDTO;

if (!content) {
return createHttpErrorResponse('피드 등록을 위해 컨텐츠를 작성해야 합니다.');
} else if (!scope) {
return createHttpErrorResponse('피드 등록을 위해 공개 범위를 설정해야 합니다.');
}

feedMockData.push({
id: feedMockData.length + 1,
user: { ...feedMockData[feedMockData.length - 1].user },
content,
images: images.map((url, index) => ({ id: index + 1, imageUrl: url })),
likeCount: 0,
commentCount: 0,
isLiked: false,
isBookmarked: false,
isBlinded: false,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
});

return createHttpSuccessResponse({});
}),

// 3️⃣ 피드 수정
http.put('http://api.example.com/v2/feeds/:feedId', async ({ request, params }) => {
const { feedId } = params;

const { content, images, scope } = (await request.json()) as IWriteFeedReqDTO;

if (!content) {
return createHttpErrorResponse('피드 수정을 위해 컨텐츠를 작성해야 합니다.');
} else if (!scope) {
return createHttpErrorResponse('피드 수정을 위해 공개 범위를 설정해야 합니다.');
}

feedMockData.forEach((feed) => {
const numFeedId = +feedId;
if (feed.id === numFeedId) {
feedMockData[numFeedId] = {
...feedMockData[numFeedId],
content,
images: images.map((url, index) => ({ id: index + 1, imageUrl: url })),
updatedAt: new Date().toISOString(),
};
}
});

return createHttpSuccessResponse({});
}),
];
1 change: 1 addition & 0 deletions src/entities/feed/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './model';
1 change: 1 addition & 0 deletions src/entities/feed/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './types';
13 changes: 13 additions & 0 deletions src/entities/feed/model/types.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P5: 피드 관련한 API가 많아서 DTO 및 기타 타입들이 많아질 것 같은데 혹시 파일별로 분리하는 건 어떻게 생각하시나요??

  • report.type.ts
  • write.type.ts

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface IReportFeedReqDTO {
category: string;
content: string;
isBlind: boolean;
}

export interface IWriteFeedReqDTO {
content: string;
images: string[];
scope: TFeedScope;
}

export type TFeedScope = 'public' | 'friends' | 'private';