diff --git a/.vscode/settings.json b/.vscode/settings.json index a3f0e00..0d5a97c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,20 @@ "terminal.integrated.enablePersistentSessions": true, "terminal.integrated.persistentSessionReviveProcess": "onExitAndWindowClose", "terminal.integrated.enableMultiLinePasteWarning": "auto", - "terminal.integrated.splitCwd": "workspaceRoot" + "terminal.integrated.splitCwd": "workspaceRoot", + "rest-client.environmentVariables": { + "$shared": { + "version": "v1" + }, + "development": { + "host": "localhost", + "port": "4321", + "baseUrl": "http://localhost:4321/api" + }, + "production": { + "host": "finance-app-production.example.com", + "port": "443", + "baseUrl": "https://finance-app-production.example.com/api" + } + } } diff --git a/bank_account.http b/bank_account.http index 4e5077e..fa5449d 100644 --- a/bank_account.http +++ b/bank_account.http @@ -1,9 +1,57 @@ -POST /api/bank-account/update/2 HTTP/1.1 +@baseUrl = {{baseUrl}} +@host = {{host}} +@port = {{port}} + +# Bank Account API Testing + +### Get all accounts +GET {{baseUrl}}/accounts +Accept: application/json + +### Get a specific account +GET {{baseUrl}}/accounts/1 +Accept: application/json + +### Get transactions for an account +GET {{baseUrl}}/accounts/1/transactions +Accept: application/json + +### Create a new account +POST {{baseUrl}}/accounts Content-Type: application/json -Host: europa:3050 -Content-Length: 85 -{"name": "BofA Joint Checking","bankName": "Bank of America","accountNumber": "4581"} +{ + "name": "BofA Joint Checking", + "bankName": "Bank of America", + "accountNumber": "4581", + "balance": 1500, + "type": "CHECKING", + "status": "ACTIVE" +} -### +### Create a new transaction +POST {{baseUrl}}/transactions +Content-Type: application/json + +{ + "accountId": "1", + "date": "2025-05-06", + "description": "Coffee Shop", + "amount": -12.50, + "category": "Food & Dining", + "type": "WITHDRAWAL" +} + +### Update a transaction +PUT {{baseUrl}}/transactions/1 +Content-Type: application/json + +{ + "description": "Updated Coffee Purchase", + "amount": -15.75, + "category": "Food & Dining" +} + +### Delete a transaction +DELETE {{baseUrl}}/transactions/1 diff --git a/src/pages/api/accounts/index.ts b/src/pages/api/accounts/index.ts index 43ad21d..07b6dc4 100644 --- a/src/pages/api/accounts/index.ts +++ b/src/pages/api/accounts/index.ts @@ -1,5 +1,6 @@ import type { APIRoute } from 'astro'; -import { accountService } from '../../../data/db.service'; +import { AccountStatus, AccountType, accountService } from '../../../data/db.service'; +import type { Account } from '../../../types'; export const GET: APIRoute = async () => { try { @@ -21,3 +22,44 @@ export const GET: APIRoute = async () => { }); } }; + +export const POST: APIRoute = async ({ request }) => { + try { + const accountData = (await request.json()) as Omit; + + // Validate required fields + if (!accountData.name || !accountData.bankName || !accountData.accountNumber) { + return new Response( + JSON.stringify({ + error: 'Missing required fields: name, bankName, and accountNumber are required', + }), + { + status: 400, + headers: { 'Content-Type': 'application/json' }, + }, + ); + } + + // Set default values if not provided + const data = { + ...accountData, + balance: accountData.balance || 0, + type: accountData.type || AccountType.CHECKING, + status: accountData.status || AccountStatus.ACTIVE, + }; + + // Create the account + const newAccount = await accountService.create(data); + + return new Response(JSON.stringify(newAccount), { + status: 201, + headers: { 'Content-Type': 'application/json' }, + }); + } catch (error) { + console.error('Error creating account:', error); + return new Response(JSON.stringify({ error: 'Failed to create account' }), { + status: 500, + headers: { 'Content-Type': 'application/json' }, + }); + } +};