feat: add request create/list API

This commit is contained in:
Jonathan
2026-07-10 16:47:54 +02:00
parent ab4db641b0
commit 15bb68bc49
4 changed files with 114 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
import { prisma } from "@/lib/db";
export async function POST(request: Request) {
let body: unknown;
try {
body = await request.json();
} catch {
return Response.json({ error: "invalid JSON" }, { status: 400 });
}
const { artist, album } = (body ?? {}) as { artist?: unknown; album?: unknown };
if (typeof artist !== "string" || !artist.trim() || typeof album !== "string" || !album.trim()) {
return Response.json({ error: "artist and album are required" }, { status: 400 });
}
const created = await prisma.request.create({
data: {
artist: artist.trim(),
album: album.trim(),
job: { create: {} },
},
include: { job: true },
});
return Response.json(
{
id: created.id,
artist: created.artist,
album: created.album,
status: created.status,
job: { id: created.job!.id, state: created.job!.state, currentStage: created.job!.currentStage },
},
{ status: 201 },
);
}
export async function GET() {
const requests = await prisma.request.findMany({
orderBy: { createdAt: "desc" },
include: { job: true },
});
return Response.json({
requests: requests.map((r) => ({
id: r.id,
artist: r.artist,
album: r.album,
status: r.status,
createdAt: r.createdAt,
job: r.job ? { state: r.job.state, currentStage: r.job.currentStage } : null,
})),
});
}