188 lines
5.5 KiB
TypeScript
188 lines
5.5 KiB
TypeScript
|
|
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 });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|