mirror of
https://github.com/acedanger/finance.git
synced 2025-12-05 22:50:12 -08:00
Refactor transaction API to improve ID handling and error responses
This commit is contained in:
@@ -3,9 +3,18 @@ import { transactions, accounts } from "../../../../data/store";
|
|||||||
import type { Transaction } from "../../../../types";
|
import type { Transaction } from "../../../../types";
|
||||||
|
|
||||||
export const PUT: APIRoute = async ({ request, params }) => {
|
export const PUT: APIRoute = async ({ request, params }) => {
|
||||||
|
const { id } = params;
|
||||||
|
|
||||||
|
if (!id) {
|
||||||
|
return new Response(JSON.stringify({ error: "Transaction ID is required" }), {
|
||||||
|
status: 400,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const updates = (await request.json()) as Partial<Transaction>;
|
const updates = (await request.json()) as Partial<Transaction>;
|
||||||
const transactionIndex = transactions.findIndex((t) => t.id === params.id);
|
const transactionIndex = transactions.findIndex((t) => t.id === id);
|
||||||
|
|
||||||
if (transactionIndex === -1) {
|
if (transactionIndex === -1) {
|
||||||
return new Response(JSON.stringify({ error: "Transaction not found" }), {
|
return new Response(JSON.stringify({ error: "Transaction not found" }), {
|
||||||
@@ -31,7 +40,7 @@ export const PUT: APIRoute = async ({ request, params }) => {
|
|||||||
const updatedTransaction: Transaction = {
|
const updatedTransaction: Transaction = {
|
||||||
...oldTransaction,
|
...oldTransaction,
|
||||||
...updates,
|
...updates,
|
||||||
id: params.id, // Ensure ID doesn't change
|
id: id, // Ensure ID doesn't change
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add new amount to account balance
|
// Add new amount to account balance
|
||||||
@@ -53,7 +62,16 @@ export const PUT: APIRoute = async ({ request, params }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const DELETE: APIRoute = async ({ params }) => {
|
export const DELETE: APIRoute = async ({ params }) => {
|
||||||
const transactionIndex = transactions.findIndex((t) => t.id === params.id);
|
const { id } = params;
|
||||||
|
|
||||||
|
if (!id) {
|
||||||
|
return new Response(JSON.stringify({ error: "Transaction ID is required" }), {
|
||||||
|
status: 400,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const transactionIndex = transactions.findIndex((t) => t.id === id);
|
||||||
|
|
||||||
if (transactionIndex === -1) {
|
if (transactionIndex === -1) {
|
||||||
return new Response(JSON.stringify({ error: "Transaction not found" }), {
|
return new Response(JSON.stringify({ error: "Transaction not found" }), {
|
||||||
|
|||||||
Reference in New Issue
Block a user