40 lines
941 B
Rust
40 lines
941 B
Rust
use actix_web::{put, web, HttpResponse, Responder, Result};
|
|
use bank::account::AccountError;
|
|
use serde::Serialize;
|
|
|
|
use bank::bank::Bank;
|
|
|
|
use crate::handlers::error::HttpAccountError;
|
|
use crate::handlers::AmountData;
|
|
use crate::AppState;
|
|
|
|
#[derive(Serialize)]
|
|
struct AccountCreated {
|
|
nr: String,
|
|
}
|
|
|
|
#[put("/{nr}")]
|
|
pub async fn route(
|
|
info: web::Path<String>,
|
|
form: web::Form<AmountData>,
|
|
data: web::Data<AppState>,
|
|
) -> Result<impl Responder> {
|
|
let nr = info.into_inner();
|
|
let amount = form.amount;
|
|
|
|
if amount <= 0_f64 {
|
|
return Err(HttpAccountError(AccountError::InvalidAmount).into());
|
|
}
|
|
|
|
info!("updating account {}...", nr);
|
|
|
|
let bank = data.bank.read().unwrap();
|
|
match Bank::account_action(bank, &nr, |account| {
|
|
account.balance = amount;
|
|
Ok(amount)
|
|
}) {
|
|
Err(e) => Err(HttpAccountError(e).into()),
|
|
Ok(_) => Ok(HttpResponse::Ok().finish()),
|
|
}
|
|
}
|