30 lines
876 B
Rust
30 lines
876 B
Rust
use actix_web::{delete, web, HttpResponse, Responder, Result};
|
|
|
|
use bank::account::AccountError;
|
|
use bank::bank::Bank;
|
|
|
|
use crate::handlers::error::HttpAccountError;
|
|
use crate::AppState;
|
|
|
|
#[delete("/{nr}")]
|
|
pub async fn route(info: web::Path<String>, data: web::Data<AppState>) -> Result<impl Responder> {
|
|
let nr = info.into_inner();
|
|
|
|
info!("closing account {}...", nr);
|
|
|
|
let bank = data.bank.read().unwrap();
|
|
match Bank::account_action(bank, &nr, |account| {
|
|
// TODO: make the error handling part of the passivate method
|
|
if account.balance > 0_f64 {
|
|
Err(AccountError::AccountNotZero)
|
|
} else if account.passivate() {
|
|
Ok(0_f64)
|
|
} else {
|
|
Err(AccountError::Inactive)
|
|
}
|
|
}) {
|
|
Err(e) => Err(HttpAccountError(e).into()),
|
|
Ok(_) => Ok(HttpResponse::Ok().finish()),
|
|
}
|
|
}
|