관리자 계정 설정, 관리자 페이지 작업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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user