From b0f542077de9e00b74c209e3719619716f4351e0 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Thu, 11 Jul 2024 13:50:59 +0200 Subject: [PATCH] Show children in item class details --- src/frontend/item_class.rs | 22 ++++++++++++++++++++-- src/manage/item_class.rs | 11 +++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/frontend/item_class.rs b/src/frontend/item_class.rs index 5a9c3bf..7760428 100644 --- a/src/frontend/item_class.rs +++ b/src/frontend/item_class.rs @@ -27,19 +27,25 @@ async fn show_item_class( ) -> actix_web::Result { 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) } + } + } + } + } }, )) } diff --git a/src/manage/item_class.rs b/src/manage/item_class.rs index 0fa3c7b..f4552d9 100644 --- a/src/manage/item_class.rs +++ b/src/manage/item_class.rs @@ -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, diesel::result::Error> { + schema::item_classes::table + .filter(schema::item_classes::parent.eq(id)) + .select(ItemClass::as_select()) + .load(conn) + .await +}