|
| 1 | +import { AxiosError } from "axios"; |
| 2 | +import { cookies } from "next/headers"; |
| 3 | +import { NextRequest, NextResponse } from "next/server"; |
| 4 | +import apiClient from "@/lib/apiClient"; |
| 5 | + |
| 6 | +// 지원하기 |
| 7 | +export async function POST(req: NextRequest, { params }: { params: { formId: string } }) { |
| 8 | + try { |
| 9 | + const accessToken = cookies().get("accessToken")?.value; |
| 10 | + |
| 11 | + if (!accessToken) { |
| 12 | + return NextResponse.json({ message: "Unauthorized" }, { status: 401 }); |
| 13 | + } |
| 14 | + |
| 15 | + const body = await req.json(); |
| 16 | + |
| 17 | + const response = await apiClient.post(`/forms/${params.formId}/applications`, body, { |
| 18 | + headers: { |
| 19 | + Authorization: `Bearer ${accessToken}`, |
| 20 | + }, |
| 21 | + }); |
| 22 | + |
| 23 | + return NextResponse.json(response.data); |
| 24 | + } catch (error: unknown) { |
| 25 | + if (error instanceof AxiosError) { |
| 26 | + console.error(`POST /api/forms/${params.formId}/applications error:`, error); |
| 27 | + if (error.response) { |
| 28 | + return NextResponse.json({ message: error.response.data.message }, { status: error.response.status }); |
| 29 | + } |
| 30 | + } |
| 31 | + return NextResponse.json({ message: "Internal Server Error" }, { status: 500 }); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// 지원 현황 목록 조회 |
| 36 | +export async function GET(req: NextRequest, { params }: { params: { formId: string } }) { |
| 37 | + try { |
| 38 | + const accessToken = cookies().get("accessToken")?.value; |
| 39 | + |
| 40 | + if (!accessToken) { |
| 41 | + return NextResponse.json({ message: "Unauthorized" }, { status: 401 }); |
| 42 | + } |
| 43 | + |
| 44 | + const { searchParams } = new URL(req.url); |
| 45 | + const queryParams = { |
| 46 | + page: searchParams.get("page"), |
| 47 | + limit: searchParams.get("limit"), |
| 48 | + status: searchParams.get("status"), |
| 49 | + }; |
| 50 | + |
| 51 | + const response = await apiClient.get(`/forms/${params.formId}/applications`, { |
| 52 | + params: queryParams, |
| 53 | + headers: { |
| 54 | + Authorization: `Bearer ${accessToken}`, |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + return NextResponse.json(response.data); |
| 59 | + } catch (error: unknown) { |
| 60 | + if (error instanceof AxiosError) { |
| 61 | + console.error(`GET /api/forms/${params.formId}/applications error:`, error); |
| 62 | + if (error.response) { |
| 63 | + return NextResponse.json({ message: error.response.data.message }, { status: error.response.status }); |
| 64 | + } |
| 65 | + } |
| 66 | + return NextResponse.json({ message: "Internal Server Error" }, { status: 500 }); |
| 67 | + } |
| 68 | +} |
0 commit comments