Skip to content

Commit

Permalink
feat: content 핸들러 구현 #442
Browse files Browse the repository at this point in the history
  • Loading branch information
rbgksqkr committed Dec 26, 2024
1 parent 521d24b commit c70377f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
3 changes: 1 addition & 2 deletions frontend-admin/src/constants/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export const API_URL = {
export const MOCK_API_URL = {
login: `${BASE_URL}/api/admin/login`,
logout: `${BASE_URL}/api/admin/logout`,
balanceContent: (category: string) =>
`${BASE_URL}/api/admin/balances/contents?category=${category}`,
balanceContent: `${BASE_URL}/api/admin/balances/contents?category=:category`,
contents: `${BASE_URL}/api/admin/balances/contents`,
options: `${BASE_URL}/api/admin/balances/options`,
deleteContent: `${BASE_URL}/api/admin/balances/contents/:contentId`,
Expand Down
98 changes: 98 additions & 0 deletions frontend-admin/src/mocks/handler/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { http, HttpResponse } from "msw";

import BALANCE_CONTENT from "../data/balanceContent.json";

import { MOCK_API_URL } from "@/constants/url";

const fetchBalanceContentHandler = () => {
return HttpResponse.json(BALANCE_CONTENT);
};

const appendContentHandler = async ({ request }: { request: Request }) => {
const body = await request.json();

const content = { ...BALANCE_CONTENT.contents[0] };

const newContent = {
contentId: ++content.contentId,
question: body.question,
firstOption: {
optionId: ++content.firstOption.optionId,
name: body.firstOption,
count: 0,
percent: 50,
},
secondOption: {
optionId: ++content.secondOption.optionId,
name: body.secondOption,
count: 0,
percent: 50,
},
};

return HttpResponse.json(newContent);
};

const editQuestionHandler = async ({ request }: { request: Request }) => {
const body = await request.json();

const content = BALANCE_CONTENT.contents.find(
(content) => content.contentId === body.contentId
);

if (!content) return new HttpResponse(null, { status: 404 });

content.firstOption.name = body.name;

return HttpResponse.json(content);
};

const editOptionHandler = async ({ request }: { request: Request }) => {
const body = await request.json();

const firstContent = BALANCE_CONTENT.contents.find(
(content) => content.firstOption.optionId === body.optionId
);

if (firstContent) {
firstContent.firstOption.name = body.name;
return HttpResponse.json(firstContent);
}

const secondContent = BALANCE_CONTENT.contents.find(
(content) => content.secondOption.optionId === body.optionId
);

if (secondContent) {
secondContent.firstOption.name = body.name;
return HttpResponse.json(secondContent);
}

return new HttpResponse(null, { status: 404 });
};

const deleteContentHandler = async ({ request }: { request: Request }) => {
const url = new URL(request.url);

const contentId = url.searchParams.get("contentId");

if (!contentId) {
return new HttpResponse(null, { status: 404 });
}

const filteredContent = BALANCE_CONTENT.contents.filter(
(content) => content.contentId !== Number(contentId)
);

BALANCE_CONTENT.contents = filteredContent;

return new HttpResponse(null, { status: 204 });
};

export const contentHandlers = [
http.get(MOCK_API_URL.balanceContent, fetchBalanceContentHandler),
http.post(MOCK_API_URL.contents, appendContentHandler),
http.patch(MOCK_API_URL.contents, editQuestionHandler),
http.patch(MOCK_API_URL.options, editOptionHandler),
http.delete(MOCK_API_URL.deleteContent, deleteContentHandler),
];

0 comments on commit c70377f

Please sign in to comment.