import { json, error } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { sql } from '$lib/server/db'; /** GET /api/notes/:noteId — Hent notat */ export const GET: RequestHandler = async ({ params, locals }) => { if (!locals.workspace || !locals.user) error(401); const [note] = await sql` SELECT n.id, n.title, n.content, n.updated_at FROM notes n JOIN nodes nd ON nd.id = n.id WHERE n.id = ${params.noteId} AND nd.workspace_id = ${locals.workspace.id} `; if (!note) error(404, 'Notat ikke funnet'); return json(note); }; /** PATCH /api/notes/:noteId — Oppdater notat */ export const PATCH: RequestHandler = async ({ params, request, locals }) => { if (!locals.workspace || !locals.user) error(401); const updates = await request.json(); const [note] = await sql` SELECT n.id FROM notes n JOIN nodes nd ON nd.id = n.id WHERE n.id = ${params.noteId} AND nd.workspace_id = ${locals.workspace.id} `; if (!note) error(404, 'Notat ikke funnet'); const [updated] = await sql` UPDATE notes SET title = COALESCE(${updates.title ?? null}, title), content = CASE WHEN ${updates.content !== undefined} THEN ${updates.content ?? ''} ELSE content END WHERE id = ${params.noteId} RETURNING id, title, content, updated_at `; return json(updated); };