260617_01_ENH: 배정 테스트를 위해 개발 중도 커밋
This commit is contained in:
125
src/app/api/user/[[...slug]]/route.ts
Normal file
125
src/app/api/user/[[...slug]]/route.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
import {NextRequest, NextResponse} from "next/server";
|
||||
import {RouteContext} from "@/types/common";
|
||||
import {errorToResponse} from "@/lib/http-util";
|
||||
import {createBackApi} from "@/lib/server-api";
|
||||
import {ExcelUploadResult} from "@/types/common";
|
||||
import {UserFull} from "@/types/gwcf";
|
||||
|
||||
export const GET = async (
|
||||
req: NextRequest,
|
||||
context: RouteContext
|
||||
) => {
|
||||
try {
|
||||
const {slug = []} = await context.params;
|
||||
const [action] = slug;
|
||||
const backApi = await createBackApi();
|
||||
|
||||
if (action === "list" && slug.length === 2) {
|
||||
const result = await backApi.get<UserFull[]>(`/user/list/${encodeURIComponent(slug[1])}`);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
if (slug.length === 1 && action) {
|
||||
const result = await backApi.get<UserFull>(`/user/${encodeURIComponent(action)}`);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const POST = async (
|
||||
req: NextRequest,
|
||||
context: RouteContext
|
||||
) => {
|
||||
try {
|
||||
const {slug = []} = await context.params;
|
||||
const [action] = slug;
|
||||
const backApi = await createBackApi();
|
||||
|
||||
if (action === "excel-upload" && slug.length === 2) {
|
||||
const formData = await req.formData();
|
||||
const file = formData.get("file") as File | null;
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json({ message: "file field is required." }, { status: 400 });
|
||||
}
|
||||
|
||||
const backendForm = new FormData();
|
||||
backendForm.append("file", file, file.name);
|
||||
|
||||
const result = await backApi.postForm<ExcelUploadResult>(
|
||||
`/user/excel/upload/${encodeURIComponent(slug[1])}`,
|
||||
backendForm
|
||||
);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
if (action === "excel-template" && slug.length === 2) {
|
||||
const result = await backApi.post<ArrayBuffer>(
|
||||
`/user/excel/template/${encodeURIComponent(slug[1])}`,
|
||||
undefined,
|
||||
{ expectBinary: true }
|
||||
);
|
||||
|
||||
return new NextResponse(result, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"Content-Disposition": `attachment; filename="user_${slug[1]}_template.xlsx"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const PATCH = async (
|
||||
req: NextRequest,
|
||||
context: RouteContext
|
||||
) => {
|
||||
try {
|
||||
const {slug = []} = await context.params;
|
||||
const [action] = slug;
|
||||
const backApi = await createBackApi();
|
||||
|
||||
if (!action) {
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body) {
|
||||
return NextResponse.json({ message: "Request body is required." }, { status: 400 });
|
||||
}
|
||||
|
||||
await backApi.patch<void>("/user", body);
|
||||
return new NextResponse(null, { status: 204 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const DELETE = async (
|
||||
req: NextRequest,
|
||||
context: RouteContext
|
||||
) => {
|
||||
try {
|
||||
const {slug = []} = await context.params;
|
||||
const [userId] = slug;
|
||||
const backApi = await createBackApi();
|
||||
|
||||
if (userId && slug.length === 1) {
|
||||
await backApi.del<void>(`/user/${encodeURIComponent(userId)}`);
|
||||
return new NextResponse(null, { status: 204 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user