initial commit
This commit is contained in:
commit
3d9c98eeca
22 changed files with 1075 additions and 0 deletions
27
src/commands/create_account.rs
Normal file
27
src/commands/create_account.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
use std::io::Write;
|
||||
use std::net::TcpStream;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::bank::Bank;
|
||||
use crate::commands::Command;
|
||||
use crate::protocol;
|
||||
|
||||
pub struct CreateAccount;
|
||||
|
||||
impl Command for CreateAccount {
|
||||
fn execute(&self, bank: Arc<RwLock<Bank>>, data: &[u8], mut stream: &TcpStream) -> Result<usize> {
|
||||
debug!("owner nr bytes {:?}", data);
|
||||
|
||||
let owner = String::from_utf8_lossy(data);
|
||||
info!("creating new account with owner {}...", owner);
|
||||
|
||||
let mut bank = bank.write().unwrap();
|
||||
let nr = bank.create_account(owner.into());
|
||||
info!("created account {}", nr);
|
||||
|
||||
let written = stream.write(&protocol::account_nr(&nr))?;
|
||||
Ok(written)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue