- Add updateExpense() to lib/expenses.ts - Add PATCH /expenses/:id route with validation and P2025 not-found handling - Edit button on each expense card pre-fills form; cancel restores add mode - Submit dynamically PATCHes or POSTs depending on edit state Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import { Prisma } from "@prisma/client";
|
|
import { NextResponse } from "next/server";
|
|
|
|
import { removeExpense, updateExpense } from "@/lib/expenses";
|
|
import { expenseInputSchema } from "@/lib/validation";
|
|
|
|
type RouteContext = {
|
|
params: Promise<{ id: string }>;
|
|
};
|
|
|
|
export async function PATCH(request: Request, context: RouteContext) {
|
|
const { id } = await context.params;
|
|
const payload = await request.json();
|
|
const parsed = expenseInputSchema.safeParse(payload);
|
|
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ error: parsed.error.issues[0]?.message ?? "Invalid expense payload." },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
try {
|
|
const expense = await updateExpense(id, {
|
|
title: parsed.data.title,
|
|
amountCents: parsed.data.amount,
|
|
date: parsed.data.date,
|
|
category: parsed.data.category,
|
|
});
|
|
return NextResponse.json({ expense });
|
|
} catch (error) {
|
|
if (
|
|
error instanceof Prisma.PrismaClientKnownRequestError &&
|
|
error.code === "P2025"
|
|
) {
|
|
return NextResponse.json({ error: "Expense not found." }, { status: 404 });
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function DELETE(_: Request, context: RouteContext) {
|
|
const { id } = await context.params;
|
|
|
|
try {
|
|
await removeExpense(id);
|
|
return new NextResponse(null, { status: 204 });
|
|
} catch (error) {
|
|
if (
|
|
error instanceof Prisma.PrismaClientKnownRequestError &&
|
|
error.code === "P2025"
|
|
) {
|
|
return NextResponse.json({ error: "Expense not found." }, { status: 404 });
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
}
|