| import { NextRequest, NextResponse } from 'next/server'; |
| import { GeneratedQuestion } from '@/types/quiz'; |
|
|
| export async function PUT(req: NextRequest) { |
| try { |
| const updatedQuestion: GeneratedQuestion = await req.json(); |
|
|
| |
| if (!updatedQuestion.id || !updatedQuestion.stem || !updatedQuestion.content) { |
| return NextResponse.json( |
| { error: 'Invalid question data' }, |
| { status: 400 } |
| ); |
| } |
|
|
| |
| const { Question, Options, Answer } = updatedQuestion.content; |
| if (!Question || !Options || !Answer) { |
| return NextResponse.json( |
| { error: 'Missing required question content' }, |
| { status: 400 } |
| ); |
| } |
|
|
| |
| const requiredOptions = ['A', 'B', 'C', 'D']; |
| for (const option of requiredOptions) { |
| if (!Options[option as keyof typeof Options]) { |
| return NextResponse.json( |
| { error: `Missing option ${option}` }, |
| { status: 400 } |
| ); |
| } |
| } |
|
|
| |
| if (!requiredOptions.includes(Answer)) { |
| return NextResponse.json( |
| { error: 'Invalid answer option' }, |
| { status: 400 } |
| ); |
| } |
|
|
| |
| if (typeof updatedQuestion.points !== 'number' || updatedQuestion.points < 1) { |
| return NextResponse.json( |
| { error: 'Points must be a positive number' }, |
| { status: 400 } |
| ); |
| } |
|
|
| |
| |
| |
| const validatedQuestion: GeneratedQuestion = { |
| ...updatedQuestion, |
| |
| type: updatedQuestion.type, |
| points: updatedQuestion.points, |
| |
| content: { |
| ...updatedQuestion.content, |
| Question: updatedQuestion.stem, |
| }, |
| |
| createdAt: new Date(), |
| }; |
|
|
| return NextResponse.json(validatedQuestion); |
|
|
| } catch (error) { |
| console.error('Error updating question:', error); |
| return NextResponse.json( |
| { error: 'Failed to update question' }, |
| { status: 500 } |
| ); |
| } |
| } |
|
|