From 537c3a3c87407885fc5ddb74b48ba9ada78ec17a Mon Sep 17 00:00:00 2001 From: arsam Date: Fri, 18 Sep 2026 23:16:59 +0330 Subject: [PATCH] Import source: vault/app/api/snippets/[id]/route Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality. --- vault/app/api/snippets/[id]/route.ts | 68 ++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 vault/app/api/snippets/[id]/route.ts diff --git a/vault/app/api/snippets/[id]/route.ts b/vault/app/api/snippets/[id]/route.ts new file mode 100644 index 0000000..d5d3def --- /dev/null +++ b/vault/app/api/snippets/[id]/route.ts @@ -0,0 +1,68 @@ +import { NextResponse } from "next/server"; +import { applySnippetUpdate, jsonError, purgeExpiredSnippets, publicSnippet, readJsonBody } from "@/lib/server-api"; +import { getCurrentUser } from "@/lib/server-security"; +import { readDb, writeDb } from "@/lib/server-db"; + +interface RouteContext { + params: Promise<{ id: string }>; +} + +export async function GET(_request: Request, context: RouteContext) { + const user = await getCurrentUser(); + if (!user) return jsonError("Authentication required", 401); + + await purgeExpiredSnippets(); + const { id } = await context.params; + const db = await readDb(); + const snippet = db.snippets.find((candidate) => candidate.id === id && candidate.ownerId === user.id); + if (!snippet) return jsonError("Snippet not found", 404); + + return NextResponse.json({ snippet: publicSnippet(snippet) }); +} + +export async function PATCH(request: Request, context: RouteContext) { + const user = await getCurrentUser(); + if (!user) return jsonError("Authentication required", 401); + + const { id } = await context.params; + const body = await readJsonBody(request); + let updatedSnippet = null as ReturnType | null; + let error: string | null = null; + + await writeDb(async (db) => { + const index = db.snippets.findIndex((candidate) => candidate.id === id && candidate.ownerId === user.id); + if (index < 0) { + error = "Snippet not found"; + return; + } + + const next = await applySnippetUpdate(db.snippets[index], body); + if ("error" in next) { + error = next.error; + return; + } + + db.snippets[index] = next; + updatedSnippet = publicSnippet(next); + }); + + if (error) return jsonError(error, error === "Snippet not found" ? 404 : 400); + return NextResponse.json({ snippet: updatedSnippet }); +} + +export async function DELETE(_request: Request, context: RouteContext) { + const user = await getCurrentUser(); + if (!user) return jsonError("Authentication required", 401); + + const { id } = await context.params; + let deleted = false; + + await writeDb((db) => { + const before = db.snippets.length; + db.snippets = db.snippets.filter((snippet) => !(snippet.id === id && snippet.ownerId === user.id)); + deleted = db.snippets.length !== before; + }); + + if (!deleted) return jsonError("Snippet not found", 404); + return new Response(null, { status: 204 }); +}