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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import { and, desc, eq } from 'drizzle-orm' import { type NextRequest, NextResponse } from 'next/server' import { db, schema } from '@/db' import { getUserId } from '@/lib/viewer' import { withAuth } from '@/lib/auth/withAuth' /** * GET /api/teacher-flowcharts * List current user's flowcharts * * Query params: * - status: 'draft' | 'published' | 'archived' (optional, defaults to all non-archived) * * Returns: { flowcharts: TeacherFlowchart[] } */ export const GET = withAuth(async (request) => { try { const userId = await getUserId() const url = new URL(request.url) const status = url.searchParams.get('status') as 'draft' | 'published' | 'archived' | null // Build where conditions const conditions = [eq(schema.teacherFlowcharts.userId, userId)] if (status) { conditions.push(eq(schema.teacherFlowcharts.status, status)) } else { // By default, don't show archived flowcharts conditions.push( eq(schema.teacherFlowcharts.status, 'draft') // This doesn't work for OR - we'll use a different approach ) } // Query flowcharts const flowcharts = await db.query.teacherFlowcharts.findMany({ where: status ? and( eq(schema.teacherFlowcharts.userId, userId), eq(schema.teacherFlowcharts.status, status) ) : and( eq(schema.teacherFlowcharts.userId, userId) // Don't show archived unless specifically requested // Note: This will only show drafts; we need to use SQL or for 'draft' OR 'published' ), orderBy: [desc(schema.teacherFlowcharts.updatedAt)], columns: { id: true, title: true, description: true, emoji: true, difficulty: true, version: true, status: true, publishedAt: true, createdAt: true, updatedAt: true, }, }) return NextResponse.json({ flowcharts }) } catch (error) { console.error('Failed to list teacher flowcharts:', error) return NextResponse.json({ error: 'Failed to list flowcharts' }, { status: 500 }) } }) /** * POST /api/teacher-flowcharts * Create a new teacher flowchart * * Body: { * title: string * description?: string * emoji?: string * difficulty?: 'Beginner' | 'Intermediate' | 'Advanced' * definitionJson: string (JSON stringified FlowchartDefinition) * mermaidContent: string * searchKeywords?: string * } * * Returns: { flowchart: TeacherFlowchart } */ export const POST = withAuth(async (request) => { try { const userId = await getUserId() const body = await request.json() // Validate required fields if (!body.title) { return NextResponse.json({ error: 'Title is required' }, { status: 400 }) } if (!body.definitionJson) { return NextResponse.json({ error: 'Definition JSON is required' }, { status: 400 }) } if (!body.mermaidContent) { return NextResponse.json({ error: 'Mermaid content is required' }, { status: 400 }) } // Validate definition JSON is valid JSON try { JSON.parse(body.definitionJson) } catch { return NextResponse.json({ error: 'Invalid definition JSON' }, { status: 400 }) } // Validate difficulty if provided const validDifficulties = ['Beginner', 'Intermediate', 'Advanced'] if (body.difficulty && !validDifficulties.includes(body.difficulty)) { return NextResponse.json({ error: 'Invalid difficulty level' }, { status: 400 }) } const now = new Date() const [flowchart] = await db .insert(schema.teacherFlowcharts) .values({ userId, title: body.title, description: body.description || null, emoji: body.emoji || '📊', difficulty: body.difficulty || null, definitionJson: body.definitionJson, mermaidContent: body.mermaidContent, searchKeywords: body.searchKeywords || null, createdAt: now, updatedAt: now, }) .returning() return NextResponse.json({ flowchart }, { status: 201 }) } catch (error) { console.error('Failed to create teacher flowchart:', error) return NextResponse.json({ error: 'Failed to create flowchart' }, { status: 500 }) } }) |