Add a widget to select a movement

This commit is contained in:
2025-05-13 19:29:26 +02:00
parent 3772dce01c
commit 5e4de364e0
9 changed files with 269 additions and 58 deletions

View File

@ -22,10 +22,55 @@ pub async fn get_accounts_balances(auth: AuthExtractor) -> HttpResult {
Ok(HttpResponse::Ok().json(movements_service::get_balances(auth.user_id()).await?))
}
/// Select movements filter
#[derive(serde::Deserialize)]
pub struct SelectMovementFilters {
amount_min: Option<f32>,
amount_max: Option<f32>,
time_min: Option<i64>,
time_max: Option<i64>,
label: Option<String>,
limit: Option<usize>,
}
/// 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?))
pub async fn get_list_of_account(
account_id: AccountInPath,
query: web::Query<SelectMovementFilters>,
) -> HttpResult {
let mut list = movements_service::get_list_account(account_id.as_ref().id()).await?;
if let Some(amount_min) = query.amount_min {
list.retain(|l| l.amount >= amount_min);
}
if let Some(amount_max) = query.amount_max {
list.retain(|l| l.amount <= amount_max);
}
if let Some(time_min) = query.time_min {
list.retain(|l| l.time >= time_min);
}
if let Some(time_max) = query.time_max {
list.retain(|l| l.time <= time_max);
}
if let Some(label) = &query.label {
list.retain(|l| {
l.label
.to_lowercase()
.contains(label.to_lowercase().as_str())
});
}
if let Some(limit) = query.limit {
if list.len() > limit {
list = list[..limit].to_vec();
}
}
Ok(HttpResponse::Ok().json(list))
}
/// Get a single movement information