Remove unused files and implement API routes for account and transaction management

This commit is contained in:
GitHub Copilot
2025-04-24 07:27:05 -04:00
parent ae6886bf92
commit 78ebf1ae32
11 changed files with 180 additions and 11 deletions

View File

@@ -0,0 +1,56 @@
import type { APIRoute } from "astro";
import { transactions, accounts } from "../../../data/store";
import type { Transaction } from "../../../types";
export const POST: APIRoute = async ({ request }) => {
try {
const transaction = (await request.json()) as Omit<Transaction, "id">;
// Validate required fields
if (
!transaction.accountId ||
!transaction.date ||
!transaction.description ||
transaction.amount === undefined
) {
return new Response(
JSON.stringify({ error: "Missing required fields" }),
{
status: 400,
headers: { "Content-Type": "application/json" },
}
);
}
// Validate account exists
const account = accounts.find((a) => a.id === transaction.accountId);
if (!account) {
return new Response(JSON.stringify({ error: "Account not found" }), {
status: 404,
headers: { "Content-Type": "application/json" },
});
}
// Create new transaction with generated ID
const newTransaction: Transaction = {
...transaction,
id: (transactions.length + 1).toString(), // Simple ID generation for demo
};
// Update account balance
account.balance += transaction.amount;
// Add to transactions array
transactions.push(newTransaction);
return new Response(JSON.stringify(newTransaction), {
status: 201,
headers: { "Content-Type": "application/json" },
});
} catch (error) {
return new Response(JSON.stringify({ error: "Invalid request body" }), {
status: 400,
headers: { "Content-Type": "application/json" },
});
}
};