Display global statistics
This commit is contained in:
@ -8,6 +8,7 @@ pub mod files_controller;
|
||||
pub mod movement_controller;
|
||||
pub mod server_controller;
|
||||
pub mod static_controller;
|
||||
pub mod stats_controller;
|
||||
pub mod tokens_controller;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
|
25
moneymgr_backend/src/controllers/stats_controller.rs
Normal file
25
moneymgr_backend/src/controllers/stats_controller.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use crate::controllers::HttpResult;
|
||||
use crate::extractors::auth_extractor::AuthExtractor;
|
||||
use crate::services::{files_service, movements_service};
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct GlobalStats {
|
||||
global_balance: f32,
|
||||
number_movements: usize,
|
||||
number_files: usize,
|
||||
total_files_size: u64,
|
||||
}
|
||||
|
||||
/// Get global statistics
|
||||
pub async fn global(auth: AuthExtractor) -> HttpResult {
|
||||
let movements = movements_service::get_all_movements_user(auth.user.id()).await?;
|
||||
let files = files_service::get_all_files_user(auth.user.id()).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(GlobalStats {
|
||||
global_balance: movements.iter().fold(0.0, |sum, m| sum + m.amount),
|
||||
number_movements: movements.len(),
|
||||
number_files: files.len(),
|
||||
total_files_size: files.iter().fold(0, |sum, m| sum + m.file_size as u64),
|
||||
}))
|
||||
}
|
Reference in New Issue
Block a user