235 lines
6.1 KiB
TypeScript
235 lines
6.1 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 userId = searchParams.get('userId');
|
|
const lectureId = searchParams.get('lectureId');
|
|
|
|
if (id) {
|
|
// 단일 수강 조회
|
|
const userLecture = await prisma.userLecture.findUnique({
|
|
where: { id },
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
},
|
|
},
|
|
lecture: {
|
|
include: {
|
|
curriculum: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!userLecture) {
|
|
return NextResponse.json({ error: '수강 정보를 찾을 수 없습니다.' }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json(userLecture);
|
|
}
|
|
|
|
// 수강 목록 조회
|
|
let where: any = {};
|
|
if (userId) {
|
|
where.userId = userId;
|
|
}
|
|
if (lectureId) {
|
|
where.lectureId = lectureId;
|
|
}
|
|
|
|
const userLectures = await prisma.userLecture.findMany({
|
|
where,
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
},
|
|
},
|
|
lecture: {
|
|
include: {
|
|
curriculum: {
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
orderBy: {
|
|
enrolledAt: 'desc',
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(userLectures);
|
|
} catch (error) {
|
|
console.error('Error fetching user lectures:', error);
|
|
return NextResponse.json({ error: '수강 정보 조회 중 오류가 발생했습니다.' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// POST - 수강 등록
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const { userId, lectureId } = body;
|
|
|
|
if (!userId || !lectureId) {
|
|
return NextResponse.json({ error: '사용자 ID와 강좌 ID는 필수입니다.' }, { status: 400 });
|
|
}
|
|
|
|
// 사용자 존재 확인
|
|
const user = await prisma.user.findUnique({
|
|
where: { id: userId },
|
|
});
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: '사용자를 찾을 수 없습니다.' }, { status: 404 });
|
|
}
|
|
|
|
// 강좌 존재 확인
|
|
const lecture = await prisma.lecture.findUnique({
|
|
where: { id: lectureId },
|
|
});
|
|
|
|
if (!lecture) {
|
|
return NextResponse.json({ error: '강좌를 찾을 수 없습니다.' }, { status: 404 });
|
|
}
|
|
|
|
// 이미 수강 중인지 확인
|
|
const existingEnrollment = await prisma.userLecture.findUnique({
|
|
where: {
|
|
userId_lectureId: {
|
|
userId,
|
|
lectureId,
|
|
},
|
|
},
|
|
});
|
|
|
|
if (existingEnrollment) {
|
|
return NextResponse.json({ error: '이미 수강 중인 강좌입니다.' }, { status: 409 });
|
|
}
|
|
|
|
// 수강 등록
|
|
const userLecture = await prisma.userLecture.create({
|
|
data: {
|
|
userId,
|
|
lectureId,
|
|
progress: 0,
|
|
isCompleted: false,
|
|
},
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
},
|
|
},
|
|
lecture: {
|
|
include: {
|
|
curriculum: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(userLecture, { status: 201 });
|
|
} catch (error) {
|
|
console.error('Error enrolling lecture:', error);
|
|
return NextResponse.json({ error: '수강 등록 중 오류가 발생했습니다.' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// PUT - 수강 정보 수정 (진행률, 완료 여부 등)
|
|
export async function PUT(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const { id, progress, isCompleted, score } = body;
|
|
|
|
if (!id) {
|
|
return NextResponse.json({ error: '수강 ID는 필수입니다.' }, { status: 400 });
|
|
}
|
|
|
|
const updateData: any = {};
|
|
if (progress !== undefined) {
|
|
const progressValue = parseInt(progress);
|
|
if (progressValue < 0 || progressValue > 100) {
|
|
return NextResponse.json({ error: '진행률은 0-100 사이여야 합니다.' }, { status: 400 });
|
|
}
|
|
updateData.progress = progressValue;
|
|
}
|
|
if (isCompleted !== undefined) {
|
|
updateData.isCompleted = isCompleted;
|
|
if (isCompleted && !updateData.completedAt) {
|
|
updateData.completedAt = new Date();
|
|
} else if (!isCompleted) {
|
|
updateData.completedAt = null;
|
|
}
|
|
}
|
|
if (score !== undefined) {
|
|
const scoreValue = parseInt(score);
|
|
if (scoreValue < 0 || scoreValue > 100) {
|
|
return NextResponse.json({ error: '점수는 0-100 사이여야 합니다.' }, { status: 400 });
|
|
}
|
|
updateData.score = scoreValue;
|
|
}
|
|
|
|
const userLecture = await prisma.userLecture.update({
|
|
where: { id },
|
|
data: updateData,
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
},
|
|
},
|
|
lecture: {
|
|
include: {
|
|
curriculum: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(userLecture);
|
|
} catch (error) {
|
|
console.error('Error updating user 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.userLecture.delete({
|
|
where: { id },
|
|
});
|
|
|
|
return NextResponse.json({ message: '수강이 취소되었습니다.' });
|
|
} catch (error) {
|
|
console.error('Error deleting user lecture:', error);
|
|
return NextResponse.json({ error: '수강 취소 중 오류가 발생했습니다.' }, { status: 500 });
|
|
}
|
|
}
|
|
|