MoneyMgr/moneymgr_backend/src/controllers/movement_controller.rs

59 lines
2.0 KiB
Rust

use crate::controllers::HttpResult;
use crate::extractors::account_extractor::AccountInPath;
use crate::extractors::auth_extractor::AuthExtractor;
use crate::extractors::movement_extractor::MovementInPath;
use crate::services::movements_service;
use crate::services::movements_service::UpdateMovementQuery;
use actix_web::{HttpResponse, web};
/// Create a new movement
pub async fn create(auth: AuthExtractor, req: web::Json<UpdateMovementQuery>) -> HttpResult {
if let Some(err) = req.check_error(auth.user_id(), None).await? {
return Ok(HttpResponse::BadRequest().json(err));
}
movements_service::create(&req).await?;
Ok(HttpResponse::Created().finish())
}
/// Get the balances of all the accounts of the user
pub async fn get_accounts_balances(auth: AuthExtractor) -> HttpResult {
Ok(HttpResponse::Ok().json(movements_service::get_balances(auth.user_id()).await?))
}
/// Get the list of movements of an account
pub async fn get_list_of_account(account_id: AccountInPath) -> HttpResult {
Ok(HttpResponse::Ok()
.json(movements_service::get_list_account(account_id.as_ref().id()).await?))
}
/// Get a single movement information
pub async fn get_single(movement: MovementInPath) -> HttpResult {
Ok(HttpResponse::Ok().json(movement.movement()))
}
/// Update a single movement information
pub async fn update(
auth: AuthExtractor,
movement: MovementInPath,
req: web::Json<UpdateMovementQuery>,
) -> HttpResult {
if let Some(err) = req
.check_error(auth.user_id(), Some(movement.movement().id()))
.await?
{
return Ok(HttpResponse::BadRequest().json(err));
}
movements_service::update(movement.movement().id(), &req).await?;
Ok(HttpResponse::Ok().json(movements_service::get_by_id(movement.movement().id()).await?))
}
/// Delete a movement
pub async fn delete(movement: MovementInPath) -> HttpResult {
movements_service::delete(movement.movement().id()).await?;
Ok(HttpResponse::Accepted().finish())
}