Refactor ItemName::new to take strings directly

This loosens the dependency on always querying full Items and
ItemClasses.
This commit is contained in:
Simon Bruder 2024-07-21 15:09:57 +02:00
parent f21f8dfa5e
commit 82c7533b4c
Signed by: simon
GPG key ID: 347FF8699CDA0776
5 changed files with 17 additions and 14 deletions

View file

@ -66,7 +66,7 @@ async fn show(
let item_class = item_classes.get(&item.class).unwrap();
let item_name = ItemName::new(&item, item_class);
let item_name = ItemName::new(item.name.as_ref(), &item_class.name);
let mut title = item_name.to_string();
title.push_str(" Item Details");
@ -133,7 +133,7 @@ async fn show(
@if let Some(original_packaging) = original_packaging {
a
href={ "/item/" (original_packaging.id) }
{ (ItemName::new(&original_packaging, &item_classes.get(&original_packaging.class).unwrap())) }
{ (ItemName::new(original_packaging.name.as_ref(), &item_classes.get(&original_packaging.class).unwrap().name)) }
} @else {
"-"
}
@ -160,7 +160,7 @@ async fn show(
ul {
@for child in children {
li {
a href={ "/item/" (child.id) } { (ItemName::new(&child, &item_classes.get(&child.class).unwrap())) }
a href={ "/item/" (child.id) } { (ItemName::new(child.name.as_ref(), &item_classes.get(&child.class).unwrap().name)) }
}
}
}
@ -172,7 +172,7 @@ async fn show(
ul {
@for item in original_packaging_of {
li {
a href={ "/item/" (item.id) } { (ItemName::new(&item, &item_classes.get(&item.class).unwrap())) }
a href={ "/item/" (item.id) } { (ItemName::new(item.name.as_ref(), &item_classes.get(&item.class).unwrap().name)) }
}
}
}
@ -246,7 +246,7 @@ async fn list(pool: web::Data<PgPool>, user: Identity) -> actix_web::Result<impl
tr {
td {
a href={ "/item/" (item.id) } {
(ItemName::new(&item, class).terse())
(ItemName::new(item.name.as_ref(), &class.name).terse())
}
}
td { a href={ "/item-class/" (class.id) } { (class.name) } }
@ -443,7 +443,7 @@ async fn edit_form(
.await
.map_err(error::ErrorInternalServerError)?;
let item_name = ItemName::new(&item, &item_class);
let item_name = ItemName::new(item.name.as_ref(), &item_class.name);
let mut title = item_name.to_string();
title.push_str(" Edit Item");

View file

@ -153,7 +153,7 @@ async fn show(
ul {
@for item in items {
li {
a href={ "/item/" (item.id) } { (ItemName::new(&item, &item_class).terse()) }
a href={ "/item/" (item.id) } { (ItemName::new(item.name.as_ref(), &item_class.name).terse()) }
}
}
}

View file

@ -120,7 +120,7 @@ async fn form(pool: web::Data<PgPool>, user: Identity) -> actix_web::Result<impl
label .form-label for="ids-multiselect" { "Items" }
select #ids-multiselect .form-select multiple size="15" {
@for item in items {
@let item_name = ItemName::new(&item, item_classes.get(&item.class).unwrap());
@let item_name = ItemName::new(item.name.as_ref(), &item_classes.get(&item.class).unwrap().name);
option value={ (item.id) } { (item_name.to_string()) " (" (item.id) ")" }
}
}

View file

@ -55,7 +55,10 @@ pub async fn items(pool: &PgPool) -> Result<Datalist, sqlx::Error> {
.iter()
.map(|i| DatalistOption {
value: i.id.to_string(),
text: Box::new(ItemName::new(i, item_classes.get(&i.class).unwrap())),
text: Box::new(ItemName::new(
i.name.as_ref(),
&item_classes.get(&i.class).unwrap().name,
)),
})
.collect(),
})

View file

@ -93,11 +93,11 @@ pub enum ItemName {
}
impl ItemName {
pub fn new(item: &Item, class: &ItemClass) -> Self {
if let Some(ref name) = item.name {
pub fn new(item_name: Option<&String>, class_name: &String) -> Self {
if let Some(ref name) = item_name {
Self::Item(name.to_string())
} else {
Self::Class(class.name.clone())
Self::Class(class_name.to_string())
}
}
}
@ -236,10 +236,10 @@ pub fn parents_breadcrumb(
};
@for parent in parents {
li .breadcrumb-item {
a href={ "/item/" (parent.id) } { (ItemName::new(parent, parents_item_classes.get(&parent.class).unwrap()) )}
a href={ "/item/" (parent.id) } { (ItemName::new(parent.name.as_ref(), &parents_item_classes.get(&parent.class).unwrap().name)) }
}
}
li .breadcrumb-item.active { (ItemName::new(item, item_class)) }
li .breadcrumb-item.active { (ItemName::new(item.name.as_ref(), &item_class.name)) }
}
}
}