Show item class parents with breadcrumbs

This commit is contained in:
Simon Bruder 2024-08-06 16:57:26 +02:00
parent 61f356269c
commit 6007e4b96a
Signed by: simon
GPG key ID: 347FF8699CDA0776
3 changed files with 60 additions and 6 deletions

View file

@ -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"
}

View file

@ -9,7 +9,7 @@ mod edit;
mod list; mod list;
mod show; mod show;
use sqlx::PgPool; use sqlx::{query, PgPool};
use uuid::Uuid; use uuid::Uuid;
pub use add::{ItemClassAddForm, ItemClassAddFormPrefilled}; pub use add::{ItemClassAddForm, ItemClassAddFormPrefilled};
@ -37,3 +37,20 @@ impl ItemClassPreview {
Self { id, name } Self { id, name }
} }
} }
impl ItemClassRepository {
pub async fn parents(&self, id: Uuid) -> sqlx::Result<Vec<ItemClassPreview>> {
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
}
}

View file

@ -4,7 +4,7 @@
use actix_identity::Identity; use actix_identity::Identity;
use actix_web::{error, get, web, Responder}; use actix_web::{error, get, web, Responder};
use maud::html; use maud::{html, Render};
use uuid::Uuid; use uuid::Uuid;
use crate::database::ItemClassRepository; use crate::database::ItemClassRepository;
@ -31,6 +31,11 @@ async fn get(
.await .await
.map_err(error::ErrorInternalServerError)?; .map_err(error::ErrorInternalServerError)?;
let parents = item_class_repo
.parents(id)
.await
.map_err(error::ErrorInternalServerError)?;
let children = item_class_repo let children = item_class_repo
.children(id) .children(id)
.await .await
@ -96,10 +101,14 @@ async fn get(
th { "Name" } th { "Name" }
td { (item_class.name) } td { (item_class.name) }
} }
@if let Some(parent) = item_class.parent {
tr { tr {
th { "Parent" } th { "Parents" }
td { (parent) } td {
(templates::helpers::parents_breadcrumb(
&item_class.name,
parents.iter().map(|parent| parent as &dyn Render).collect(),
true
))
} }
} }
tr { tr {