관리자 계정 설정, 관리자 페이지 작업1
This commit is contained in:
124
app/api/curriculums/[id]/route.ts
Normal file
124
app/api/curriculums/[id]/route.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
// GET: 특정 교육 과정 조회
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const curriculum = await prisma.curriculum.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
lectures: {
|
||||
orderBy: {
|
||||
registeredAt: 'desc',
|
||||
},
|
||||
include: {
|
||||
registrant: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!curriculum) {
|
||||
return NextResponse.json(
|
||||
{ error: '교육 과정을 찾을 수 없습니다.' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
id: curriculum.id,
|
||||
courseName: curriculum.title,
|
||||
instructorId: curriculum.instructorId,
|
||||
thumbnailImage: curriculum.thumbnailImage,
|
||||
createdAt: curriculum.createdAt.toISOString().split('T')[0],
|
||||
lectures: curriculum.lectures.map((lecture) => ({
|
||||
id: lecture.id,
|
||||
lectureName: lecture.title,
|
||||
attachedFile: lecture.attachmentFile || '없음',
|
||||
questionCount: lecture.evaluationQuestionCount,
|
||||
registrar: lecture.registrant.name || '알 수 없음',
|
||||
createdAt: lecture.registeredAt.toISOString().split('T')[0],
|
||||
})),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching curriculum:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '교육 과정을 불러오는 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PUT: 교육 과정 수정
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { title, instructorId, thumbnailImage } = body;
|
||||
|
||||
const curriculum = await prisma.curriculum.update({
|
||||
where: { id: params.id },
|
||||
data: {
|
||||
...(title && { title }),
|
||||
...(instructorId && { instructorId }),
|
||||
...(thumbnailImage !== undefined && { thumbnailImage }),
|
||||
},
|
||||
include: {
|
||||
lectures: {
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
id: curriculum.id,
|
||||
courseName: curriculum.title,
|
||||
instructorId: curriculum.instructorId,
|
||||
thumbnailImage: curriculum.thumbnailImage,
|
||||
createdAt: curriculum.createdAt.toISOString().split('T')[0],
|
||||
lectureCount: curriculum.lectures.length,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error updating curriculum:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '교육 과정 수정 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE: 교육 과정 삭제
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
await prisma.curriculum.delete({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ message: '교육 과정이 삭제되었습니다.' });
|
||||
} catch (error) {
|
||||
console.error('Error deleting curriculum:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '교육 과정 삭제 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
113
app/api/curriculums/route.ts
Normal file
113
app/api/curriculums/route.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
// GET: 교육 과정 목록 조회
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const page = parseInt(searchParams.get('page') || '1');
|
||||
const limit = parseInt(searchParams.get('limit') || '13');
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const [curriculums, total] = await Promise.all([
|
||||
prisma.curriculum.findMany({
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
include: {
|
||||
lectures: {
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
prisma.curriculum.count(),
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
data: curriculums.map((curriculum) => ({
|
||||
id: curriculum.id,
|
||||
courseName: curriculum.title,
|
||||
instructorId: curriculum.instructorId,
|
||||
thumbnailImage: curriculum.thumbnailImage,
|
||||
createdAt: curriculum.createdAt.toISOString().split('T')[0],
|
||||
lectureCount: curriculum.lectures.length,
|
||||
})),
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
});
|
||||
} 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, thumbnailImage } = body;
|
||||
|
||||
if (!title || !instructorId) {
|
||||
return NextResponse.json(
|
||||
{ error: '교육 과정명과 강사 ID는 필수입니다.' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 강사 존재 확인
|
||||
const instructor = await prisma.user.findUnique({
|
||||
where: { id: instructorId },
|
||||
});
|
||||
|
||||
if (!instructor || instructor.role !== 'INSTRUCTOR') {
|
||||
return NextResponse.json(
|
||||
{ error: '유효한 강사를 선택해주세요.' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const curriculum = await prisma.curriculum.create({
|
||||
data: {
|
||||
title,
|
||||
instructorId,
|
||||
thumbnailImage: thumbnailImage || null,
|
||||
},
|
||||
include: {
|
||||
lectures: {
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
id: curriculum.id,
|
||||
courseName: curriculum.title,
|
||||
instructorId: curriculum.instructorId,
|
||||
thumbnailImage: curriculum.thumbnailImage,
|
||||
createdAt: curriculum.createdAt.toISOString().split('T')[0],
|
||||
lectureCount: curriculum.lectures.length,
|
||||
},
|
||||
}, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Error creating curriculum:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '교육 과정 생성 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
127
app/api/lectures/[id]/route.ts
Normal file
127
app/api/lectures/[id]/route.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
// GET: 특정 강좌 조회
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const lecture = await prisma.lecture.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
curriculum: {
|
||||
select: {
|
||||
title: true,
|
||||
},
|
||||
},
|
||||
registrant: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!lecture) {
|
||||
return NextResponse.json(
|
||||
{ error: '강좌를 찾을 수 없습니다.' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
id: lecture.id,
|
||||
courseName: lecture.curriculum.title,
|
||||
lectureName: lecture.title,
|
||||
attachedFile: lecture.attachmentFile || '없음',
|
||||
questionCount: lecture.evaluationQuestionCount,
|
||||
registrar: lecture.registrant.name || '알 수 없음',
|
||||
createdAt: lecture.registeredAt.toISOString().split('T')[0],
|
||||
curriculumId: lecture.curriculumId,
|
||||
thumbnailImage: lecture.thumbnailImage,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching lecture:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '강좌를 불러오는 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PUT: 강좌 수정
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { title, attachmentFile, evaluationQuestionCount, thumbnailImage } = body;
|
||||
|
||||
const lecture = await prisma.lecture.update({
|
||||
where: { id: params.id },
|
||||
data: {
|
||||
...(title && { title }),
|
||||
...(attachmentFile !== undefined && { attachmentFile }),
|
||||
...(evaluationQuestionCount !== undefined && { evaluationQuestionCount }),
|
||||
...(thumbnailImage !== undefined && { thumbnailImage }),
|
||||
},
|
||||
include: {
|
||||
curriculum: {
|
||||
select: {
|
||||
title: true,
|
||||
},
|
||||
},
|
||||
registrant: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
id: lecture.id,
|
||||
courseName: lecture.curriculum.title,
|
||||
lectureName: lecture.title,
|
||||
attachedFile: lecture.attachmentFile || '없음',
|
||||
questionCount: lecture.evaluationQuestionCount,
|
||||
registrar: lecture.registrant.name || '알 수 없음',
|
||||
createdAt: lecture.registeredAt.toISOString().split('T')[0],
|
||||
curriculumId: lecture.curriculumId,
|
||||
thumbnailImage: lecture.thumbnailImage,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error updating lecture:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '강좌 수정 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE: 강좌 삭제
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
await prisma.lecture.delete({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ message: '강좌가 삭제되었습니다.' });
|
||||
} catch (error) {
|
||||
console.error('Error deleting lecture:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '강좌 삭제 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
148
app/api/lectures/route.ts
Normal file
148
app/api/lectures/route.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
// GET: 강좌 목록 조회
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const page = parseInt(searchParams.get('page') || '1');
|
||||
const limit = parseInt(searchParams.get('limit') || '13');
|
||||
const curriculumId = searchParams.get('curriculumId');
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where = curriculumId ? { curriculumId } : {};
|
||||
|
||||
const [lectures, total] = await Promise.all([
|
||||
prisma.lecture.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: {
|
||||
registeredAt: 'desc',
|
||||
},
|
||||
include: {
|
||||
curriculum: {
|
||||
select: {
|
||||
title: true,
|
||||
},
|
||||
},
|
||||
registrant: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
prisma.lecture.count({ where }),
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
data: lectures.map((lecture) => ({
|
||||
id: lecture.id,
|
||||
courseName: lecture.curriculum.title,
|
||||
lectureName: lecture.title,
|
||||
attachedFile: lecture.attachmentFile || '없음',
|
||||
questionCount: lecture.evaluationQuestionCount,
|
||||
registrar: lecture.registrant.name || '알 수 없음',
|
||||
createdAt: lecture.registeredAt.toISOString().split('T')[0],
|
||||
curriculumId: lecture.curriculumId,
|
||||
thumbnailImage: lecture.thumbnailImage,
|
||||
})),
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
});
|
||||
} 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, curriculumId, registrantId, attachmentFile, evaluationQuestionCount, thumbnailImage } = 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: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 등록자 존재 확인
|
||||
const registrant = await prisma.user.findUnique({
|
||||
where: { id: registrantId },
|
||||
});
|
||||
|
||||
if (!registrant) {
|
||||
return NextResponse.json(
|
||||
{ error: '유효한 등록자를 선택해주세요.' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const lecture = await prisma.lecture.create({
|
||||
data: {
|
||||
title,
|
||||
curriculumId,
|
||||
registrantId,
|
||||
attachmentFile: attachmentFile || null,
|
||||
evaluationQuestionCount: evaluationQuestionCount || 0,
|
||||
thumbnailImage: thumbnailImage || null,
|
||||
},
|
||||
include: {
|
||||
curriculum: {
|
||||
select: {
|
||||
title: true,
|
||||
},
|
||||
},
|
||||
registrant: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
id: lecture.id,
|
||||
courseName: lecture.curriculum.title,
|
||||
lectureName: lecture.title,
|
||||
attachedFile: lecture.attachmentFile || '없음',
|
||||
questionCount: lecture.evaluationQuestionCount,
|
||||
registrar: lecture.registrant.name || '알 수 없음',
|
||||
createdAt: lecture.registeredAt.toISOString().split('T')[0],
|
||||
curriculumId: lecture.curriculumId,
|
||||
thumbnailImage: lecture.thumbnailImage,
|
||||
},
|
||||
}, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Error creating lecture:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '강좌 생성 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
import { PrismaClient } from "@/lib/generated/prisma/client";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const tests = await prisma.test.findMany();
|
||||
return NextResponse.json(tests);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ error: "Failed to fetch tests." }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const body = await req.json();
|
||||
const { name } = body;
|
||||
if (!name) {
|
||||
return NextResponse.json({ error: "Name is required." }, { status: 400 });
|
||||
}
|
||||
const test = await prisma.test.create({
|
||||
data: { name },
|
||||
});
|
||||
return NextResponse.json(test, { status: 201 });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Failed to create test." }, { status: 500 });
|
||||
}
|
||||
}
|
||||
152
app/api/user-lectures/[id]/route.ts
Normal file
152
app/api/user-lectures/[id]/route.ts
Normal file
@@ -0,0 +1,152 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
// GET: 특정 수강 관계 조회
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const userLecture = await prisma.userLecture.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
lecture: {
|
||||
include: {
|
||||
curriculum: {
|
||||
select: {
|
||||
title: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!userLecture) {
|
||||
return NextResponse.json(
|
||||
{ error: '수강 관계를 찾을 수 없습니다.' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
id: userLecture.id,
|
||||
userId: userLecture.userId,
|
||||
userName: userLecture.user.name,
|
||||
userEmail: userLecture.user.email,
|
||||
lectureId: userLecture.lectureId,
|
||||
lectureName: userLecture.lecture.title,
|
||||
courseName: userLecture.lecture.curriculum.title,
|
||||
enrolledAt: userLecture.enrolledAt.toISOString().split('T')[0],
|
||||
completedAt: userLecture.completedAt ? userLecture.completedAt.toISOString().split('T')[0] : null,
|
||||
isCompleted: userLecture.isCompleted,
|
||||
progress: userLecture.progress,
|
||||
score: userLecture.score,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching user lecture:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '수강 관계를 불러오는 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PUT: 수강 관계 수정 (진행률, 완료 여부, 점수 등)
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { progress, isCompleted, score } = body;
|
||||
|
||||
const updateData: any = {};
|
||||
if (progress !== undefined) updateData.progress = Math.max(0, Math.min(100, progress));
|
||||
if (isCompleted !== undefined) {
|
||||
updateData.isCompleted = isCompleted;
|
||||
if (isCompleted && !body.completedAt) {
|
||||
updateData.completedAt = new Date();
|
||||
} else if (!isCompleted) {
|
||||
updateData.completedAt = null;
|
||||
}
|
||||
}
|
||||
if (score !== undefined) updateData.score = score;
|
||||
|
||||
const userLecture = await prisma.userLecture.update({
|
||||
where: { id: params.id },
|
||||
data: updateData,
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
lecture: {
|
||||
include: {
|
||||
curriculum: {
|
||||
select: {
|
||||
title: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
id: userLecture.id,
|
||||
userId: userLecture.userId,
|
||||
userName: userLecture.user.name,
|
||||
userEmail: userLecture.user.email,
|
||||
lectureId: userLecture.lectureId,
|
||||
lectureName: userLecture.lecture.title,
|
||||
courseName: userLecture.lecture.curriculum.title,
|
||||
enrolledAt: userLecture.enrolledAt.toISOString().split('T')[0],
|
||||
completedAt: userLecture.completedAt ? userLecture.completedAt.toISOString().split('T')[0] : null,
|
||||
isCompleted: userLecture.isCompleted,
|
||||
progress: userLecture.progress,
|
||||
score: userLecture.score,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error updating user lecture:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '수강 관계 수정 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE: 수강 관계 삭제 (수강 취소)
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
await prisma.userLecture.delete({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ message: '수강이 취소되었습니다.' });
|
||||
} catch (error) {
|
||||
console.error('Error deleting user lecture:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '수강 취소 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
182
app/api/user-lectures/route.ts
Normal file
182
app/api/user-lectures/route.ts
Normal file
@@ -0,0 +1,182 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
// GET: 사용자-강좌 수강 관계 목록 조회
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const userId = searchParams.get('userId');
|
||||
const lectureId = searchParams.get('lectureId');
|
||||
const page = parseInt(searchParams.get('page') || '1');
|
||||
const limit = parseInt(searchParams.get('limit') || '100');
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: any = {};
|
||||
if (userId) where.userId = userId;
|
||||
if (lectureId) where.lectureId = lectureId;
|
||||
|
||||
const [userLectures, total] = await Promise.all([
|
||||
prisma.userLecture.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: {
|
||||
enrolledAt: 'desc',
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
lecture: {
|
||||
include: {
|
||||
curriculum: {
|
||||
select: {
|
||||
title: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
prisma.userLecture.count({ where }),
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
data: userLectures.map((ul) => ({
|
||||
id: ul.id,
|
||||
userId: ul.userId,
|
||||
userName: ul.user.name,
|
||||
userEmail: ul.user.email,
|
||||
lectureId: ul.lectureId,
|
||||
lectureName: ul.lecture.title,
|
||||
courseName: ul.lecture.curriculum.title,
|
||||
enrolledAt: ul.enrolledAt.toISOString().split('T')[0],
|
||||
completedAt: ul.completedAt ? ul.completedAt.toISOString().split('T')[0] : null,
|
||||
isCompleted: ul.isCompleted,
|
||||
progress: ul.progress,
|
||||
score: ul.score,
|
||||
})),
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
});
|
||||
} 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: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 강좌 존재 확인
|
||||
const lecture = await prisma.lecture.findUnique({
|
||||
where: { id: lectureId },
|
||||
});
|
||||
|
||||
if (!lecture) {
|
||||
return NextResponse.json(
|
||||
{ error: '유효한 강좌를 선택해주세요.' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 이미 수강 중인지 확인
|
||||
const existing = await prisma.userLecture.findUnique({
|
||||
where: {
|
||||
userId_lectureId: {
|
||||
userId,
|
||||
lectureId,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return NextResponse.json(
|
||||
{ error: '이미 수강 중인 강좌입니다.' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const userLecture = await prisma.userLecture.create({
|
||||
data: {
|
||||
userId,
|
||||
lectureId,
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
lecture: {
|
||||
include: {
|
||||
curriculum: {
|
||||
select: {
|
||||
title: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
id: userLecture.id,
|
||||
userId: userLecture.userId,
|
||||
userName: userLecture.user.name,
|
||||
userEmail: userLecture.user.email,
|
||||
lectureId: userLecture.lectureId,
|
||||
lectureName: userLecture.lecture.title,
|
||||
courseName: userLecture.lecture.curriculum.title,
|
||||
enrolledAt: userLecture.enrolledAt.toISOString().split('T')[0],
|
||||
completedAt: userLecture.completedAt ? userLecture.completedAt.toISOString().split('T')[0] : null,
|
||||
isCompleted: userLecture.isCompleted,
|
||||
progress: userLecture.progress,
|
||||
score: userLecture.score,
|
||||
},
|
||||
}, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Error creating user lecture:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '수강 신청 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
59
app/api/users/route.ts
Normal file
59
app/api/users/route.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
// GET: 사용자 목록 조회 (강사 목록 포함)
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const role = searchParams.get('role'); // 'INSTRUCTOR', 'ADMIN', 'STUDENT' 등
|
||||
const page = parseInt(searchParams.get('page') || '1');
|
||||
const limit = parseInt(searchParams.get('limit') || '100');
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where = role ? { role: role as any } : {};
|
||||
|
||||
const [users, total] = await Promise.all([
|
||||
prisma.user.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
role: true,
|
||||
isActive: true,
|
||||
createdAt: true,
|
||||
},
|
||||
}),
|
||||
prisma.user.count({ where }),
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
data: users.map((user) => ({
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
role: user.role,
|
||||
isActive: user.isActive,
|
||||
createdAt: user.createdAt.toISOString().split('T')[0],
|
||||
})),
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching users:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '사용자 목록을 불러오는 중 오류가 발생했습니다.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user