import { NextResponse } from "next/server"; import { apiError } from "@/lib/api.ts"; import { requireUser } from "@/lib/auth/session.ts"; import { all } from "@/lib/db/index.ts"; export async function GET() { try { const user = await requireUser(); const rows = all( `SELECT a.id, a.title, a.body, a.course_code, a.pinned, a.created_at FROM announcements a WHERE a.active = 1 AND (a.course_code IS NULL OR a.course_code IN (SELECT course_code FROM enrollments WHERE user_id = ?)) ORDER BY a.pinned DESC, a.created_at DESC LIMIT 20`, user.id ); return NextResponse.json({ announcements: rows }); } catch (e) { return apiError(e); } }