Add expense editing with inline form and PATCH API route
- 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>
This commit is contained in:
@@ -1,12 +1,44 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import { removeExpense } from "@/lib/expenses";
|
||||
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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user