diff --git a/db/1.sqlite b/db/1.sqlite index b67f55d..5a615d4 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 new file mode 100644 index 0000000..4e2fcea --- /dev/null +++ b/src/inventory/handler.rs @@ -0,0 +1,65 @@ + +use axum::{extract::{ws::{close_code::STATUS, Message}, Path, State}, http::StatusCode, response::IntoResponse}; +use rusqlite::params; +use serde_json::json; + + + +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, + 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) }