server/web/src/routes/api/notes/[noteId]/+server.ts
vegard 7e83292abe Notater: auto-save scratchpad med debounce
- Migrering 0004: notes-tabell (nodes i kunnskapsgrafen)
- REST API: GET/PATCH notat
- PG-adapter med 500ms debounce og 10s polling
- NotesBlock: tittel + fritekst med auto-lagring og status
- Seed: notater for begge workspaces, kalenderside med 2-1 layout

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 03:03:41 +01:00

42 lines
1.3 KiB
TypeScript

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);
};