Skip to content

Commit

Permalink
Merge pull request #282 from TripInfoWeb/dev_diary
Browse files Browse the repository at this point in the history
Refactor: 일기 수정 및 삭제 API를 Path Parameter 방식으로 변경
  • Loading branch information
HyunJinNo authored Sep 10, 2024
2 parents 6c0db6c + 6de0f24 commit 550d618
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 57 deletions.
58 changes: 58 additions & 0 deletions src/app/api/diary/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -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;
}
54 changes: 1 addition & 53 deletions src/app/api/diary/route.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion src/containers/diary/detail/DiaryDeleteModalContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
Expand Down
5 changes: 2 additions & 3 deletions src/containers/diary/edit/DiaryEditorContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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();
};

Expand Down

0 comments on commit 550d618

Please sign in to comment.