kreativortex/app/api/assignments/route.ts
Jessica Rekcah 4253483f44 jalan
2025-12-02 00:22:34 +07:00

101 lines
2.4 KiB
TypeScript

/**
* File: route.ts
* Created by: AI Assistant
* Date: 2025-11-29
* Purpose: Assignment API endpoints for listing and creating assignments
* Part of: kreatiVortex - Platform Pembelajaran Tari Online
*/
import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const classId = searchParams.get('classId');
const whereClause: any = { isActive: true };
if (classId) {
whereClause.classId = classId;
}
const assignments = await prisma.assignment.findMany({
where: whereClause,
include: {
educator: {
include: {
user: {
select: {
name: true,
image: true,
},
},
},
},
class: {
select: {
name: true,
},
},
},
orderBy: {
dueDate: 'asc',
},
});
return NextResponse.json({ success: true, data: assignments });
} catch (error) {
console.error('Error fetching assignments:', error);
return NextResponse.json(
{ success: false, message: 'Failed to fetch assignments' },
{ status: 500 }
);
}
}
export async function POST(request: Request) {
try {
const body = await request.json();
const { title, description, classId, dueDate, maxScore } = body;
// Validation
if (!title || !description || !classId || !dueDate) {
return NextResponse.json(
{ success: false, message: 'All fields are required' },
{ status: 400 }
);
}
// Mock educator ID for now
const educator = await prisma.userProfile.findFirst();
if (!educator) {
return NextResponse.json(
{ success: false, message: 'No user profile found' },
{ status: 400 }
);
}
const assignment = await prisma.assignment.create({
data: {
title,
description,
classId,
dueDate: new Date(dueDate),
maxScore: Number(maxScore),
educatorId: educator.id,
createdBy: educator.id,
updatedBy: educator.id,
},
});
return NextResponse.json({ success: true, data: assignment });
} catch (error) {
console.error('Error creating assignment:', error);
return NextResponse.json(
{ success: false, message: 'Failed to create assignment' },
{ status: 500 }
);
}
}