Implement the http variant of the bank server.

During that process, many shortcomings with the socket server and the
bank lib were fixed.

I am aware a massive commit like this is not ideal.
This commit is contained in:
Sebastian Hugentobler 2022-03-18 19:35:34 +01:00
parent c69654a924
commit dac95b7dae
Signed by: shu
GPG key ID: BB32CF3CA052C2F0
34 changed files with 1797 additions and 140 deletions

View file

@ -1,38 +1,61 @@
use std::collections::{HashMap, HashSet};
use std::sync::RwLock;
use std::sync::{RwLock, RwLockReadGuard};
#[cfg(test)]
use anyhow::Result;
use crate::account::Account;
use crate::account::{Account, AccountError};
#[derive(Default)]
pub struct Bank {
pub accounts: HashMap<String, RwLock<Account>>,
}
impl Bank {
pub fn new() -> Self {
Self { accounts: HashMap::new() }
Default::default()
}
pub fn account_action<F: Fn(&mut Account) -> Result<f64, AccountError>>(
bank: RwLockReadGuard<'_, Bank>,
nr: &str,
action: F,
) -> Result<f64, AccountError> {
match bank.accounts.get(nr) {
None => Err(AccountError::NotFound),
Some(account) => {
let mut account = account.write().unwrap();
action(&mut account)
}
}
}
pub fn account_numbers(&self) -> HashSet<String> {
self.accounts.iter()
self.accounts
.iter()
.filter(|(_, acc)| acc.read().unwrap().is_active)
.map(|(_, acc)| acc.read().unwrap().number.clone())
.collect()
}
pub fn create_account(&mut self, owner: String) -> String {
let acc = Account { owner, ..Default::default() };
let number = acc.number.clone();
let acc = Account {
owner,
..Default::default()
};
let nr = acc.number.clone();
self.accounts.insert(acc.number.clone(), RwLock::new(acc));
number
nr
}
#[cfg(test)]
pub fn transfer(&self, from: &mut Account, to: &mut Account, amount: f64) -> Result<()> {
pub fn transfer(
&self,
from: &mut Account,
to: &mut Account,
amount: f64,
) -> Result<(), AccountError> {
from.withdraw(amount)?;
to.deposit(amount)?;
@ -83,4 +106,4 @@ mod tests {
assert_eq!(1, bank.account_numbers().len());
}
}
}