import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; // GET - 교육 과정 목록 조회 또는 단일 교육 과정 조회 export async function GET(request: NextRequest) { try { const { searchParams } = new URL(request.url); const id = searchParams.get('id'); const instructorId = searchParams.get('instructorId'); if (id) { // 단일 교육 과정 조회 const curriculum = await prisma.curriculum.findUnique({ where: { id }, include: { lectures: { include: { registrant: { select: { id: true, name: true, email: true, }, }, }, }, }, }); if (!curriculum) { return NextResponse.json({ error: '교육 과정을 찾을 수 없습니다.' }, { status: 404 }); } return NextResponse.json(curriculum); } // 전체 교육 과정 목록 조회 let where: any = {}; if (instructorId) { where.instructorId = instructorId; } const curriculums = await prisma.curriculum.findMany({ where, include: { lectures: { select: { id: true, title: true, }, }, }, orderBy: { createdAt: 'desc', }, }); return NextResponse.json(curriculums); } catch (error) { console.error('Error fetching curriculums:', error); return NextResponse.json({ error: '교육 과정 조회 중 오류가 발생했습니다.' }, { status: 500 }); } } // POST - 교육 과정 생성 export async function POST(request: NextRequest) { try { const body = await request.json(); const { title, instructorId } = body; if (!title || !instructorId) { return NextResponse.json({ error: '과정 제목과 강사 ID는 필수입니다.' }, { status: 400 }); } // 강사 존재 확인 const instructor = await prisma.user.findUnique({ where: { id: instructorId }, }); if (!instructor) { return NextResponse.json({ error: '강사를 찾을 수 없습니다.' }, { status: 404 }); } const curriculum = await prisma.curriculum.create({ data: { title, instructorId, }, include: { lectures: true, }, }); return NextResponse.json(curriculum, { status: 201 }); } catch (error) { console.error('Error creating curriculum:', error); return NextResponse.json({ error: '교육 과정 생성 중 오류가 발생했습니다.' }, { status: 500 }); } } // PUT - 교육 과정 수정 export async function PUT(request: NextRequest) { try { const body = await request.json(); const { id, title, instructorId } = body; if (!id) { return NextResponse.json({ error: '교육 과정 ID는 필수입니다.' }, { status: 400 }); } const updateData: any = {}; if (title !== undefined) updateData.title = title; if (instructorId !== undefined) { // 강사 존재 확인 const instructor = await prisma.user.findUnique({ where: { id: instructorId }, }); if (!instructor) { return NextResponse.json({ error: '강사를 찾을 수 없습니다.' }, { status: 404 }); } updateData.instructorId = instructorId; } const curriculum = await prisma.curriculum.update({ where: { id }, data: updateData, include: { lectures: true, }, }); return NextResponse.json(curriculum); } catch (error) { console.error('Error updating curriculum:', error); return NextResponse.json({ error: '교육 과정 수정 중 오류가 발생했습니다.' }, { status: 500 }); } } // DELETE - 교육 과정 삭제 export async function DELETE(request: NextRequest) { try { const { searchParams } = new URL(request.url); const id = searchParams.get('id'); if (!id) { return NextResponse.json({ error: '교육 과정 ID는 필수입니다.' }, { status: 400 }); } await prisma.curriculum.delete({ where: { id }, }); return NextResponse.json({ message: '교육 과정이 삭제되었습니다.' }); } catch (error) { console.error('Error deleting curriculum:', error); return NextResponse.json({ error: '교육 과정 삭제 중 오류가 발생했습니다.' }, { status: 500 }); } }