use actix_web::{patch, web, HttpResponse, Responder, Result}; use serde::{Deserialize, Serialize}; use bank::account::AccountError; use crate::handlers::error::HttpAccountError; use crate::AppState; #[derive(Deserialize, Serialize)] pub struct TransferData { pub(crate) from: String, pub(crate) to: String, pub(crate) amount: f64, } #[patch("/transfer")] pub async fn route( form: web::Form, data: web::Data, ) -> Result { let from = form.from.clone(); let to = form.to.clone(); let amount = form.amount; info!("transferring {} from {} to {}...", from, to, amount); let bank = data.bank.read().unwrap(); let from_acc = bank .accounts .get(&from) .ok_or(HttpAccountError(AccountError::NotFound))?; let mut from_acc = from_acc.write().unwrap(); let to_acc = bank .accounts .get(&to) .ok_or(HttpAccountError(AccountError::NotFound))?; let mut to_acc = to_acc.write().unwrap(); match bank.transfer(&mut from_acc, &mut to_acc, amount) { Err(e) => Err(HttpAccountError(e).into()), Ok(_) => Ok(HttpResponse::Ok().finish()), } }