feat(api): REST-Endpunkte fuer Projekte, Notizen, Verbindungen und Sync
Alles, was die Oberflaeche kann, ist damit auch scriptbar: GET POST /api/projects GET PATCH DELETE /api/projects/:id POST /api/projects/:id/sync GET POST /api/projects/:id/notes PATCH DELETE /api/notes/:id GET POST /api/connections PATCH DELETE /api/connections/:id POST /api/connections/:id/check GET /api/connections/:id/repos GET /api/health Details: - Jede Eingabe laeuft durch Zod; Feldfehler kommen als Objekt zurueck, damit Formulare sie direkt anzeigen koennen. - Beim Anlegen werden Owner und Repo-Name aus der URL abgeleitet, falls nicht explizit angegeben. - Ein leeres Token-Feld beim Bearbeiten einer Verbindung laesst das gespeicherte Token unveraendert; null entfernt es. - Verbindungsantworten enthalten nie das Chiffrat, nur einen Hinweis wie "ghp_...9f3a". - /api/health prueft die Datenbank und dient dem Docker-Healthcheck. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { formatZodError, noteCreateSchema } from "@/lib/validation";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function GET(_request: Request, { params }: Params) {
|
||||
const { id } = await params;
|
||||
const notes = await prisma.note.findMany({
|
||||
where: { projectId: id },
|
||||
orderBy: [{ pinned: "desc" }, { updatedAt: "desc" }],
|
||||
});
|
||||
return NextResponse.json({ notes });
|
||||
}
|
||||
|
||||
export async function POST(request: Request, { params }: Params) {
|
||||
const { id } = await params;
|
||||
|
||||
const project = await prisma.project.findUnique({ where: { id }, select: { id: true } });
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: "Projekt nicht gefunden" }, { status: 404 });
|
||||
}
|
||||
|
||||
let body: unknown;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Ungueltiger JSON-Body" }, { status: 400 });
|
||||
}
|
||||
|
||||
const parsed = noteCreateSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: "Validierung fehlgeschlagen", fields: formatZodError(parsed.error) },
|
||||
{ status: 422 },
|
||||
);
|
||||
}
|
||||
|
||||
const note = await prisma.note.create({
|
||||
data: {
|
||||
projectId: id,
|
||||
title: parsed.data.title ?? null,
|
||||
content: parsed.data.content,
|
||||
pinned: parsed.data.pinned,
|
||||
},
|
||||
});
|
||||
|
||||
// Projekt "anfassen", damit die Sortierung nach Aktivitaet stimmt.
|
||||
await prisma.project.update({ where: { id }, data: { updatedAt: new Date() } });
|
||||
|
||||
return NextResponse.json({ note }, { status: 201 });
|
||||
}
|
||||
Reference in New Issue
Block a user