From 1dc71e451b1e80a468e230503a94f01f8f8a730b Mon Sep 17 00:00:00 2001 From: Romain Mallard Date: Fri, 26 Sep 2025 16:51:08 +0200 Subject: [PATCH] add inventory endpoint --- db/1.sqlite | Bin 40960 -> 40960 bytes src/inventory/handler.rs | 65 +++++++++++++++++++++++++++++++++++++++ src/inventory/mod.rs | 3 ++ src/inventory/routes.rs | 13 ++++++++ src/lib.rs | 1 + src/main.rs | 1 + src/routes/mod.rs | 3 ++ 7 files changed, 86 insertions(+) create mode 100644 src/inventory/handler.rs create mode 100644 src/inventory/mod.rs create mode 100644 src/inventory/routes.rs diff --git a/db/1.sqlite b/db/1.sqlite index b67f55d89188480caef8cdf623df9f93c6355a29..5a615d406e88a23854cbfe0e23be92a18dec87eb 100644 GIT binary patch delta 434 zcmZoTz|?SnX@az%76StV7Z7s*F(VN3Pt-A%*J99X)8yshV&LGq#=x({f0pk8&uczL z?u%U4cr>`!H#T;1x;6Unu#2m!Gd6{nBqrsgX6BWp=9T0ZRmPX(7NBt0oP%5)LtGU? z9G!ez6%tV75+-wTYnvtJ=9lJ`Bq(_Lg}A!A2I&A5m89mz=OyN*CMblsMug}nloqEJ z#b>5~lmMkm3sMqGChy_Z*nEx4oH0aBTw0p3Ss3h&+|=UY#Prlg4~Xj;9e}jFJY$nT zsw?5HKy{rGTzoPow+mi*v!H+v7f%xl7kd)}7rVH!GUMiJT;`1S55=XW8JmSm5|eULb5o0p6VqW7 zhjWmtV~DFlh@+E_t3m=)GC@HjAu}aG!P76q)!j8{vJv, + Path(item_name): Path, + Path(item_amount): Path, + AuthClaims{ user_id, hotel_id, username}: AuthClaims, +) -> impl IntoResponse { + + let pool = state.hotel_pools.get_pool(hotel_id); + + let conn = match pool.get(){ + Ok(conn) => conn, + Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("couldn't open the connection")) + }; + + let result = conn.execute( + "INSERT INTO invenstory (item_name, item_amount) VALUE (?1, ?2)", + params![&item_name,&item_amount] + ); + match result { + Ok(rows) => (StatusCode::OK, format!("inserted item {item_name}, with {item_amount} amount")), + Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, "couldn't add the new item".into()) + } +} + + +pub async fn update_inventory_item( + State(state): State, + Path(item_id): Path, + Path(item_amount): Path, + AuthClaims { user_id, hotel_id, username }: AuthClaims, +) -> impl IntoResponse { + + //TODO: make better error handling : + // if wrong param collumn targeted, + // if missing path param + + let pool = state.hotel_pools.get_pool(hotel_id); + + let conn = match pool.get(){ + Ok(conn) => conn, + Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Pool error: {err}")), + }; + + let result = conn.execute( + "UPDATE inventory SET amount = ?1, user_id=?3 WHERE id = ?2", + params![&item_amount, &item_id, &user_id], + ); + + match result { + Ok(row) => (StatusCode::OK, format!("Items updated")), + Ok(_) => (StatusCode::NOT_FOUND, format!("No item with this id exist")), + Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("error updating the item with id :{} with amount: {}", item_id, item_amount)) + } +} \ No newline at end of file diff --git a/src/inventory/mod.rs b/src/inventory/mod.rs new file mode 100644 index 0000000..5a539d5 --- /dev/null +++ b/src/inventory/mod.rs @@ -0,0 +1,3 @@ +mod handler; + +pub mod routes; \ No newline at end of file diff --git a/src/inventory/routes.rs b/src/inventory/routes.rs new file mode 100644 index 0000000..4ffcd7a --- /dev/null +++ b/src/inventory/routes.rs @@ -0,0 +1,13 @@ +use axum::{ + routing::{get, put, post}, + Router, +}; + +use crate::{inventory::handler::*, utils::db_pool::AppState}; + +pub fn inventory_routes() -> Router { + + Router::new() + .route("/update_item/{item_id}/{item_amount}", put(update_inventory_item)) + .route("/add_item/{item_name/{items_amount}", post(create_inventory_item)) +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 80763e0..0128a59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,3 +5,4 @@ pub mod utils; pub mod routes; pub mod rooms; pub mod chat; +pub mod inventory; diff --git a/src/main.rs b/src/main.rs index b0828a2..f6a0036 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod utils; mod routes; mod rooms; mod chat; +mod inventory; use r2d2::{Pool}; use r2d2_sqlite::SqliteConnectionManager; use dashmap::DashMap; diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 13549a1..61988f2 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -7,6 +7,7 @@ use r2d2::{Pool}; use crate::rooms::routes::rooms_routes; use crate::utils::routes::utils_routes; use crate::chat::routes::chat_routes; +use crate::inventory::routes::inventory_routes; use crate::utils::db_pool::{AppState}; //TODO: add secret fomr dotenv here @@ -21,6 +22,8 @@ pub fn create_router(state: AppState) -> Router { .nest("/auth", utils_routes().with_state(state.clone())) .nest("/rooms", rooms_routes().with_state(state.clone())) .nest("/chat", chat_routes().with_state(state.clone())) + .nest("/inventory", inventory_routes().with_state(state.clone())) + .with_state(state) }