221 lines
6.0 KiB
TypeScript
221 lines
6.0 KiB
TypeScript
|
|
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 curriculumId = searchParams.get('curriculumId');
|
||
|
|
const registrantId = searchParams.get('registrantId');
|
||
|
|
|
||
|
|
if (id) {
|
||
|
|
// 단일 강좌 조회
|
||
|
|
const lecture = await prisma.lecture.findUnique({
|
||
|
|
where: { id },
|
||
|
|
include: {
|
||
|
|
curriculum: true,
|
||
|
|
registrant: {
|
||
|
|
select: {
|
||
|
|
id: true,
|
||
|
|
name: true,
|
||
|
|
email: true,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
enrolledUsers: {
|
||
|
|
include: {
|
||
|
|
user: {
|
||
|
|
select: {
|
||
|
|
id: true,
|
||
|
|
name: true,
|
||
|
|
email: true,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!lecture) {
|
||
|
|
return NextResponse.json({ error: '강좌를 찾을 수 없습니다.' }, { status: 404 });
|
||
|
|
}
|
||
|
|
|
||
|
|
return NextResponse.json(lecture);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 전체 강좌 목록 조회
|
||
|
|
let where: any = {};
|
||
|
|
if (curriculumId) {
|
||
|
|
where.curriculumId = curriculumId;
|
||
|
|
}
|
||
|
|
if (registrantId) {
|
||
|
|
where.registrantId = registrantId;
|
||
|
|
}
|
||
|
|
|
||
|
|
const lectures = await prisma.lecture.findMany({
|
||
|
|
where,
|
||
|
|
include: {
|
||
|
|
curriculum: {
|
||
|
|
select: {
|
||
|
|
id: true,
|
||
|
|
title: true,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
registrant: {
|
||
|
|
select: {
|
||
|
|
id: true,
|
||
|
|
name: true,
|
||
|
|
email: true,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
orderBy: {
|
||
|
|
createdAt: 'desc',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json(lectures);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error fetching lectures:', error);
|
||
|
|
return NextResponse.json({ error: '강좌 조회 중 오류가 발생했습니다.' }, { status: 500 });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// POST - 강좌 생성
|
||
|
|
export async function POST(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const body = await request.json();
|
||
|
|
const { title, attachmentFile, evaluationQuestionCount, curriculumId, registrantId } = body;
|
||
|
|
|
||
|
|
if (!title || !curriculumId || !registrantId) {
|
||
|
|
return NextResponse.json({ error: '강좌명, 교육 과정 ID, 등록자 ID는 필수입니다.' }, { status: 400 });
|
||
|
|
}
|
||
|
|
|
||
|
|
// 교육 과정 존재 확인
|
||
|
|
const curriculum = await prisma.curriculum.findUnique({
|
||
|
|
where: { id: curriculumId },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!curriculum) {
|
||
|
|
return NextResponse.json({ error: '교육 과정을 찾을 수 없습니다.' }, { status: 404 });
|
||
|
|
}
|
||
|
|
|
||
|
|
// 등록자 존재 확인
|
||
|
|
const registrant = await prisma.user.findUnique({
|
||
|
|
where: { id: registrantId },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!registrant) {
|
||
|
|
return NextResponse.json({ error: '등록자를 찾을 수 없습니다.' }, { status: 404 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const lecture = await prisma.lecture.create({
|
||
|
|
data: {
|
||
|
|
title,
|
||
|
|
attachmentFile: attachmentFile || null,
|
||
|
|
evaluationQuestionCount: evaluationQuestionCount || 0,
|
||
|
|
curriculumId,
|
||
|
|
registrantId,
|
||
|
|
},
|
||
|
|
include: {
|
||
|
|
curriculum: true,
|
||
|
|
registrant: {
|
||
|
|
select: {
|
||
|
|
id: true,
|
||
|
|
name: true,
|
||
|
|
email: true,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json(lecture, { status: 201 });
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error creating lecture:', error);
|
||
|
|
return NextResponse.json({ error: '강좌 생성 중 오류가 발생했습니다.' }, { status: 500 });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// PUT - 강좌 수정
|
||
|
|
export async function PUT(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const body = await request.json();
|
||
|
|
const { id, title, attachmentFile, evaluationQuestionCount, curriculumId, registrantId } = body;
|
||
|
|
|
||
|
|
if (!id) {
|
||
|
|
return NextResponse.json({ error: '강좌 ID는 필수입니다.' }, { status: 400 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const updateData: any = {};
|
||
|
|
if (title !== undefined) updateData.title = title;
|
||
|
|
if (attachmentFile !== undefined) updateData.attachmentFile = attachmentFile;
|
||
|
|
if (evaluationQuestionCount !== undefined) updateData.evaluationQuestionCount = parseInt(evaluationQuestionCount);
|
||
|
|
if (curriculumId !== undefined) {
|
||
|
|
// 교육 과정 존재 확인
|
||
|
|
const curriculum = await prisma.curriculum.findUnique({
|
||
|
|
where: { id: curriculumId },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!curriculum) {
|
||
|
|
return NextResponse.json({ error: '교육 과정을 찾을 수 없습니다.' }, { status: 404 });
|
||
|
|
}
|
||
|
|
|
||
|
|
updateData.curriculumId = curriculumId;
|
||
|
|
}
|
||
|
|
if (registrantId !== undefined) {
|
||
|
|
// 등록자 존재 확인
|
||
|
|
const registrant = await prisma.user.findUnique({
|
||
|
|
where: { id: registrantId },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!registrant) {
|
||
|
|
return NextResponse.json({ error: '등록자를 찾을 수 없습니다.' }, { status: 404 });
|
||
|
|
}
|
||
|
|
|
||
|
|
updateData.registrantId = registrantId;
|
||
|
|
}
|
||
|
|
|
||
|
|
const lecture = await prisma.lecture.update({
|
||
|
|
where: { id },
|
||
|
|
data: updateData,
|
||
|
|
include: {
|
||
|
|
curriculum: true,
|
||
|
|
registrant: {
|
||
|
|
select: {
|
||
|
|
id: true,
|
||
|
|
name: true,
|
||
|
|
email: true,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json(lecture);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error updating lecture:', 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.lecture.delete({
|
||
|
|
where: { id },
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json({ message: '강좌가 삭제되었습니다.' });
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error deleting lecture:', error);
|
||
|
|
return NextResponse.json({ error: '강좌 삭제 중 오류가 발생했습니다.' }, { status: 500 });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|