"feat: refactor API handlers to use services, add logging, and improve security checks"
This commit is contained in:
34
03_source/cms_backend/src/app/api/student/route.ts
Normal file
34
03_source/cms_backend/src/app/api/student/route.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import prisma from '@/lib/prisma';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
// GET: 获取所有学生
|
||||
export async function GET() {
|
||||
try {
|
||||
const students = await prisma.student.findMany();
|
||||
return NextResponse.json(students);
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Failed to fetch students' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// POST: 创建新学生
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { email, metadata } = await request.json();
|
||||
|
||||
if (!email) {
|
||||
return NextResponse.json({ error: 'Email is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const student = await prisma.student.create({
|
||||
data: {
|
||||
email,
|
||||
metadata: metadata || {},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(student, { status: 201 });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Failed to create student' }, { status: 500 });
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user