Show children in item class details

This commit is contained in:
Simon Bruder 2024-07-11 13:50:59 +02:00
parent 6251dea6a1
commit b0f542077d
Signed by: simon
GPG key ID: 347FF8699CDA0776
2 changed files with 31 additions and 2 deletions

View file

@ -27,19 +27,25 @@ async fn show_item_class(
) -> actix_web::Result<impl Responder> {
let id = path.into_inner();
let item_class = manage::item_class::get(&mut pool.clone().get().await.unwrap(), id)
let mut conn = pool.clone().get().await.unwrap();
let item_class = manage::item_class::get(&mut conn, id)
.await
.map_err(error::ErrorInternalServerError)?;
// TODO: Once async closures are stable, use map_or on item_class.parent instead
let parent = match item_class.parent {
Some(id) => manage::item_class::get(&mut pool.get().await.unwrap(), id)
Some(id) => manage::item_class::get(&mut conn, id)
.await
.map(Some)
.map_err(error::ErrorInternalServerError)?,
None => None,
};
let children = manage::item_class::children(&mut conn, id)
.await
.map_err(error::ErrorInternalServerError)?;
let mut title = item_class.name.clone();
title.push_str(" Item Details");
@ -73,6 +79,18 @@ async fn show_item_class(
}
}
}
@if !children.is_empty() {
h3 .mt-4 { "Children" }
ul {
@for child in children {
li {
a href={ "/item-class/" (child.id) } { (child.name) }
}
}
}
}
},
))
}

View file

@ -83,3 +83,14 @@ pub async fn delete(conn: &mut AsyncPgConnection, id: Uuid) -> Result<(), diesel
assert_eq!(num_deleted, 1);
Ok(())
}
pub async fn children(
conn: &mut AsyncPgConnection,
id: Uuid,
) -> Result<Vec<ItemClass>, diesel::result::Error> {
schema::item_classes::table
.filter(schema::item_classes::parent.eq(id))
.select(ItemClass::as_select())
.load(conn)
.await
}