add inventory endpoint
This commit is contained in:
BIN
db/1.sqlite
BIN
db/1.sqlite
Binary file not shown.
65
src/inventory/handler.rs
Normal file
65
src/inventory/handler.rs
Normal file
@@ -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<AppState>,
|
||||||
|
Path(item_name): Path<String>,
|
||||||
|
Path(item_amount): Path<i32>,
|
||||||
|
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<AppState>,
|
||||||
|
Path(item_id): Path<i32>,
|
||||||
|
Path(item_amount): Path<i32>,
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
3
src/inventory/mod.rs
Normal file
3
src/inventory/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
mod handler;
|
||||||
|
|
||||||
|
pub mod routes;
|
||||||
13
src/inventory/routes.rs
Normal file
13
src/inventory/routes.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use axum::{
|
||||||
|
routing::{get, put, post},
|
||||||
|
Router,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{inventory::handler::*, utils::db_pool::AppState};
|
||||||
|
|
||||||
|
pub fn inventory_routes() -> Router<AppState> {
|
||||||
|
|
||||||
|
Router::new()
|
||||||
|
.route("/update_item/{item_id}/{item_amount}", put(update_inventory_item))
|
||||||
|
.route("/add_item/{item_name/{items_amount}", post(create_inventory_item))
|
||||||
|
}
|
||||||
@@ -5,3 +5,4 @@ pub mod utils;
|
|||||||
pub mod routes;
|
pub mod routes;
|
||||||
pub mod rooms;
|
pub mod rooms;
|
||||||
pub mod chat;
|
pub mod chat;
|
||||||
|
pub mod inventory;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ mod utils;
|
|||||||
mod routes;
|
mod routes;
|
||||||
mod rooms;
|
mod rooms;
|
||||||
mod chat;
|
mod chat;
|
||||||
|
mod inventory;
|
||||||
use r2d2::{Pool};
|
use r2d2::{Pool};
|
||||||
use r2d2_sqlite::SqliteConnectionManager;
|
use r2d2_sqlite::SqliteConnectionManager;
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use r2d2::{Pool};
|
|||||||
use crate::rooms::routes::rooms_routes;
|
use crate::rooms::routes::rooms_routes;
|
||||||
use crate::utils::routes::utils_routes;
|
use crate::utils::routes::utils_routes;
|
||||||
use crate::chat::routes::chat_routes;
|
use crate::chat::routes::chat_routes;
|
||||||
|
use crate::inventory::routes::inventory_routes;
|
||||||
|
|
||||||
use crate::utils::db_pool::{AppState};
|
use crate::utils::db_pool::{AppState};
|
||||||
//TODO: add secret fomr dotenv here
|
//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("/auth", utils_routes().with_state(state.clone()))
|
||||||
.nest("/rooms", rooms_routes().with_state(state.clone()))
|
.nest("/rooms", rooms_routes().with_state(state.clone()))
|
||||||
.nest("/chat", chat_routes().with_state(state.clone()))
|
.nest("/chat", chat_routes().with_state(state.clone()))
|
||||||
|
.nest("/inventory", inventory_routes().with_state(state.clone()))
|
||||||
|
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user