work
This commit is contained in:
parent
9da4ef072c
commit
ba61580adb
7 changed files with 177 additions and 179 deletions
|
@ -1,70 +0,0 @@
|
|||
use actix_web::{get, post, HttpRequest, Responder};
|
||||
use maud::html;
|
||||
|
||||
use crate::item;
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
struct AddinventoryForm {
|
||||
variant: String,
|
||||
origin: String,
|
||||
price: f64,
|
||||
}
|
||||
|
||||
#[post("/item/{item_id}/inventory/add")]
|
||||
pub async fn add_item_inventory_page_post(
|
||||
r: HttpRequest,
|
||||
f: actix_web::web::Form<AddinventoryForm>,
|
||||
) -> impl Responder {
|
||||
let id = r.match_info().query("item_id");
|
||||
let itemdb: &actix_web::web::Data<item::ItemDB> = r.app_data().unwrap();
|
||||
let item = itemdb.get_item(id).unwrap();
|
||||
let rec_id = item.add_inventory(&f.variant, &f.origin, f.price).await;
|
||||
|
||||
// todo : generate receipts
|
||||
|
||||
web_base::redirect(&format!("/receipt/{}", rec_id))
|
||||
}
|
||||
|
||||
#[get("/item/{item_id}/inventory/add")]
|
||||
pub async fn add_item_inventory_page(r: HttpRequest) -> impl Responder {
|
||||
let id = r.match_info().query("item_id");
|
||||
|
||||
let itemdb: &actix_web::web::Data<item::ItemDB> = r.app_data().unwrap();
|
||||
|
||||
let item = itemdb.get_item(id).unwrap();
|
||||
let item_variants = item.get_variants().await;
|
||||
|
||||
// todo : get previous origins
|
||||
let item_origins = item.get_origins().await;
|
||||
|
||||
let content = html!(
|
||||
div class="bg-white p-8 rounded shadow-md max-w-md grid items-center justify-center m-auto mt-5" {
|
||||
h1 class="text-2xl font-bold mb-4" { "Add Item inventory" };
|
||||
form action=(format!("/item/{}/inventory/add", id)) method="post" {
|
||||
div {
|
||||
label class="text-sm font-medium text-gray-600 mr-3" for="variant" { "Variant:" };
|
||||
select class="mt-1 p-2 border border-gray-300 rounded focus:outline-none focus:border-blue-500" id="variant" name="variant" {
|
||||
@for variant in item_variants {
|
||||
option value=(variant) { (variant) };
|
||||
};
|
||||
};
|
||||
};
|
||||
br;
|
||||
label class="text-sm font-medium text-gray-600" for="origin" { "Origin:" };
|
||||
input class="mt-1 p-2 border border-gray-300 rounded focus:outline-none focus:border-blue-500" type="text" name="origin" list="origin_options" placeholder="Origin";
|
||||
datalist id="origin_options" {
|
||||
@for origin in item_origins {
|
||||
option value=(origin);
|
||||
}
|
||||
};
|
||||
br;
|
||||
label class="text-sm font-medium text-gray-600" for="price" { "Price: "};
|
||||
input class="mt-1 p-2 border border-gray-300 rounded focus:outline-none focus:border-blue-500" type="number" step="0.01" name="price" placeholder="0.00";
|
||||
br;
|
||||
input class="hover:cursor-pointer bg-blue-500 text-white rounded px-4 py-2 mt-5" type="submit" name="submit" value="Add";
|
||||
};
|
||||
};
|
||||
).into_string();
|
||||
|
||||
web_base::build_site(&r, "Add Item inventory", &content)
|
||||
}
|
|
@ -1,50 +1,60 @@
|
|||
use actix_web::{get, HttpRequest, Responder};
|
||||
use actix_web::post;
|
||||
use actix_web::{get, HttpRequest, HttpResponse, Responder};
|
||||
use maud::html;
|
||||
pub mod inventory_page;
|
||||
pub mod variant_pages;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::item;
|
||||
|
||||
#[get("/item/{item_id}")]
|
||||
pub async fn item_page(r: HttpRequest) -> impl Responder {
|
||||
let id = r.match_info().query("item_id");
|
||||
println!("{}", id);
|
||||
|
||||
let itemdb: &actix_web::web::Data<item::ItemDB> = r.app_data().unwrap();
|
||||
|
||||
let item = itemdb.get_item(id).unwrap();
|
||||
|
||||
let content =
|
||||
html!(
|
||||
div class="bg-gray-100 min-h-screen min-w-screen items-center justify-center py-5" {
|
||||
div class="w-screen px-5" {
|
||||
div {
|
||||
p class="mb-2 font-bold text-2xl" { (format!("Item : {}", item.item.name)) };
|
||||
p class="font-bold" { (format!("Category: {}", item.item.category))};
|
||||
};
|
||||
div class="mt-5" {
|
||||
// (button("Add Variant", &format!("/item/{}/variant/add", item.item.name)))
|
||||
// (button("Add inventory", &format!("/item/{}/inventory/add", item.item.name)));
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
div class="mt-5 px-5" {
|
||||
h1 class="text-2xl font-bold mb-4" { "Variants" };
|
||||
@for variant in item.get_variants().await {
|
||||
p class="mb-2" { (variant) };
|
||||
}
|
||||
@for q in item.get_inventory_entries().await {
|
||||
div class="p-2" {
|
||||
p { (format!("Variant {}", q.variant_name))};
|
||||
p { (format!("Origin {}", q.origin))};
|
||||
p { (format!("Price {}", q.price))};
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
)
|
||||
.into_string();
|
||||
|
||||
web_base::build_site(&r, "Item", &content)
|
||||
macro_rules! get_itemdb {
|
||||
($req:expr) => {{
|
||||
let itemdb: &actix_web::web::Data<item::ItemDB> = $req.app_data().unwrap();
|
||||
itemdb
|
||||
}};
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct SupplyForm {
|
||||
item: String,
|
||||
variant: String,
|
||||
amount: Option<usize>,
|
||||
price: String,
|
||||
origin: String,
|
||||
}
|
||||
|
||||
#[post("/supply")]
|
||||
pub async fn supply_route(
|
||||
req: HttpRequest,
|
||||
form: actix_web::web::Form<SupplyForm>,
|
||||
) -> impl Responder {
|
||||
let itemdb = get_itemdb!(req);
|
||||
println!("{form:?}");
|
||||
let variant = itemdb
|
||||
.get_item(&form.item)
|
||||
.unwrap()
|
||||
.variant(&form.variant)
|
||||
.unwrap();
|
||||
let transaction_id = variant
|
||||
.supply(
|
||||
form.amount.unwrap_or(1),
|
||||
form.price.clone().into(),
|
||||
&form.origin,
|
||||
)
|
||||
.await;
|
||||
actix_web::HttpResponse::Ok().json(serde_json::json!({"uuid": transaction_id}))
|
||||
}
|
||||
|
||||
#[get("/item/{item_id}/variants")]
|
||||
pub async fn item_variants_page(r: HttpRequest) -> impl Responder {
|
||||
let id = r.match_info().query("item_id");
|
||||
|
||||
let itemdb = get_itemdb!(r);
|
||||
|
||||
let item = itemdb.get_item(id);
|
||||
|
||||
let variants = item.unwrap().get_variants().await;
|
||||
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"item": id,
|
||||
"variants": variants
|
||||
}))
|
||||
}
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
use actix_web::{get, post, HttpRequest, Responder};
|
||||
use maud::html;
|
||||
|
||||
use crate::item;
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
struct AddVariantForm {
|
||||
variant_name: String,
|
||||
}
|
||||
|
||||
#[post("/item/{item_id}/variant/add")]
|
||||
pub async fn add_item_variant_page_post(
|
||||
r: HttpRequest,
|
||||
f: actix_web::web::Form<AddVariantForm>,
|
||||
) -> impl Responder {
|
||||
let id = r.match_info().query("item_id");
|
||||
let itemdb: &actix_web::web::Data<item::ItemDB> = r.app_data().unwrap();
|
||||
let item = itemdb.get_item(id).unwrap();
|
||||
item.add_variant(&f.variant_name).await;
|
||||
|
||||
web_base::redirect(&format!("/item/{}", id))
|
||||
}
|
||||
|
||||
#[get("/item/{item_id}/variant/add")]
|
||||
pub async fn add_item_variant_page(r: HttpRequest) -> impl Responder {
|
||||
let id = r.match_info().query("item_id");
|
||||
|
||||
let itemdb: &actix_web::web::Data<item::ItemDB> = r.app_data().unwrap();
|
||||
|
||||
let item = itemdb.get_item(id).unwrap();
|
||||
|
||||
let content = html!(
|
||||
div class="bg-white p-8 rounded shadow-md max-w-md grid items-center justify-center m-auto mt-5" {
|
||||
h1 class="text-2xl font-bold mb-4" { "Add Variant for " (item.item.name) };
|
||||
form action=(format!("/item/{}/variant/add", id)) method="post" {
|
||||
input class="mt-1 p-2 border border-gray-300 rounded focus:outline-none focus:border-blue-500" type="text" name="variant_name" placeholder="Name" { };
|
||||
input class="ml-5 hover:cursor-pointer bg-blue-500 text-white rounded px-4 py-2 mt-5" type="submit" name="submit" value="Add";
|
||||
};
|
||||
};
|
||||
).into_string();
|
||||
|
||||
web_base::build_site(&r, "Add Item Variant", &content)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue