| |
| |
| |
|
|
| import { NextRequest, NextResponse } from 'next/server'; |
| import { sessionManager } from '@/lib/session-manager'; |
|
|
| export const runtime = 'nodejs'; |
| export const dynamic = 'force-dynamic'; |
|
|
| export async function POST(request: NextRequest) { |
| try { |
| const body = await request.json(); |
| const { action, sessionId, question, options, correctAnswer, duration, pollId, option, studentId } = body; |
|
|
| if (!sessionId) { |
| return NextResponse.json({ |
| success: false, |
| error: 'Missing sessionId' |
| }, { status: 400 }); |
| } |
|
|
| |
| if (action === 'create') { |
| if (!question || !options || !Array.isArray(options)) { |
| return NextResponse.json({ |
| success: false, |
| error: 'Missing required fields for poll creation' |
| }, { status: 400 }); |
| } |
|
|
| const poll = await sessionManager.createPoll( |
| sessionId, |
| question, |
| options, |
| correctAnswer, |
| duration || 60 |
| ); |
|
|
| return NextResponse.json({ |
| success: true, |
| data: poll |
| }); |
| } |
|
|
| |
| if (action === 'vote') { |
| if (!pollId || !option || !studentId) { |
| return NextResponse.json({ |
| success: false, |
| error: 'Missing required fields for voting' |
| }, { status: 400 }); |
| } |
|
|
| const poll = await sessionManager.votePoll(sessionId, pollId, option, studentId); |
|
|
| if (!poll) { |
| return NextResponse.json({ |
| success: false, |
| error: 'Poll not found or inactive' |
| }, { status: 404 }); |
| } |
|
|
| return NextResponse.json({ |
| success: true, |
| data: poll |
| }); |
| } |
|
|
| |
| if (action === 'end') { |
| if (!pollId) { |
| return NextResponse.json({ |
| success: false, |
| error: 'Missing pollId' |
| }, { status: 400 }); |
| } |
|
|
| await sessionManager.endPoll(sessionId, pollId); |
|
|
| return NextResponse.json({ |
| success: true, |
| message: 'Poll ended' |
| }); |
| } |
|
|
| return NextResponse.json({ |
| success: false, |
| error: 'Invalid action' |
| }, { status: 400 }); |
|
|
| } catch (error) { |
| console.error('Poll API error:', error); |
| return NextResponse.json({ |
| success: false, |
| error: 'Internal server error' |
| }, { status: 500 }); |
| } |
| } |
|
|
| export async function GET(request: NextRequest) { |
| try { |
| const { searchParams } = new URL(request.url); |
| const sessionId = searchParams.get('sessionId'); |
|
|
| if (!sessionId) { |
| return NextResponse.json({ |
| success: false, |
| error: 'Missing sessionId' |
| }, { status: 400 }); |
| } |
|
|
| const activePoll = sessionManager.getActivePoll(sessionId); |
|
|
| return NextResponse.json({ |
| success: true, |
| data: activePoll |
| }); |
|
|
| } catch (error) { |
| console.error('Poll API error:', error); |
| return NextResponse.json({ |
| success: false, |
| error: 'Internal server error' |
| }, { status: 500 }); |
| } |
| } |
|
|