From 6de0f24b9f92172442c7537288b8806ce4255e7d Mon Sep 17 00:00:00 2001 From: HyunJinNo Date: Tue, 10 Sep 2024 16:29:38 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20=EC=9D=BC=EA=B8=B0=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EB=B0=8F=20=EC=82=AD=EC=A0=9C=20API=EB=A5=BC=20Pat?= =?UTF-8?q?h=20Parameter=20=EB=B0=A9=EC=8B=9D=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/diary/[id]/route.ts | 58 +++++++++++++++++++ src/app/api/diary/route.ts | 54 +---------------- .../detail/DiaryDeleteModalContainer.tsx | 2 +- .../diary/edit/DiaryEditorContainer.tsx | 5 +- 4 files changed, 62 insertions(+), 57 deletions(-) create mode 100644 src/app/api/diary/[id]/route.ts diff --git a/src/app/api/diary/[id]/route.ts b/src/app/api/diary/[id]/route.ts new file mode 100644 index 00000000..cb2c7623 --- /dev/null +++ b/src/app/api/diary/[id]/route.ts @@ -0,0 +1,58 @@ +import { UpdateDiaryRequestDto } from "@/types/DiaryDto"; +import { revalidateTag } from "next/cache"; +import { NextRequest } from "next/server"; + +/** + * 일기 수정 + */ +export async function PUT( + request: NextRequest, + { params }: { params: { id: string } }, +) { + const cookie = request.cookies.get("access_token"); + const body: UpdateDiaryRequestDto = await request.json(); + + const response = await fetch( + `${process.env.BACKEND_URL}/api/diary/${params.id}`, + { + method: "PUT", + headers: { + "Content-Type": "application/json", + Cookie: `${cookie?.name}=${cookie?.value}`, + }, + body: JSON.stringify(body), + cache: "no-store", + }, + ); + + if (!response.ok) { + throw new Error(response.statusText); + } + + revalidateTag("getDiaryList"); + revalidateTag(`getDiary/${params.id}`); + return response; +} + +/** + * 일기 삭제 + */ +export async function DELETE( + request: NextRequest, + { params }: { params: { id: string } }, +) { + const cookie = request.cookies.get("access_token"); + const response = await fetch( + `${process.env.BACKEND_URL}/api/diary/${params.id}`, + { + method: "DELETE", + headers: { + Cookie: `${cookie?.name}=${cookie?.value}`, + }, + cache: "no-store", + }, + ); + + revalidateTag("getDiaryList"); + return response; +} diff --git a/src/app/api/diary/route.ts b/src/app/api/diary/route.ts index 1b778ace..d5103dcf 100644 --- a/src/app/api/diary/route.ts +++ b/src/app/api/diary/route.ts @@ -1,4 +1,4 @@ -import { CreateDiaryRequestDto, UpdateDiaryRequestDto } from "@/types/DiaryDto"; +import { CreateDiaryRequestDto } from "@/types/DiaryDto"; import { revalidateTag } from "next/cache"; import { NextRequest } from "next/server"; @@ -20,55 +20,3 @@ export async function POST(request: NextRequest) { revalidateTag("getDiaryList"); return response; } - -/** - * 일기 수정 - */ -export async function PUT(request: NextRequest) { - const cookie = request.cookies.get("access_token"); - const diaryId = request.nextUrl.searchParams.get("diaryId"); - const body: UpdateDiaryRequestDto = await request.json(); - - const response = await fetch( - `${process.env.BACKEND_URL}/api/diary?diaryId=${diaryId}`, - { - method: "PUT", - headers: { - "Content-Type": "application/json", - Cookie: `${cookie?.name}=${cookie?.value}`, - }, - body: JSON.stringify(body), - cache: "no-store", - }, - ); - - if (!response.ok) { - throw new Error(response.statusText); - } - - revalidateTag("getDiaryList"); - revalidateTag(`getDiary/${diaryId}`); - return response; -} - -/** - * 일기 삭제 - */ -export async function DELETE(request: NextRequest) { - const cookie = request.cookies.get("access_token"); - const diaryId = request.nextUrl.searchParams.get("diaryId"); - - const response = await fetch( - `${process.env.BACKEND_URL}/api/diary?diaryId=${diaryId}`, - { - method: "DELETE", - headers: { - Cookie: `${cookie?.name}=${cookie?.value}`, - }, - cache: "no-store", - }, - ); - - revalidateTag("getDiaryList"); - return response; -} diff --git a/src/containers/diary/detail/DiaryDeleteModalContainer.tsx b/src/containers/diary/detail/DiaryDeleteModalContainer.tsx index 7a4a9b06..743a5dfd 100644 --- a/src/containers/diary/detail/DiaryDeleteModalContainer.tsx +++ b/src/containers/diary/detail/DiaryDeleteModalContainer.tsx @@ -16,7 +16,7 @@ const DiaryDeleteModalContainer = ({ diaryId, closeModal }: Props) => { const onDeleteClick = async () => { setLoading(true); - const response = await fetch(`/api/diary?diaryId=${diaryId}`, { + const response = await fetch(`/api/diary/${diaryId}`, { method: "DELETE", cache: "no-store", }); diff --git a/src/containers/diary/edit/DiaryEditorContainer.tsx b/src/containers/diary/edit/DiaryEditorContainer.tsx index bfe9bd1e..059d823f 100644 --- a/src/containers/diary/edit/DiaryEditorContainer.tsx +++ b/src/containers/diary/edit/DiaryEditorContainer.tsx @@ -110,7 +110,7 @@ const DiaryEditorContainer = ({ diaryData }: Props) => { setLoading(true); const response = await fetch( - `/api/diary?diaryId=${diaryData.diaryContentResponse.diaryId}`, + `/api/diary/${diaryData.diaryContentResponse.diaryId}`, { method: "PUT", headers: { @@ -127,8 +127,7 @@ const DiaryEditorContainer = ({ diaryData }: Props) => { throw new Error(response.statusText); } - const diaryId = await response.text(); - router.push(`/diary/${diaryId}`); + router.push(`/diary/${diaryData.diaryContentResponse.diaryId}`); router.refresh(); };