Skip to content

Commit

Permalink
✨ 피드 작성, 수정 모킹 API Handler 추가 (#16)
Browse files Browse the repository at this point in the history
* feat: 피드 수정, 작성 handler 구현

* feat: 피드 수정, 작성 Request DTO를 entities/feed/model/types.ts 에 적용

* feat: entities/feed/model/types.ts를 common.type, report.type, write.type ts파일로 분리
  • Loading branch information
suhwan2004 authored Nov 7, 2024
1 parent 8a064d1 commit b4b120b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
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, isBlind } = (await request.json()) as IReportFeedReqDto;
const { category, isBlind } = (await request.json()) as IReportFeedReqDTO;

if (!feedId || !category) {
return createHttpErrorResponse('피드 ID, 신고 카테고리가 필수로 입력되어야 합니다.');
Expand All @@ -29,4 +28,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({});
}),
];
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export interface IFeed {
createdAt: string;
updatedAt: string;
}

export type TFeedScope = 'public' | 'friends' | 'private';
4 changes: 3 additions & 1 deletion src/entities/feed/model/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './type';
export * from './common.type';
export * from './report.type';
export * from './write.type';
5 changes: 5 additions & 0 deletions src/entities/feed/model/report.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface IReportFeedReqDTO {
category: string;
content: string;
isBlind: boolean;
}
7 changes: 7 additions & 0 deletions src/entities/feed/model/write.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TFeedScope } from './common.type';

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

0 comments on commit b4b120b

Please sign in to comment.