TypeScript 98.3%
CSS 0.9%
Shell 0.7%
1// Bibliothèque personnelle : éléments sauvegardés (réponses, résumés, notes).2import { NextResponse } from "next/server";3import { z } from "zod";4import { apiError, parseBody } from "@/lib/api.ts";5import { assertSameOrigin, requireUser } from "@/lib/auth/session.ts";6import { all, get, run } from "@/lib/db/index.ts";78export async function GET(req: Request) {9 try {10 const user = await requireUser();11 const url = new URL(req.url);12 const kind = url.searchParams.get("kind");13 const params: unknown[] = [user.id];14 let where = "user_id = ?";15 if (kind) { where += " AND kind = ?"; params.push(kind); }16 const items = all(`SELECT id, kind, course_code, title, content, meta, created_at FROM saved_items WHERE ${where} ORDER BY id DESC LIMIT 200`, ...params);17 return NextResponse.json({ items });18 } catch (e) {19 return apiError(e);20 }21}2223const createSchema = z.object({24 kind: z.enum(["note", "answer", "summary", "quiz", "plan"]),25 courseCode: z.enum(["IMM1003", "IMM1033"]).nullable().optional(),26 title: z.string().min(1).max(200),27 content: z.string().max(50_000),28});2930export async function POST(req: Request) {31 try {32 await assertSameOrigin();33 const user = await requireUser();34 const body = await parseBody(req, createSchema);35 const r = run(36 "INSERT INTO saved_items (user_id, kind, course_code, title, content) VALUES (?, ?, ?, ?, ?)",37 user.id, body.kind, body.courseCode ?? null, body.title, body.content38 );39 return NextResponse.json({ ok: true, id: Number(r.lastInsertRowid) });40 } catch (e) {41 return apiError(e);42 }43}4445export async function DELETE(req: Request) {46 try {47 await assertSameOrigin();48 const user = await requireUser();49 const { id } = await parseBody(req, z.object({ id: z.number().int().positive() }));50 const row = get("SELECT id FROM saved_items WHERE id = ? AND user_id = ?", id, user.id);51 if (!row) return NextResponse.json({ error: "Élément introuvable." }, { status: 404 });52 run("DELETE FROM saved_items WHERE id = ?", id);53 return NextResponse.json({ ok: true });54 } catch (e) {55 return apiError(e);56 }57}58