From df7f395e8b268b0b2a5c56991bd22d39b196282f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 8 May 2025 16:34:00 +0200 Subject: [PATCH] Add a route to get the list of inbox entries --- .../src/controllers/inbox_controller.rs | 21 +++++++++++++++++++ moneymgr_backend/src/main.rs | 1 + moneymgr_backend/src/models/inbox.rs | 4 ++-- .../src/services/inbox_service.rs | 13 +++++++++--- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/moneymgr_backend/src/controllers/inbox_controller.rs b/moneymgr_backend/src/controllers/inbox_controller.rs index 9f57f26..fe0e18f 100644 --- a/moneymgr_backend/src/controllers/inbox_controller.rs +++ b/moneymgr_backend/src/controllers/inbox_controller.rs @@ -14,3 +14,24 @@ pub async fn create(auth: AuthExtractor, req: web::Json) Ok(HttpResponse::Created().finish()) } + +#[derive(serde::Deserialize)] +pub struct GetInboxQuery { + #[serde(default)] + include_attached: bool, +} + +/// Get inbox content +pub async fn get_list(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(list)) +} diff --git a/moneymgr_backend/src/main.rs b/moneymgr_backend/src/main.rs index e410943..b40d987 100644 --- a/moneymgr_backend/src/main.rs +++ b/moneymgr_backend/src/main.rs @@ -157,6 +157,7 @@ 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)) // Statistics controller .route("/api/stats/global", web::get().to(stats_controller::global)) .route( diff --git a/moneymgr_backend/src/models/inbox.rs b/moneymgr_backend/src/models/inbox.rs index e90e462..086d1c3 100644 --- a/moneymgr_backend/src/models/inbox.rs +++ b/moneymgr_backend/src/models/inbox.rs @@ -9,7 +9,7 @@ pub struct InboxID(pub i32); /// Single inbox entry information #[derive(Queryable, Debug, Clone, serde::Serialize, serde::Deserialize)] -pub struct Inbox { +pub struct InboxEntry { /// The ID of the inbox entry id: i32, /// ID of the file attached to this inbox entry @@ -30,7 +30,7 @@ pub struct Inbox { pub time_update: i64, } -impl Inbox { +impl InboxEntry { /// Get the ID of the inbox entry pub fn id(&self) -> InboxID { InboxID(self.id) diff --git a/moneymgr_backend/src/services/inbox_service.rs b/moneymgr_backend/src/services/inbox_service.rs index f186be9..6ee2d1e 100644 --- a/moneymgr_backend/src/services/inbox_service.rs +++ b/moneymgr_backend/src/services/inbox_service.rs @@ -1,7 +1,7 @@ use crate::connections::db_connection::db; use crate::controllers::server_controller::ServerConstraints; use crate::models::files::FileID; -use crate::models::inbox::{Inbox, InboxID, NewInboxEntry}; +use crate::models::inbox::{InboxEntry, InboxID, NewInboxEntry}; use crate::models::movements::MovementID; use crate::models::users::UserID; use crate::schema::inbox; @@ -55,7 +55,7 @@ impl UpdateInboxEntryQuery { } /// Create a new inbox entry -pub async fn create(user_id: UserID, query: &UpdateInboxEntryQuery) -> anyhow::Result { +pub async fn create(user_id: UserID, query: &UpdateInboxEntryQuery) -> anyhow::Result { let new_entry = NewInboxEntry { time: query.time as i64, label: query.label.as_deref(), @@ -67,7 +67,7 @@ pub async fn create(user_id: UserID, query: &UpdateInboxEntryQuery) -> anyhow::R movement_id: None, }; - let res: Inbox = diesel::insert_into(inbox::table) + let res: InboxEntry = diesel::insert_into(inbox::table) .values(&new_entry) .get_result(&mut db()?)?; @@ -91,3 +91,10 @@ pub async fn update(id: InboxID, q: &UpdateInboxEntryQuery) -> anyhow::Result<() Ok(()) } + +/// Get the full list of inbox entries of a user +pub async fn get_list_user(user_id: UserID) -> anyhow::Result> { + Ok(inbox::table + .filter(inbox::dsl::user_id.eq(user_id.0)) + .get_results(&mut db()?)?) +}