-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathroute.ts
More file actions
89 lines (74 loc) · 2.91 KB
/
Copy pathroute.ts
File metadata and controls
89 lines (74 loc) · 2.91 KB
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
import { auth } from '@clerk/nextjs/server'
import { NextRequest, NextResponse } from 'next/server'
import { connectDB } from '@/lib/mongodb'
import { Assignment } from '@/models/Assignment'
import { z } from 'zod'
const AssignmentSchema = z.object({
title: z.string().min(1),
description: z.string().optional(),
subject: z.string().min(1),
class: z.string().min(1),
deadline: z.string().min(1),
maxMarks: z.number().min(1).optional(),
status: z.enum(['active', 'closed']).optional(),
kanbanStatus: z.enum(['todo', 'in_progress', 'submitted']).optional(),
})
export async function GET(req: NextRequest) {
const { userId } = await auth()
if (!userId) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
try {
await connectDB()
const { searchParams } = new URL(req.url)
const status = searchParams.get('status')
// Parse and validate pagination
const pageStr = searchParams.get('page') ?? '1'
const limitStr = searchParams.get('limit') ?? '20'
let page = parseInt(pageStr, 10)
let limit = parseInt(limitStr, 10)
if (!Number.isFinite(page) || page < 1) page = 1
if (!Number.isFinite(limit) || limit < 1) limit = 20
limit = Math.min(limit, 100) // Cap at 100
const query: Record<string, unknown> = { teacherId: userId }
if (status) query.status = status
const skip = (page - 1) * limit
const assignments = await Assignment.find(query)
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit)
.lean()
const total = await Assignment.countDocuments(query)
return NextResponse.json({ assignments, total, page, pages: Math.ceil(total / limit) })
} catch (error) {
console.error('GET /api/assignments error:', error)
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
}
}
export async function POST(req: NextRequest) {
const { userId } = await auth()
if (!userId) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
try {
await connectDB()
let body
try {
body = await req.json()
} catch {
return NextResponse.json({ error: 'Invalid JSON in request body' }, { status: 400 })
}
const parsed = AssignmentSchema.safeParse(body)
if (!parsed.success) return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 })
try {
const assignment = await Assignment.create({ ...parsed.data, teacherId: userId })
return NextResponse.json(assignment, { status: 201 })
} catch (dbError) {
if (dbError instanceof Error) {
console.error('Assignment.create error:', dbError.message)
}
return NextResponse.json({ error: 'Failed to create assignment' }, { status: 500 })
}
} catch (error) {
if (error instanceof Error) {
console.error('POST /api/assignments error:', error.message)
}
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
}
}