From 008fc377ec3020e4f56e6c55c6a53b85e1efba25 Mon Sep 17 00:00:00 2001 From: Romain Mallard Date: Fri, 26 Sep 2025 19:54:10 +0200 Subject: [PATCH] add history for inventory endpoint + small fix --- db/1.sqlite | Bin 53248 -> 57344 bytes src/inventory/handler.rs | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/db/1.sqlite b/db/1.sqlite index 68cbb20b8893b7b176143e5f461854f795806aec..e27249de1bcaaf06452d8955d1daf87c4a231a71 100644 GIT binary patch delta 1098 zcmcIjK}^$N6#oAfwzli~cY>J&h<~Ppkqov`>R_1Q#45xwade3>G3%5@7TJcQsF;9qb}kdb(?$=^Ty-h1Es-s^iyHF{}- zUTyac0{{eayyQ^ibW}r9_b}*t`q0amz~f#6D1f$w5Bv`SdOmVHe9*nds;rNzaFsXJ z&&@ZN2YqgTECyGQk-Iyq=ZlZ@qERX@W@qyA2e(m}YX~xX_xeQ$FU(En41GGAGY*3A z73FFolT`6&CY@GNYE~VeP^Q$x#AFXw0nh#6FkE39*X@X{H-bN*-nw1GrpMFG;Y?~g zamwY-gw0j4E4R4QjzM9 z^vk`Gq2B%rI2wz~k+?kA23@v9>8L3x#pUQ*tx04c+68DAeIkroDAYLg|<9vyhKg!zLNMeBf6x^D5c8oC{{HNkF)zQ3r?VS{WOeq1U)_$$2}j|>HgvO~|Y tJbCb+~AJjNg@CM delta 607 zcmZoTz}&Ead4jZ{2?GNIFA#GBF*6X0PSi10HDS=JD&*ziV&LRrW8hcfKg)N4=QSTA z_eHL2JQ`eVoYy(KIJ-6r3Yc&>HTv_ji|gt#HU*X>Cgr3S<>%+dXJi(a}0iZ4h1`hrYn*}#K;um0LW@Kbx0g=o= zz`?O8fkl9WiT?@%|1170K=V%U>xeM1GB7f#GpARk7ATYegI=LLzbIRT5i07D2?Pqz z7+KVyzyr2`fq@A~^K9l>uz-K!f)FlH3IN&7e;a7%MSgu{MpkKQVRcWx5Lb8CpnxDx nU&o+GZ`Vl2&=7x5KVTgCy84B1!8CmZI`%a`Hy5)sCsYRjWdN$B diff --git a/src/inventory/handler.rs b/src/inventory/handler.rs index 4e2fcea..0baf5f0 100644 --- a/src/inventory/handler.rs +++ b/src/inventory/handler.rs @@ -1,4 +1,5 @@ +use argon2::Params; use axum::{extract::{ws::{close_code::STATUS, Message}, Path, State}, http::StatusCode, response::IntoResponse}; use rusqlite::params; use serde_json::json; @@ -11,8 +12,7 @@ use crate::utils::{auth::AuthClaims, db_pool::AppState}; pub async fn create_inventory_item( State(state): State, - Path(item_name): Path, - Path(item_amount): Path, + Path((item_name, item_amount)): Path<(String, i32)>, AuthClaims{ user_id, hotel_id, username}: AuthClaims, ) -> impl IntoResponse { @@ -24,20 +24,19 @@ pub async fn create_inventory_item( }; let result = conn.execute( - "INSERT INTO invenstory (item_name, item_amount) VALUE (?1, ?2)", - params![&item_name,&item_amount] + "INSERT INTO inventory (item_name, amount, user_id) VALUES (?1, ?2, ?3)", + params![&item_name,&item_amount,&user_id] ); 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()) + Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("couldn't add the new item, err: {}", err )) } } pub async fn update_inventory_item( State(state): State, - Path(item_id): Path, - Path(item_amount): Path, + Path((item_id, item_amount)): Path<(i32, i32)>, AuthClaims { user_id, hotel_id, username }: AuthClaims, ) -> impl IntoResponse { @@ -52,14 +51,34 @@ pub async fn update_inventory_item( 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", + let result: Result = conn.query_row( + "UPDATE inventory SET amount = ?1, user_id=?3 WHERE id = ?2 RETURNING item_name", params![&item_amount, &item_id, &user_id], + |row| row.get(0), ); + + match result { + Ok(item_name) => { + if let Err(err) = conn.execute( + "INSERT INTO inventory_history (item_id, amount, item_name, user_id) VALUES (?1,?2,?3,?4)", + params![&item_id,&item_amount,&item_name, &user_id] + ){ + return (StatusCode::INTERNAL_SERVER_ERROR, format!("failed to update inventory history")); + } + (StatusCode::OK, format!("updated item history")) + } + + Ok(_) => (StatusCode::NOT_FOUND, "No room found".to_string()), + Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error from DB: {err}")), + } + +/* 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