finish interview

This commit is contained in:
Interview
2025-07-14 11:06:32 +08:00
parent 742c8f5931
commit a5006183b9
2 changed files with 107 additions and 79 deletions

View File

@ -4,27 +4,54 @@ import { Hono } from 'hono'
const app = new Hono()
const accounts = {
'1': {amount: 1000},
'2': {amount: 1000},
'1' : {amount: 1000},
'2' : {amount: 1000},
}
const transactions = []
const transactions :ITransferResponse[] = []
interface ITransferResponse {
amount: number;
sender: '1' | '2';
receiver: '1' | '2';
}
app.post('/transfer', async (c) => {
const body = await c.req.json();
const body : ITransferResponse = await c.req.json();
const { amount, sender, receiver } = body;
if (amount <= 0) {
return c.json({ error: 'Amount must be greater than 0' }, 1000);
return c.json({ error: 'Amount must be greater than 0' });
}
if (!Object.keys(accounts).includes(sender) || !Object.keys(accounts).includes(receiver)) {
return c.json({ error: 'Account does not exist.' });
}
const senderAccountAmt = accounts[sender].amount;
const receiverAccountAmt = accounts[receiver].amount;
if (amount > senderAccountAmt) {
return c.json({ error: 'Sender has insufficient balance' })
}
const newSenderAmount = senderAccountAmt - amount
const newReceiverAmount = receiverAccountAmt + amount
accounts[sender].amount = newSenderAmount;
accounts[receiver].amount = newReceiverAmount;
const transfer = {
amount,
sender,
receiver
}
console.log('transfer', transfe);
transactions.push(transfer)
console.log('transfer', transfer);
console.log('accounts', accounts);
console.log('transactions', transactions);
return c.json(transfer)
})