mirror of
https://github.com/microsoft/Web-Dev-For-Beginners.git
synced 2025-09-02 03:02:51 +02:00
Fix: removed broken API request in 7.bank solution
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
const serverUrl = 'http://localhost:5000/api';
|
const serverUrl = 'http://localhost:5000/api';
|
||||||
const storageKey = 'savedAccount';
|
const storageKey = 'savedAccount';
|
||||||
|
const accountsKey = 'accounts'; // New key for all accounts
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Router
|
// Router
|
||||||
@@ -32,7 +33,7 @@ function updateRoute() {
|
|||||||
const app = document.getElementById('app');
|
const app = document.getElementById('app');
|
||||||
app.innerHTML = '';
|
app.innerHTML = '';
|
||||||
app.appendChild(view);
|
app.appendChild(view);
|
||||||
|
|
||||||
if (typeof route.init === 'function') {
|
if (typeof route.init === 'function') {
|
||||||
route.init();
|
route.init();
|
||||||
}
|
}
|
||||||
@@ -41,32 +42,70 @@ function updateRoute() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// API interactions
|
// API interactions (replaced with localStorage logic)
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
async function sendRequest(api, method, body) {
|
function getAccounts() {
|
||||||
try {
|
return JSON.parse(localStorage.getItem(accountsKey) || '[]');
|
||||||
const response = await fetch(serverUrl + api, {
|
}
|
||||||
method: method || 'GET',
|
|
||||||
headers: body ? { 'Content-Type': 'application/json' } : undefined,
|
function saveAccounts(accounts) {
|
||||||
body
|
localStorage.setItem(accountsKey, JSON.stringify(accounts));
|
||||||
});
|
}
|
||||||
return await response.json();
|
|
||||||
} catch (error) {
|
function findAccount(user) {
|
||||||
return { error: error.message || 'Unknown error' };
|
const accounts = getAccounts();
|
||||||
}
|
return accounts.find(acc => acc.user === user) || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getAccount(user) {
|
async function getAccount(user) {
|
||||||
return sendRequest('/accounts/' + encodeURIComponent(user));
|
// Simulate async
|
||||||
|
return new Promise(resolve => {
|
||||||
|
setTimeout(() => {
|
||||||
|
const acc = findAccount(user);
|
||||||
|
if (!acc) resolve({ error: 'Account not found' });
|
||||||
|
else resolve(acc);
|
||||||
|
}, 100);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createAccount(account) {
|
async function createAccount(accountJson) {
|
||||||
return sendRequest('/accounts', 'POST', account);
|
return new Promise(resolve => {
|
||||||
|
setTimeout(() => {
|
||||||
|
const data = JSON.parse(accountJson);
|
||||||
|
if (!data.user) return resolve({ error: 'Username required' });
|
||||||
|
if (findAccount(data.user)) return resolve({ error: 'User already exists' });
|
||||||
|
// Set up initial account structure
|
||||||
|
const newAcc = {
|
||||||
|
user: data.user,
|
||||||
|
description: data.description || '',
|
||||||
|
balance: 0,
|
||||||
|
currency: data.currency || 'USD',
|
||||||
|
transactions: []
|
||||||
|
};
|
||||||
|
const accounts = getAccounts();
|
||||||
|
accounts.push(newAcc);
|
||||||
|
saveAccounts(accounts);
|
||||||
|
resolve(newAcc);
|
||||||
|
}, 100);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createTransaction(user, transaction) {
|
async function createTransaction(user, transactionJson) {
|
||||||
return sendRequest('/accounts/' + user + '/transactions', 'POST', transaction);
|
return new Promise(resolve => {
|
||||||
|
setTimeout(() => {
|
||||||
|
const accounts = getAccounts();
|
||||||
|
const idx = accounts.findIndex(acc => acc.user === user);
|
||||||
|
if (idx === -1) return resolve({ error: 'Account not found' });
|
||||||
|
const tx = JSON.parse(transactionJson);
|
||||||
|
tx.amount = parseFloat(tx.amount);
|
||||||
|
tx.date = tx.date || new Date().toISOString().slice(0, 10);
|
||||||
|
accounts[idx].balance += tx.amount;
|
||||||
|
accounts[idx].transactions.push(tx);
|
||||||
|
saveAccounts(accounts);
|
||||||
|
resolve(tx);
|
||||||
|
}, 100);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
Reference in New Issue
Block a user