-
Notifications
You must be signed in to change notification settings - Fork 14k
Fix: Removed broken API request from 7.bank solution (Fixes #1384) #1437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ | |||||||||||||||
|
||||||||||||||||
const serverUrl = 'http://localhost:5000/api'; | ||||||||||||||||
const storageKey = 'savedAccount'; | ||||||||||||||||
const accountsKey = 'accounts'; // New key for all accounts | ||||||||||||||||
|
||||||||||||||||
// --------------------------------------------------------------------------- | ||||||||||||||||
// Router | ||||||||||||||||
|
@@ -32,7 +33,7 @@ function updateRoute() { | |||||||||||||||
const app = document.getElementById('app'); | ||||||||||||||||
app.innerHTML = ''; | ||||||||||||||||
app.appendChild(view); | ||||||||||||||||
|
||||||||||||||||
if (typeof route.init === 'function') { | ||||||||||||||||
route.init(); | ||||||||||||||||
} | ||||||||||||||||
|
@@ -41,32 +42,75 @@ function updateRoute() { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// --------------------------------------------------------------------------- | ||||||||||||||||
// API interactions | ||||||||||||||||
// API interactions (replaced with localStorage logic) | ||||||||||||||||
// --------------------------------------------------------------------------- | ||||||||||||||||
|
||||||||||||||||
async function sendRequest(api, method, body) { | ||||||||||||||||
try { | ||||||||||||||||
const response = await fetch(serverUrl + api, { | ||||||||||||||||
method: method || 'GET', | ||||||||||||||||
headers: body ? { 'Content-Type': 'application/json' } : undefined, | ||||||||||||||||
body | ||||||||||||||||
}); | ||||||||||||||||
return await response.json(); | ||||||||||||||||
} catch (error) { | ||||||||||||||||
return { error: error.message || 'Unknown error' }; | ||||||||||||||||
} | ||||||||||||||||
function getAccounts() { | ||||||||||||||||
return JSON.parse(localStorage.getItem(accountsKey) || '[]'); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
function saveAccounts(accounts) { | ||||||||||||||||
localStorage.setItem(accountsKey, JSON.stringify(accounts)); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
function findAccount(user) { | ||||||||||||||||
const accounts = getAccounts(); | ||||||||||||||||
return accounts.find(acc => acc.user === user) || null; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
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) { | ||||||||||||||||
return sendRequest('/accounts', 'POST', account); | ||||||||||||||||
async function createAccount(accountJson) { | ||||||||||||||||
return new Promise(resolve => { | ||||||||||||||||
setTimeout(() => { | ||||||||||||||||
let data; | ||||||||||||||||
try { | ||||||||||||||||
data = JSON.parse(accountJson); | ||||||||||||||||
} catch (e) { | ||||||||||||||||
return resolve({ error: 'Malformed account data' }); | ||||||||||||||||
} | ||||||||||||||||
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) { | ||||||||||||||||
return sendRequest('/accounts/' + user + '/transactions', 'POST', transaction); | ||||||||||||||||
async function createTransaction(user, transactionJson) { | ||||||||||||||||
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); | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSON.parse without error handling can throw an exception if transactionJson is invalid JSON. Wrap in try-catch to handle malformed input gracefully.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||
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); | ||||||||||||||||
}); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// --------------------------------------------------------------------------- | ||||||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] This message constants addition appears unrelated to the main PR purpose of fixing the bank project API. Consider moving this to a separate PR for better change isolation.
Copilot uses AI. Check for mistakes.