|
|
|
@ -5,12 +5,13 @@ use askama_axum::{IntoResponse, Response};
|
|
|
|
use axum::extract::State;
|
|
|
|
use axum::extract::State;
|
|
|
|
use axum::{async_trait, debug_handler, Form};
|
|
|
|
use axum::{async_trait, debug_handler, Form};
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
use tracing::info;
|
|
|
|
use crate::app::routes::AppState;
|
|
|
|
use crate::app::routes::AppState;
|
|
|
|
use crate::db::display_unit::DbDisplayUnit;
|
|
|
|
use crate::db::display_unit::DbDisplayUnit;
|
|
|
|
use crate::db;
|
|
|
|
use crate::db;
|
|
|
|
use crate::util::extract::form_helpers::form_checkbox_is_checked;
|
|
|
|
|
|
|
|
use crate::util::extract::htmx_form_data::{HtmxFormData, HtmxFormDataError};
|
|
|
|
use crate::util::extract::htmx_form_data::{HtmxFormData, HtmxFormDataError};
|
|
|
|
use crate::util::extract::validated_form::ValidatedForm;
|
|
|
|
use crate::util::extract::validated_form::ValidatedForm;
|
|
|
|
|
|
|
|
use crate::util::extract::form_helpers::deserialize_form_checkbox;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Template, Debug)]
|
|
|
|
#[derive(Template, Debug)]
|
|
|
|
#[template(path = "item/item-create-form.html")]
|
|
|
|
#[template(path = "item/item-create-form.html")]
|
|
|
|
@ -45,9 +46,10 @@ pub struct CreateItemFormData {
|
|
|
|
name: String,
|
|
|
|
name: String,
|
|
|
|
display_unit: String,
|
|
|
|
display_unit: String,
|
|
|
|
reorder_point: f64,
|
|
|
|
reorder_point: f64,
|
|
|
|
allow_fractional_units: Option<String>,
|
|
|
|
|
|
|
|
pims_id: Option<String>,
|
|
|
|
pims_id: Option<String>,
|
|
|
|
vetcove_id: Option<String>,
|
|
|
|
vetcove_id: Option<String>,
|
|
|
|
|
|
|
|
#[serde(default, deserialize_with = "deserialize_form_checkbox")]
|
|
|
|
|
|
|
|
allow_fractional_units: bool,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
#[async_trait]
|
|
|
|
@ -58,8 +60,6 @@ impl HtmxFormData for CreateItemFormData {
|
|
|
|
let mut base = Self::base_template(&state).await?;
|
|
|
|
let mut base = Self::base_template(&state).await?;
|
|
|
|
let display_units = &base.display_units;
|
|
|
|
let display_units = &base.display_units;
|
|
|
|
|
|
|
|
|
|
|
|
let allow_fractional_units = form_checkbox_is_checked(&self.allow_fractional_units);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let name_error = if self.name.is_empty() {
|
|
|
|
let name_error = if self.name.is_empty() {
|
|
|
|
"Please provide a name"
|
|
|
|
"Please provide a name"
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
@ -79,7 +79,7 @@ impl HtmxFormData for CreateItemFormData {
|
|
|
|
|| self.reorder_point.is_sign_negative()
|
|
|
|
|| self.reorder_point.is_sign_negative()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
"Provide a positive number"
|
|
|
|
"Provide a positive number"
|
|
|
|
} else if !(allow_fractional_units || self.reorder_point.fract() == 0.0) {
|
|
|
|
} else if !(self.allow_fractional_units || self.reorder_point.fract() == 0.0) {
|
|
|
|
"Fractional units not allowed"
|
|
|
|
"Fractional units not allowed"
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
""
|
|
|
|
@ -126,7 +126,7 @@ impl HtmxFormData for CreateItemFormData {
|
|
|
|
base.pims_id_error = pims_id_error;
|
|
|
|
base.pims_id_error = pims_id_error;
|
|
|
|
base.vetcove_id_value = self.vetcove_id.as_deref().unwrap_or_default().to_string();
|
|
|
|
base.vetcove_id_value = self.vetcove_id.as_deref().unwrap_or_default().to_string();
|
|
|
|
base.vetcove_id_error = vetcove_id_error;
|
|
|
|
base.vetcove_id_error = vetcove_id_error;
|
|
|
|
base.allow_fractional_units_value = allow_fractional_units;
|
|
|
|
base.allow_fractional_units_value = self.allow_fractional_units;
|
|
|
|
|
|
|
|
|
|
|
|
return Err(HtmxFormDataError::ValidationError(base));
|
|
|
|
return Err(HtmxFormDataError::ValidationError(base));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -163,10 +163,8 @@ pub async fn create_item_form_post(
|
|
|
|
form_data: ValidatedForm<CreateItemFormData>,
|
|
|
|
form_data: ValidatedForm<CreateItemFormData>,
|
|
|
|
) -> Result<Response, AppError> {
|
|
|
|
) -> Result<Response, AppError> {
|
|
|
|
|
|
|
|
|
|
|
|
let allow_fractional_units = form_checkbox_is_checked(&form_data.allow_fractional_units);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let _new_id = db::inventory_item::add_inventory_item(&state.db, &form_data.name, form_data.reorder_point,
|
|
|
|
let _new_id = db::inventory_item::add_inventory_item(&state.db, &form_data.name, form_data.reorder_point,
|
|
|
|
allow_fractional_units, &form_data.display_unit,
|
|
|
|
form_data.allow_fractional_units, &form_data.display_unit,
|
|
|
|
&form_data.pims_id, &form_data.vetcove_id,
|
|
|
|
&form_data.pims_id, &form_data.vetcove_id,
|
|
|
|
).await?;
|
|
|
|
).await?;
|
|
|
|
|
|
|
|
|
|
|
|
|