diff --git a/db/1.sqlite b/db/1.sqlite index 68cbb20..e27249d 100644 Binary files a/db/1.sqlite and b/db/1.sqlite differ 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