Add a route to get the list of inbox entries

This commit is contained in:
Pierre HUBERT 2025-05-08 16:34:00 +02:00
parent b6281be349
commit df7f395e8b
4 changed files with 34 additions and 5 deletions

View File

@ -14,3 +14,24 @@ pub async fn create(auth: AuthExtractor, req: web::Json<UpdateInboxEntryQuery>)
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<GetInboxQuery>) -> 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))
}

View File

@ -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(

View File

@ -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)

View File

@ -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<Inbox> {
pub async fn create(user_id: UserID, query: &UpdateInboxEntryQuery) -> anyhow::Result<InboxEntry> {
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<Vec<InboxEntry>> {
Ok(inbox::table
.filter(inbox::dsl::user_id.eq(user_id.0))
.get_results(&mut db()?)?)
}