From 5a802018504524f5c4dcd85460c3c3fec1314dd5 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sat, 13 Jul 2024 16:16:30 +0200 Subject: [PATCH] Add original packaging attribute to items --- ...c7ee7520478ff4f5886b4523fb0dc4df0e204.json | 8 ++- ...f28fd05f18706f70220b6ebb7354d2a0a9e3b.json | 8 ++- ...9b33ea9d0775ef1e2b37a1d7fd0109f98b27.json} | 14 ++-- ...aa92283151d67f52fbb22e5d976b1c6a5c367.json | 8 ++- ...598749d931af69a80d24f4575a4bc2c740f3b.json | 8 ++- ...86f1f9fdb076e8f8b98f10fc1b981cfe8038c.json | 8 ++- ...fbc5433e972b5522b367098afe6cb90a30bf2.json | 8 ++- ...57d6852d2d3f63dc055442125a6a92950729d.json | 8 ++- ...2b19308d9a558703b02e758f60e7d8644d00.json} | 14 ++-- ...082b29931e992a08534b315a2935978e827e8.json | 62 ++++++++++++++++++ ...34634_item_add_original_packaging.down.sql | 6 ++ ...3134634_item_add_original_packaging.up.sql | 6 ++ src/frontend/item.rs | 65 +++++++++++++++++-- src/manage/item.rs | 23 +++++-- src/models.rs | 3 + 15 files changed, 224 insertions(+), 25 deletions(-) rename .sqlx/{query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json => query-30a7f461d0a20179d08436d9d2c69b33ea9d0775ef1e2b37a1d7fd0109f98b27.json} (72%) rename .sqlx/{query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json => query-efe2258db42b60f732d562d106842b19308d9a558703b02e758f60e7d8644d00.json} (74%) create mode 100644 .sqlx/query-fa9770ac99749cc4255da6172e5082b29931e992a08534b315a2935978e827e8.json create mode 100644 migrations/20240713134634_item_add_original_packaging.down.sql create mode 100644 migrations/20240713134634_item_add_original_packaging.up.sql diff --git a/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json b/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json index 5dfb444..a2397f4 100644 --- a/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json +++ b/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json @@ -32,6 +32,11 @@ "ordinal": 5, "name": "short_id", "type_info": "Int4" + }, + { + "ordinal": 6, + "name": "original_packaging", + "type_info": "Uuid" } ], "parameters": { @@ -45,7 +50,8 @@ true, false, false, - false + false, + true ] }, "hash": "1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204" diff --git a/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json b/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json index 1e9b115..61cb75e 100644 --- a/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json +++ b/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json @@ -32,6 +32,11 @@ "ordinal": 5, "name": "short_id", "type_info": "Int4" + }, + { + "ordinal": 6, + "name": "original_packaging", + "type_info": "Uuid" } ], "parameters": { @@ -45,7 +50,8 @@ true, false, false, - false + false, + true ] }, "hash": "2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b" diff --git a/.sqlx/query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json b/.sqlx/query-30a7f461d0a20179d08436d9d2c69b33ea9d0775ef1e2b37a1d7fd0109f98b27.json similarity index 72% rename from .sqlx/query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json rename to .sqlx/query-30a7f461d0a20179d08436d9d2c69b33ea9d0775ef1e2b37a1d7fd0109f98b27.json index f8d747f..a2b877b 100644 --- a/.sqlx/query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json +++ b/.sqlx/query-30a7f461d0a20179d08436d9d2c69b33ea9d0775ef1e2b37a1d7fd0109f98b27.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "UPDATE items SET name = $2, parent = $3, class = $4 WHERE id = $1 RETURNING *", + "query": "INSERT INTO items (name, parent, class, original_packaging) VALUES ($1, $2, $3, $4) RETURNING *", "describe": { "columns": [ { @@ -32,13 +32,18 @@ "ordinal": 5, "name": "short_id", "type_info": "Int4" + }, + { + "ordinal": 6, + "name": "original_packaging", + "type_info": "Uuid" } ], "parameters": { "Left": [ - "Uuid", "Varchar", "Uuid", + "Uuid", "Uuid" ] }, @@ -48,8 +53,9 @@ true, false, false, - false + false, + true ] }, - "hash": "85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e" + "hash": "30a7f461d0a20179d08436d9d2c69b33ea9d0775ef1e2b37a1d7fd0109f98b27" } diff --git a/.sqlx/query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json b/.sqlx/query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json index bb26826..ccb058b 100644 --- a/.sqlx/query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json +++ b/.sqlx/query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json @@ -32,6 +32,11 @@ "ordinal": 5, "name": "short_id", "type_info": "Int4" + }, + { + "ordinal": 6, + "name": "original_packaging", + "type_info": "Uuid" } ], "parameters": { @@ -43,7 +48,8 @@ true, false, false, - false + false, + true ] }, "hash": "58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367" diff --git a/.sqlx/query-79d8bfe2ed76ee550cdc31f282f598749d931af69a80d24f4575a4bc2c740f3b.json b/.sqlx/query-79d8bfe2ed76ee550cdc31f282f598749d931af69a80d24f4575a4bc2c740f3b.json index fab3573..f085fc2 100644 --- a/.sqlx/query-79d8bfe2ed76ee550cdc31f282f598749d931af69a80d24f4575a4bc2c740f3b.json +++ b/.sqlx/query-79d8bfe2ed76ee550cdc31f282f598749d931af69a80d24f4575a4bc2c740f3b.json @@ -32,6 +32,11 @@ "ordinal": 5, "name": "short_id", "type_info": "Int4" + }, + { + "ordinal": 6, + "name": "original_packaging", + "type_info": "Uuid" } ], "parameters": { @@ -45,7 +50,8 @@ true, false, false, - false + false, + true ] }, "hash": "79d8bfe2ed76ee550cdc31f282f598749d931af69a80d24f4575a4bc2c740f3b" diff --git a/.sqlx/query-7eeb752c8b00ac4000104f0254186f1f9fdb076e8f8b98f10fc1b981cfe8038c.json b/.sqlx/query-7eeb752c8b00ac4000104f0254186f1f9fdb076e8f8b98f10fc1b981cfe8038c.json index 9465980..403169c 100644 --- a/.sqlx/query-7eeb752c8b00ac4000104f0254186f1f9fdb076e8f8b98f10fc1b981cfe8038c.json +++ b/.sqlx/query-7eeb752c8b00ac4000104f0254186f1f9fdb076e8f8b98f10fc1b981cfe8038c.json @@ -32,6 +32,11 @@ "ordinal": 5, "name": "short_id", "type_info": "Int4" + }, + { + "ordinal": 6, + "name": "original_packaging", + "type_info": "Uuid" } ], "parameters": { @@ -45,7 +50,8 @@ true, false, false, - false + false, + true ] }, "hash": "7eeb752c8b00ac4000104f0254186f1f9fdb076e8f8b98f10fc1b981cfe8038c" diff --git a/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json b/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json index 950b148..336949f 100644 --- a/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json +++ b/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json @@ -32,6 +32,11 @@ "ordinal": 5, "name": "short_id", "type_info": "Int4" + }, + { + "ordinal": 6, + "name": "original_packaging", + "type_info": "Uuid" } ], "parameters": { @@ -45,7 +50,8 @@ true, false, false, - false + false, + true ] }, "hash": "94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2" diff --git a/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json b/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json index 7ecd529..59f6624 100644 --- a/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json +++ b/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json @@ -32,6 +32,11 @@ "ordinal": 5, "name": "short_id", "type_info": "Int4" + }, + { + "ordinal": 6, + "name": "original_packaging", + "type_info": "Uuid" } ], "parameters": { @@ -45,7 +50,8 @@ true, false, false, - false + false, + true ] }, "hash": "e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d" diff --git a/.sqlx/query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json b/.sqlx/query-efe2258db42b60f732d562d106842b19308d9a558703b02e758f60e7d8644d00.json similarity index 74% rename from .sqlx/query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json rename to .sqlx/query-efe2258db42b60f732d562d106842b19308d9a558703b02e758f60e7d8644d00.json index 8cc93c7..658f4f6 100644 --- a/.sqlx/query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json +++ b/.sqlx/query-efe2258db42b60f732d562d106842b19308d9a558703b02e758f60e7d8644d00.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "INSERT INTO items (name, parent, class) VALUES ($1, $2, $3) RETURNING *", + "query": "SELECT * FROM items WHERE original_packaging = $1", "describe": { "columns": [ { @@ -32,12 +32,15 @@ "ordinal": 5, "name": "short_id", "type_info": "Int4" + }, + { + "ordinal": 6, + "name": "original_packaging", + "type_info": "Uuid" } ], "parameters": { "Left": [ - "Varchar", - "Uuid", "Uuid" ] }, @@ -47,8 +50,9 @@ true, false, false, - false + false, + true ] }, - "hash": "05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec" + "hash": "efe2258db42b60f732d562d106842b19308d9a558703b02e758f60e7d8644d00" } diff --git a/.sqlx/query-fa9770ac99749cc4255da6172e5082b29931e992a08534b315a2935978e827e8.json b/.sqlx/query-fa9770ac99749cc4255da6172e5082b29931e992a08534b315a2935978e827e8.json new file mode 100644 index 0000000..7a9a202 --- /dev/null +++ b/.sqlx/query-fa9770ac99749cc4255da6172e5082b29931e992a08534b315a2935978e827e8.json @@ -0,0 +1,62 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE items SET name = $2, parent = $3, class = $4, original_packaging = $5 WHERE id = $1 RETURNING *", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Uuid" + }, + { + "ordinal": 1, + "name": "name", + "type_info": "Varchar" + }, + { + "ordinal": 2, + "name": "parent", + "type_info": "Uuid" + }, + { + "ordinal": 3, + "name": "class", + "type_info": "Uuid" + }, + { + "ordinal": 4, + "name": "created_at", + "type_info": "Timestamptz" + }, + { + "ordinal": 5, + "name": "short_id", + "type_info": "Int4" + }, + { + "ordinal": 6, + "name": "original_packaging", + "type_info": "Uuid" + } + ], + "parameters": { + "Left": [ + "Uuid", + "Varchar", + "Uuid", + "Uuid", + "Uuid" + ] + }, + "nullable": [ + false, + true, + true, + false, + false, + false, + true + ] + }, + "hash": "fa9770ac99749cc4255da6172e5082b29931e992a08534b315a2935978e827e8" +} diff --git a/migrations/20240713134634_item_add_original_packaging.down.sql b/migrations/20240713134634_item_add_original_packaging.down.sql new file mode 100644 index 0000000..d7d1003 --- /dev/null +++ b/migrations/20240713134634_item_add_original_packaging.down.sql @@ -0,0 +1,6 @@ +-- SPDX-FileCopyrightText: 2024 Simon Bruder +-- +-- SPDX-License-Identifier: AGPL-3.0-or-later + +ALTER TABLE items + DROP original_packaging; diff --git a/migrations/20240713134634_item_add_original_packaging.up.sql b/migrations/20240713134634_item_add_original_packaging.up.sql new file mode 100644 index 0000000..8b29ad5 --- /dev/null +++ b/migrations/20240713134634_item_add_original_packaging.up.sql @@ -0,0 +1,6 @@ +-- SPDX-FileCopyrightText: 2024 Simon Bruder +-- +-- SPDX-License-Identifier: AGPL-3.0-or-later + +ALTER TABLE items + ADD original_packaging UUID REFERENCES items(id) CHECK (original_packaging <> id); diff --git a/src/frontend/item.rs b/src/frontend/item.rs index a0ac9c0..075f5ec 100644 --- a/src/frontend/item.rs +++ b/src/frontend/item.rs @@ -10,7 +10,7 @@ use maud::html; use sqlx::PgPool; use uuid::Uuid; -use super::templates::helpers::{Colour, PageAction, PageActionMethod}; +use super::templates::helpers::{Colour, ItemName, PageAction, PageActionMethod}; use super::templates::{self, datalist, forms, TemplateConfig}; use crate::manage; use crate::models::*; @@ -49,9 +49,22 @@ async fn show( .await .map_err(error::ErrorInternalServerError)?; + let original_packaging = match item.original_packaging { + Some(id) => Some( + manage::item::get(&pool, id) + .await + .map_err(error::ErrorInternalServerError)?, + ), + None => None, + }; + + let original_packaging_of = manage::item::original_packaging_contents(&pool, id) + .await + .map_err(error::ErrorInternalServerError)?; + let item_class = item_classes.get(&item.class).unwrap(); - let item_name = templates::helpers::ItemName::new(&item, item_class); + let item_name = ItemName::new(&item, item_class); let mut title = item_name.to_string(); title.push_str(" – Item Details"); @@ -105,6 +118,18 @@ async fn show( th { "Parents" } td { (templates::helpers::parents_breadcrumb(&item, &item_class, &parents, &item_classes, true)) } } + tr { + th { "Original Packaging" } + td { + @if let Some(original_packaging) = original_packaging { + a + href={ "/item/" (original_packaging.id) } + { (ItemName::new(&original_packaging, &item_classes.get(&original_packaging.class).unwrap())) } + } @else { + "-" + } + } + } } @if !children.is_empty() { @@ -113,7 +138,19 @@ async fn show( ul { @for child in children { li { - a href={ "/item/" (child.id) } { (templates::helpers::ItemName::new(&child, &item_classes.get(&child.class).unwrap())) } + a href={ "/item/" (child.id) } { (ItemName::new(&child, &item_classes.get(&child.class).unwrap())) } + } + } + } + } + + @if !original_packaging_of.is_empty() { + h3 .mt-4 { "Original Packaging of" } + + ul { + @for item in original_packaging_of { + li { + a href={ "/item/" (item.id) } { (ItemName::new(&item, &item_classes.get(&item.class).unwrap())) } } } } @@ -185,7 +222,7 @@ async fn list(pool: web::Data, user: Identity) -> actix_web::Result Result { query_as!( Item, - "INSERT INTO items (name, parent, class) VALUES ($1, $2, $3) RETURNING *", + "INSERT INTO items (name, parent, class, original_packaging) VALUES ($1, $2, $3, $4) RETURNING *", new_item.name, new_item.parent, - new_item.class + new_item.class, + new_item.original_packaging ) .fetch_one(pool) .await @@ -56,11 +57,12 @@ pub async fn get_all_as_map(pool: &PgPool) -> Result, sqlx:: pub async fn update(pool: &PgPool, id: Uuid, modified_item: NewItem) -> Result { query_as!( Item, - "UPDATE items SET name = $2, parent = $3, class = $4 WHERE id = $1 RETURNING *", + "UPDATE items SET name = $2, parent = $3, class = $4, original_packaging = $5 WHERE id = $1 RETURNING *", id, modified_item.name, modified_item.parent, - modified_item.class + modified_item.class, + modified_item.original_packaging ) .fetch_one(pool) .await @@ -110,3 +112,16 @@ pub async fn get_children(pool: &PgPool, id: Uuid) -> Result, sqlx::Er .fetch_all(pool) .await } + +pub async fn original_packaging_contents( + pool: &PgPool, + id: Uuid, +) -> Result, sqlx::Error> { + query_as!( + Item, + "SELECT * FROM items WHERE original_packaging = $1", + id + ) + .fetch_all(pool) + .await +} diff --git a/src/models.rs b/src/models.rs index f4d5d6e..5d6738e 100644 --- a/src/models.rs +++ b/src/models.rs @@ -21,6 +21,7 @@ pub struct Item { #[serde(with = "time::serde::iso8601")] pub created_at: OffsetDateTime, pub short_id: i32, + pub original_packaging: Option, } #[derive(Debug, Deserialize)] @@ -30,6 +31,7 @@ pub struct NewItem { #[serde(default)] pub parent: Option, pub class: Uuid, + pub original_packaging: Option, } // Its structure is not how it could ideally be @@ -43,6 +45,7 @@ pub struct NewItemForm { #[serde(default)] pub parent: Option, pub class: Option, + pub original_packaging: Option, } #[derive(Clone, Debug, Serialize)]