prizma 이상한거거
This commit is contained in:
@@ -1,161 +0,0 @@
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,220 +0,0 @@
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,234 +0,0 @@
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
// POST - 로그인
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { email, password } = body;
|
||||
|
||||
if (!email || !password) {
|
||||
return NextResponse.json({ error: '이메일과 비밀번호를 입력해주세요.' }, { status: 400 });
|
||||
}
|
||||
|
||||
// 사용자 조회
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { email },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: '아이디 또는 비밀번호가 올바르지 않습니다.' }, { status: 401 });
|
||||
}
|
||||
|
||||
// 계정 활성화 확인
|
||||
if (!user.isActive) {
|
||||
return NextResponse.json({ error: '비활성화된 계정입니다.' }, { status: 403 });
|
||||
}
|
||||
|
||||
// 비밀번호 확인
|
||||
const isPasswordValid = await bcrypt.compare(password, user.password);
|
||||
|
||||
if (!isPasswordValid) {
|
||||
return NextResponse.json({ error: '아이디 또는 비밀번호가 올바르지 않습니다.' }, { status: 401 });
|
||||
}
|
||||
|
||||
// 비밀번호 제외하고 사용자 정보 반환
|
||||
const { password: _, ...userWithoutPassword } = user;
|
||||
|
||||
return NextResponse.json({
|
||||
user: userWithoutPassword,
|
||||
message: '로그인 성공',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error during login:', error);
|
||||
return NextResponse.json({ error: '로그인 중 오류가 발생했습니다.' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,187 +0,0 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
// GET - 사용자 목록 조회 또는 단일 사용자 조회
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const id = searchParams.get('id');
|
||||
const email = searchParams.get('email');
|
||||
|
||||
if (id) {
|
||||
// 단일 사용자 조회
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
enrolledLectures: {
|
||||
include: {
|
||||
lecture: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: '사용자를 찾을 수 없습니다.' }, { status: 404 });
|
||||
}
|
||||
|
||||
// 비밀번호 제외
|
||||
const { password, ...userWithoutPassword } = user;
|
||||
return NextResponse.json(userWithoutPassword);
|
||||
}
|
||||
|
||||
if (email) {
|
||||
// 이메일로 사용자 조회
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { email },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: '사용자를 찾을 수 없습니다.' }, { status: 404 });
|
||||
}
|
||||
|
||||
const { password, ...userWithoutPassword } = user;
|
||||
return NextResponse.json(userWithoutPassword);
|
||||
}
|
||||
|
||||
// 전체 사용자 목록 조회
|
||||
const users = await prisma.user.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
phone: true,
|
||||
gender: true,
|
||||
role: true,
|
||||
isActive: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(users);
|
||||
} catch (error) {
|
||||
console.error('Error fetching users:', error);
|
||||
return NextResponse.json({ error: '사용자 조회 중 오류가 발생했습니다.' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// POST - 사용자 생성 (회원가입)
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { email, password, name, phone, gender, birthYear, birthMonth, birthDay, role } = body;
|
||||
|
||||
// 필수 필드 검증
|
||||
if (!email || !password) {
|
||||
return NextResponse.json({ error: '이메일과 비밀번호는 필수입니다.' }, { status: 400 });
|
||||
}
|
||||
|
||||
// 이메일 중복 확인
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email },
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
return NextResponse.json({ error: '이미 등록된 이메일입니다.' }, { status: 409 });
|
||||
}
|
||||
|
||||
// 비밀번호 해시화
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
// 사용자 생성
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
email,
|
||||
password: hashedPassword,
|
||||
name: name || null,
|
||||
phone: phone || null,
|
||||
gender: gender || null,
|
||||
birthYear: birthYear ? parseInt(birthYear) : null,
|
||||
birthMonth: birthMonth ? parseInt(birthMonth) : null,
|
||||
birthDay: birthDay ? parseInt(birthDay) : null,
|
||||
role: role || 'STUDENT',
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
phone: true,
|
||||
gender: true,
|
||||
role: true,
|
||||
isActive: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(user, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Error creating user:', error);
|
||||
return NextResponse.json({ error: '사용자 생성 중 오류가 발생했습니다.' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// PUT - 사용자 정보 수정
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { id, name, phone, gender, birthYear, birthMonth, birthDay, role, isActive } = body;
|
||||
|
||||
if (!id) {
|
||||
return NextResponse.json({ error: '사용자 ID는 필수입니다.' }, { status: 400 });
|
||||
}
|
||||
|
||||
const updateData: any = {};
|
||||
if (name !== undefined) updateData.name = name;
|
||||
if (phone !== undefined) updateData.phone = phone;
|
||||
if (gender !== undefined) updateData.gender = gender;
|
||||
if (birthYear !== undefined) updateData.birthYear = birthYear ? parseInt(birthYear) : null;
|
||||
if (birthMonth !== undefined) updateData.birthMonth = birthMonth ? parseInt(birthMonth) : null;
|
||||
if (birthDay !== undefined) updateData.birthDay = birthDay ? parseInt(birthDay) : null;
|
||||
if (role !== undefined) updateData.role = role;
|
||||
if (isActive !== undefined) updateData.isActive = isActive;
|
||||
|
||||
const user = await prisma.user.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
phone: true,
|
||||
gender: true,
|
||||
role: true,
|
||||
isActive: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(user);
|
||||
} catch (error) {
|
||||
console.error('Error updating user:', 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.user.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ message: '사용자가 삭제되었습니다.' });
|
||||
} catch (error) {
|
||||
console.error('Error deleting user:', error);
|
||||
return NextResponse.json({ error: '사용자 삭제 중 오류가 발생했습니다.' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user