Add a route to get the list of inbox entries
This commit is contained in:
parent
b6281be349
commit
df7f395e8b
@ -14,3 +14,24 @@ pub async fn create(auth: AuthExtractor, req: web::Json<UpdateInboxEntryQuery>)
|
|||||||
|
|
||||||
Ok(HttpResponse::Created().finish())
|
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))
|
||||||
|
}
|
||||||
|
@ -157,6 +157,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
)
|
)
|
||||||
// Inbox controller
|
// Inbox controller
|
||||||
.route("/api/inbox", web::post().to(inbox_controller::create))
|
.route("/api/inbox", web::post().to(inbox_controller::create))
|
||||||
|
.route("/api/inbox", web::get().to(inbox_controller::get_list))
|
||||||
// Statistics controller
|
// Statistics controller
|
||||||
.route("/api/stats/global", web::get().to(stats_controller::global))
|
.route("/api/stats/global", web::get().to(stats_controller::global))
|
||||||
.route(
|
.route(
|
||||||
|
@ -9,7 +9,7 @@ pub struct InboxID(pub i32);
|
|||||||
|
|
||||||
/// Single inbox entry information
|
/// Single inbox entry information
|
||||||
#[derive(Queryable, Debug, Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Queryable, Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct Inbox {
|
pub struct InboxEntry {
|
||||||
/// The ID of the inbox entry
|
/// The ID of the inbox entry
|
||||||
id: i32,
|
id: i32,
|
||||||
/// ID of the file attached to this inbox entry
|
/// ID of the file attached to this inbox entry
|
||||||
@ -30,7 +30,7 @@ pub struct Inbox {
|
|||||||
pub time_update: i64,
|
pub time_update: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Inbox {
|
impl InboxEntry {
|
||||||
/// Get the ID of the inbox entry
|
/// Get the ID of the inbox entry
|
||||||
pub fn id(&self) -> InboxID {
|
pub fn id(&self) -> InboxID {
|
||||||
InboxID(self.id)
|
InboxID(self.id)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::connections::db_connection::db;
|
use crate::connections::db_connection::db;
|
||||||
use crate::controllers::server_controller::ServerConstraints;
|
use crate::controllers::server_controller::ServerConstraints;
|
||||||
use crate::models::files::FileID;
|
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::movements::MovementID;
|
||||||
use crate::models::users::UserID;
|
use crate::models::users::UserID;
|
||||||
use crate::schema::inbox;
|
use crate::schema::inbox;
|
||||||
@ -55,7 +55,7 @@ impl UpdateInboxEntryQuery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new inbox entry
|
/// 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 {
|
let new_entry = NewInboxEntry {
|
||||||
time: query.time as i64,
|
time: query.time as i64,
|
||||||
label: query.label.as_deref(),
|
label: query.label.as_deref(),
|
||||||
@ -67,7 +67,7 @@ pub async fn create(user_id: UserID, query: &UpdateInboxEntryQuery) -> anyhow::R
|
|||||||
movement_id: None,
|
movement_id: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let res: Inbox = diesel::insert_into(inbox::table)
|
let res: InboxEntry = diesel::insert_into(inbox::table)
|
||||||
.values(&new_entry)
|
.values(&new_entry)
|
||||||
.get_result(&mut db()?)?;
|
.get_result(&mut db()?)?;
|
||||||
|
|
||||||
@ -91,3 +91,10 @@ pub async fn update(id: InboxID, q: &UpdateInboxEntryQuery) -> anyhow::Result<()
|
|||||||
|
|
||||||
Ok(())
|
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()?)?)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user