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,84 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { parseRepoUrl } from "@/lib/git";
|
||||
import { projectListInclude, uniqueSlug } from "@/lib/projects";
|
||||
import { formatZodError, projectCreateSchema } from "@/lib/validation";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const status = searchParams.get("status");
|
||||
const query = searchParams.get("q");
|
||||
|
||||
const projects = await prisma.project.findMany({
|
||||
where: {
|
||||
...(status ? { status: status as never } : {}),
|
||||
...(query
|
||||
? {
|
||||
OR: [
|
||||
{ name: { contains: query, mode: "insensitive" as const } },
|
||||
{ summary: { contains: query, mode: "insensitive" as const } },
|
||||
{ tags: { has: query.toLowerCase() } },
|
||||
],
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
include: projectListInclude,
|
||||
orderBy: [{ favorite: "desc" }, { updatedAt: "desc" }],
|
||||
});
|
||||
|
||||
return NextResponse.json({ projects });
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
let body: unknown;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Ungueltiger JSON-Body" }, { status: 400 });
|
||||
}
|
||||
|
||||
const parsed = projectCreateSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: "Validierung fehlgeschlagen", fields: formatZodError(parsed.error) },
|
||||
{ status: 422 },
|
||||
);
|
||||
}
|
||||
|
||||
const data = parsed.data;
|
||||
|
||||
// Owner/Name aus der URL ableiten, falls nicht explizit angegeben.
|
||||
let repoOwner = data.repoOwner ?? null;
|
||||
let repoName = data.repoName ?? null;
|
||||
if (data.repoUrl && (!repoOwner || !repoName)) {
|
||||
const parsedUrl = parseRepoUrl(data.repoUrl);
|
||||
if (parsedUrl) {
|
||||
repoOwner = repoOwner ?? parsedUrl.owner;
|
||||
repoName = repoName ?? parsedUrl.name;
|
||||
}
|
||||
}
|
||||
|
||||
const project = await prisma.project.create({
|
||||
data: {
|
||||
name: data.name,
|
||||
slug: await uniqueSlug(data.name),
|
||||
summary: data.summary ?? null,
|
||||
description: data.description ?? null,
|
||||
status: data.status,
|
||||
priority: data.priority,
|
||||
progress: data.progress,
|
||||
accent: data.accent,
|
||||
tags: data.tags,
|
||||
favorite: data.favorite,
|
||||
repoUrl: data.repoUrl ?? null,
|
||||
repoOwner,
|
||||
repoName,
|
||||
connectionId: data.connectionId ?? null,
|
||||
},
|
||||
include: projectListInclude,
|
||||
});
|
||||
|
||||
return NextResponse.json({ project }, { status: 201 });
|
||||
}
|
||||
Reference in New Issue
Block a user