Skip to content

Commit 85cc30f

Browse files
Fix: removed broken API request in 7.bank solution
1 parent d784884 commit 85cc30f

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

7-bank-project/solution/app.js

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const serverUrl = 'http://localhost:5000/api';
66
const storageKey = 'savedAccount';
7+
const accountsKey = 'accounts'; // New key for all accounts
78

89
// ---------------------------------------------------------------------------
910
// Router
@@ -32,7 +33,7 @@ function updateRoute() {
3233
const app = document.getElementById('app');
3334
app.innerHTML = '';
3435
app.appendChild(view);
35-
36+
3637
if (typeof route.init === 'function') {
3738
route.init();
3839
}
@@ -41,32 +42,70 @@ function updateRoute() {
4142
}
4243

4344
// ---------------------------------------------------------------------------
44-
// API interactions
45+
// API interactions (replaced with localStorage logic)
4546
// ---------------------------------------------------------------------------
4647

47-
async function sendRequest(api, method, body) {
48-
try {
49-
const response = await fetch(serverUrl + api, {
50-
method: method || 'GET',
51-
headers: body ? { 'Content-Type': 'application/json' } : undefined,
52-
body
53-
});
54-
return await response.json();
55-
} catch (error) {
56-
return { error: error.message || 'Unknown error' };
57-
}
48+
function getAccounts() {
49+
return JSON.parse(localStorage.getItem(accountsKey) || '[]');
50+
}
51+
52+
function saveAccounts(accounts) {
53+
localStorage.setItem(accountsKey, JSON.stringify(accounts));
54+
}
55+
56+
function findAccount(user) {
57+
const accounts = getAccounts();
58+
return accounts.find(acc => acc.user === user) || null;
5859
}
5960

6061
async function getAccount(user) {
61-
return sendRequest('/accounts/' + encodeURIComponent(user));
62+
// Simulate async
63+
return new Promise(resolve => {
64+
setTimeout(() => {
65+
const acc = findAccount(user);
66+
if (!acc) resolve({ error: 'Account not found' });
67+
else resolve(acc);
68+
}, 100);
69+
});
6270
}
6371

64-
async function createAccount(account) {
65-
return sendRequest('/accounts', 'POST', account);
72+
async function createAccount(accountJson) {
73+
return new Promise(resolve => {
74+
setTimeout(() => {
75+
const data = JSON.parse(accountJson);
76+
if (!data.user) return resolve({ error: 'Username required' });
77+
if (findAccount(data.user)) return resolve({ error: 'User already exists' });
78+
// Set up initial account structure
79+
const newAcc = {
80+
user: data.user,
81+
description: data.description || '',
82+
balance: 0,
83+
currency: data.currency || 'USD',
84+
transactions: []
85+
};
86+
const accounts = getAccounts();
87+
accounts.push(newAcc);
88+
saveAccounts(accounts);
89+
resolve(newAcc);
90+
}, 100);
91+
});
6692
}
6793

68-
async function createTransaction(user, transaction) {
69-
return sendRequest('/accounts/' + user + '/transactions', 'POST', transaction);
94+
async function createTransaction(user, transactionJson) {
95+
return new Promise(resolve => {
96+
setTimeout(() => {
97+
const accounts = getAccounts();
98+
const idx = accounts.findIndex(acc => acc.user === user);
99+
if (idx === -1) return resolve({ error: 'Account not found' });
100+
const tx = JSON.parse(transactionJson);
101+
tx.amount = parseFloat(tx.amount);
102+
tx.date = tx.date || new Date().toISOString().slice(0, 10);
103+
accounts[idx].balance += tx.amount;
104+
accounts[idx].transactions.push(tx);
105+
saveAccounts(accounts);
106+
resolve(tx);
107+
}, 100);
108+
});
70109
}
71110

72111
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)