add history for inventory endpoint + small fix

This commit is contained in:
2025-09-26 19:54:10 +02:00
parent 9212ba53a1
commit 008fc377ec
2 changed files with 28 additions and 9 deletions

View File

@@ -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<AppState>,
Path(item_name): Path<String>,
Path(item_amount): Path<i32>,
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<AppState>,
Path(item_id): Path<i32>,
Path(item_amount): Path<i32>,
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<String, rusqlite::Error> = 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))
}
*/
}