feat: add request create/list API
This commit is contained in:
@@ -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,
|
||||
})),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user