From a22953558bdeca9a60338f0283191a8a2e114fe1 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Tue, 6 Aug 2024 16:57:26 +0200 Subject: [PATCH] Show item class parents with breadcrumbs --- ...eece6556236c25253a45c8ddc834751f72694.json | 28 +++++++++++++++++++ src/database/item_classes/mod.rs | 19 ++++++++++++- src/frontend/item_class/show.rs | 19 +++++++++---- 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 .sqlx/query-85e1b1bae08c7edda9671214e6eeece6556236c25253a45c8ddc834751f72694.json diff --git a/.sqlx/query-85e1b1bae08c7edda9671214e6eeece6556236c25253a45c8ddc834751f72694.json b/.sqlx/query-85e1b1bae08c7edda9671214e6eeece6556236c25253a45c8ddc834751f72694.json new file mode 100644 index 0000000..39363aa --- /dev/null +++ b/.sqlx/query-85e1b1bae08c7edda9671214e6eeece6556236c25253a45c8ddc834751f72694.json @@ -0,0 +1,28 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT item_classes.id, item_classes.name\n FROM item_classes\n JOIN unnest((SELECT parents FROM item_class_tree WHERE id = $1))\n WITH ORDINALITY AS parents(id, n)\n ON item_classes.id = parents.id\n ORDER BY parents.n", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Uuid" + }, + { + "ordinal": 1, + "name": "name", + "type_info": "Varchar" + } + ], + "parameters": { + "Left": [ + "Uuid" + ] + }, + "nullable": [ + false, + false + ] + }, + "hash": "85e1b1bae08c7edda9671214e6eeece6556236c25253a45c8ddc834751f72694" +} diff --git a/src/database/item_classes/mod.rs b/src/database/item_classes/mod.rs index c374b19..aa66977 100644 --- a/src/database/item_classes/mod.rs +++ b/src/database/item_classes/mod.rs @@ -9,7 +9,7 @@ mod edit; mod list; mod show; -use sqlx::PgPool; +use sqlx::{query, PgPool}; use uuid::Uuid; pub use add::{ItemClassAddForm, ItemClassAddFormPrefilled}; @@ -37,3 +37,20 @@ impl ItemClassPreview { Self { id, name } } } + +impl ItemClassRepository { + pub async fn parents(&self, id: Uuid) -> sqlx::Result> { + query!( + r#"SELECT item_classes.id, item_classes.name + FROM item_classes + JOIN unnest((SELECT parents FROM item_class_tree WHERE id = $1)) + WITH ORDINALITY AS parents(id, n) + ON item_classes.id = parents.id + ORDER BY parents.n"#, + id + ) + .map(|row| ItemClassPreview::new(row.id, row.name)) + .fetch_all(&self.pool) + .await + } +} diff --git a/src/frontend/item_class/show.rs b/src/frontend/item_class/show.rs index 4615158..7cce11d 100644 --- a/src/frontend/item_class/show.rs +++ b/src/frontend/item_class/show.rs @@ -4,7 +4,7 @@ use actix_identity::Identity; use actix_web::{error, get, web, Responder}; -use maud::html; +use maud::{html, Render}; use uuid::Uuid; use crate::database::ItemClassRepository; @@ -31,6 +31,11 @@ async fn get( .await .map_err(error::ErrorInternalServerError)?; + let parents = item_class_repo + .parents(id) + .await + .map_err(error::ErrorInternalServerError)?; + let children = item_class_repo .children(id) .await @@ -96,10 +101,14 @@ async fn get( th { "Name" } td { (item_class.name) } } - @if let Some(parent) = item_class.parent { - tr { - th { "Parent" } - td { (parent) } + tr { + th { "Parents" } + td { + (templates::helpers::parents_breadcrumb( + &item_class.name, + parents.iter().map(|parent| parent as &dyn Render).collect(), + true + )) } } tr {