From cceed381bd671712281629d55d9556d8c78b14b8 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 8 May 2025 17:42:49 +0200 Subject: [PATCH] Get only the number of inbox entries --- .../src/controllers/inbox_controller.rs | 20 +++++++++++++++++++ moneymgr_backend/src/main.rs | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/moneymgr_backend/src/controllers/inbox_controller.rs b/moneymgr_backend/src/controllers/inbox_controller.rs index 6badee3..4b5f94d 100644 --- a/moneymgr_backend/src/controllers/inbox_controller.rs +++ b/moneymgr_backend/src/controllers/inbox_controller.rs @@ -37,6 +37,26 @@ pub async fn get_list(auth: AuthExtractor, query: web::Query) -> Ok(HttpResponse::Ok().json(list)) } +#[derive(serde::Serialize)] +struct InboxCount { + count: usize, +} + +/// Count the number of inbox entries +pub async fn count_entries(auth: AuthExtractor, query: web::Query) -> HttpResult { + let mut list = inbox_service::get_list_user(auth.user_id()).await?; + + list.retain(|entry| { + if !query.include_attached && entry.movement_id().is_some() { + return false; + } + + true + }); + + Ok(HttpResponse::Ok().json(InboxCount { count: list.len() })) +} + /// Get a single inbox entry pub async fn get_single(entry: InboxEntryInPath) -> HttpResult { Ok(HttpResponse::Ok().json(entry.as_ref())) diff --git a/moneymgr_backend/src/main.rs b/moneymgr_backend/src/main.rs index 6817b80..319f631 100644 --- a/moneymgr_backend/src/main.rs +++ b/moneymgr_backend/src/main.rs @@ -158,6 +158,10 @@ async fn main() -> std::io::Result<()> { // Inbox controller .route("/api/inbox", web::post().to(inbox_controller::create)) .route("/api/inbox", web::get().to(inbox_controller::get_list)) + .route( + "/api/inbox/count", + web::get().to(inbox_controller::count_entries), + ) .route( "/api/inbox/{inbox_id}", web::get().to(inbox_controller::get_single),