Get accounts and balances

This commit is contained in:
2025-04-21 12:08:12 +02:00
parent f5b0ae49ca
commit 6ee250d872
5 changed files with 72 additions and 5 deletions

View File

@ -1,8 +1,9 @@
use crate::controllers::HttpResult;
use crate::extractors::account_extractor::AccountInPath;
use crate::extractors::auth_extractor::AuthExtractor;
use crate::services::accounts_service;
use crate::models::accounts::Account;
use crate::services::accounts_service::UpdateAccountQuery;
use crate::services::{accounts_service, movements_service};
use actix_web::{HttpResponse, web};
/// Create a new account
@ -16,9 +17,38 @@ pub async fn create(auth: AuthExtractor, req: web::Json<UpdateAccountQuery>) ->
Ok(HttpResponse::Created().finish())
}
#[derive(serde::Deserialize)]
pub struct GetListQuery {
#[serde(default)]
include_balances: bool,
}
#[derive(serde::Serialize)]
struct AccountAndBalance {
#[serde(flatten)]
account: Account,
balance: f32,
}
/// Get the list of accounts of the user
pub async fn get_list(auth: AuthExtractor) -> HttpResult {
Ok(HttpResponse::Ok().json(accounts_service::get_list_user(auth.user_id()).await?))
pub async fn get_list(auth: AuthExtractor, query: web::Query<GetListQuery>) -> HttpResult {
let accounts = accounts_service::get_list_user(auth.user_id()).await?;
Ok(match query.include_balances {
false => HttpResponse::Ok().json(accounts),
true => {
let balances = movements_service::get_balances(auth.user_id()).await?;
let accounts = accounts
.into_iter()
.map(|account| AccountAndBalance {
balance: *balances.get(&account.id()).unwrap_or(&0.0),
account,
})
.collect::<Vec<_>>();
HttpResponse::Ok().json(accounts)
}
})
}
/// Get a single account

View File

@ -17,6 +17,11 @@ pub async fn create(auth: AuthExtractor, req: web::Json<UpdateMovementQuery>) ->
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()