This commit is contained in:
2023-08-29 21:33:59 +02:00
parent e0c9bc542a
commit 201fb88aef
6 changed files with 107 additions and 6 deletions

View File

@@ -323,9 +323,11 @@ impl InventoryNewItemFormName {
."justify-items-center"
."items-center"
hx-post="/inventory/item/name/validate"
hx-trigger="input delay:1s, every 5s"
hx-trigger="input delay:1s, loaded from:document"
hx-params="new-item-name"
hx-swap="outerHTML"
#abc
{
label for="name" .font-bold { "Name" }
input

View File

@@ -1,4 +1,4 @@
use maud::{html, Markup, DOCTYPE};
use maud::{html, Markup, PreEscaped, DOCTYPE};
pub mod home;
pub mod inventory;
@@ -29,7 +29,7 @@ impl Root {
script src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.js" defer {}
link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@mdi/font@6.9.96/css/materialdesignicons.min.css";
link rel="shortcut icon" type="image/svg+xml" href="/favicon.svg";
script { (include_str!(concat!(env!("CARGO_MANIFEST_DIR"),"/js/app.js"))) }
script { (PreEscaped(include_str!(concat!(env!("CARGO_MANIFEST_DIR"),"/js/app.js")))) }
}
body
hx-boost="true"

View File

@@ -394,7 +394,38 @@ async fn inventory_item_delete(
)
.execute(&state.database_pool)
.await
.map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))?;
.map_err(|error| match error {
sqlx::Error::Database(ref error) => {
let sqlite_error = error.downcast_ref::<SqliteError>();
if let Some(code) = sqlite_error.code() {
match &*code {
"787" => {
// SQLITE_CONSTRAINT_FOREIGNKEY
(
StatusCode::BAD_REQUEST,
// TODO: this is not perfect, as both foreign keys
// may be responsible for the error. how can we tell
// which one?
format!("item {} cannot be deleted because it's on use in trips. instead, archive it", code.to_string()),
)
}
_ => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("got error with unknown code: {}", sqlite_error.to_string()),
),
}
} else {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("got error without code: {}", sqlite_error.to_string()),
)
}
}
_ => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("got unknown error: {}", error.to_string()),
),
})?;
if results.rows_affected() == 0 {
Err((