add inventory endpoint
This commit is contained in:
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))
|
||||
}
|
||||
Reference in New Issue
Block a user