All files / web/src/app/api/classrooms route.ts

0% Statements 0/58
0% Branches 0/1
0% Functions 0/1
0% Lines 0/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                                                                                                                     
import { NextResponse } from 'next/server'
import { createClassroom, getTeacherClassroom } from '@/lib/classroom'
import { getUserId } from '@/lib/viewer'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * GET /api/classrooms
 * Get current user's classroom (alias for /api/classrooms/mine)
 *
 * Returns: { classroom } or { classroom: null }
 */
export const GET = withAuth(async () => {
  try {
    const userId = await getUserId()

    const classroom = await getTeacherClassroom(userId)

    return NextResponse.json({ classroom })
  } catch (error) {
    console.error('Failed to fetch classroom:', error)
    return NextResponse.json({ error: 'Failed to fetch classroom' }, { status: 500 })
  }
})

/**
 * POST /api/classrooms
 * Create a classroom for current user (becomes teacher)
 *
 * Body: { name: string }
 * Returns: { success: true, classroom } or { success: false, error }
 */
export const POST = withAuth(async (req) => {
  try {
    const userId = await getUserId()
    const body = await req.json()

    if (!body.name) {
      return NextResponse.json({ success: false, error: 'Missing name' }, { status: 400 })
    }

    const result = await createClassroom({
      teacherId: userId,
      name: body.name,
    })

    if (!result.success) {
      return NextResponse.json({ success: false, error: result.error }, { status: 400 })
    }

    return NextResponse.json({ success: true, classroom: result.classroom }, { status: 201 })
  } catch (error) {
    console.error('Failed to create classroom:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to create classroom' },
      { status: 500 }
    )
  }
})