From c8d547998c5595bfb09b511b4775454fe9a1d132 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Fri, 12 Jul 2024 00:50:32 +0200 Subject: [PATCH] Add short id to item for usage with labels --- ...9c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json | 6 +++ ...c7ee7520478ff4f5886b4523fb0dc4df0e204.json | 6 +++ ...f28fd05f18706f70220b6ebb7354d2a0a9e3b.json | 6 +++ ...aa92283151d67f52fbb22e5d976b1c6a5c367.json | 6 +++ ...598749d931af69a80d24f4575a4bc2c740f3b.json | 52 +++++++++++++++++++ ...4c85cfc7db545724d94c1a467b2c0c74b6b6e.json | 6 +++ ...fbc5433e972b5522b367098afe6cb90a30bf2.json | 6 +++ ...57d6852d2d3f63dc055442125a6a92950729d.json | 6 +++ .../20240711222015_item_add_short_id.down.sql | 6 +++ .../20240711222015_item_add_short_id.up.sql | 6 +++ src/api/v1/label.rs | 19 +++++-- src/frontend/item.rs | 4 ++ src/manage/item.rs | 6 +++ src/models.rs | 1 + 14 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 .sqlx/query-79d8bfe2ed76ee550cdc31f282f598749d931af69a80d24f4575a4bc2c740f3b.json create mode 100644 migrations/20240711222015_item_add_short_id.down.sql create mode 100644 migrations/20240711222015_item_add_short_id.up.sql diff --git a/.sqlx/query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json b/.sqlx/query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json index 92605eb..8cc93c7 100644 --- a/.sqlx/query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json +++ b/.sqlx/query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json @@ -27,6 +27,11 @@ "ordinal": 4, "name": "created_at", "type_info": "Timestamptz" + }, + { + "ordinal": 5, + "name": "short_id", + "type_info": "Int4" } ], "parameters": { @@ -41,6 +46,7 @@ true, true, false, + false, false ] }, diff --git a/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json b/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json index 1217e4d..5dfb444 100644 --- a/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json +++ b/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json @@ -27,6 +27,11 @@ "ordinal": 4, "name": "created_at", "type_info": "Timestamptz" + }, + { + "ordinal": 5, + "name": "short_id", + "type_info": "Int4" } ], "parameters": { @@ -39,6 +44,7 @@ true, true, false, + false, false ] }, diff --git a/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json b/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json index 73a859f..1e9b115 100644 --- a/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json +++ b/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json @@ -27,6 +27,11 @@ "ordinal": 4, "name": "created_at", "type_info": "Timestamptz" + }, + { + "ordinal": 5, + "name": "short_id", + "type_info": "Int4" } ], "parameters": { @@ -39,6 +44,7 @@ true, true, false, + false, false ] }, diff --git a/.sqlx/query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json b/.sqlx/query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json index 40c6ee7..bb26826 100644 --- a/.sqlx/query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json +++ b/.sqlx/query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json @@ -27,6 +27,11 @@ "ordinal": 4, "name": "created_at", "type_info": "Timestamptz" + }, + { + "ordinal": 5, + "name": "short_id", + "type_info": "Int4" } ], "parameters": { @@ -37,6 +42,7 @@ true, true, false, + false, false ] }, diff --git a/.sqlx/query-79d8bfe2ed76ee550cdc31f282f598749d931af69a80d24f4575a4bc2c740f3b.json b/.sqlx/query-79d8bfe2ed76ee550cdc31f282f598749d931af69a80d24f4575a4bc2c740f3b.json new file mode 100644 index 0000000..fab3573 --- /dev/null +++ b/.sqlx/query-79d8bfe2ed76ee550cdc31f282f598749d931af69a80d24f4575a4bc2c740f3b.json @@ -0,0 +1,52 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT * FROM items WHERE id = ANY ($1)", + "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" + } + ], + "parameters": { + "Left": [ + "UuidArray" + ] + }, + "nullable": [ + false, + true, + true, + false, + false, + false + ] + }, + "hash": "79d8bfe2ed76ee550cdc31f282f598749d931af69a80d24f4575a4bc2c740f3b" +} diff --git a/.sqlx/query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json b/.sqlx/query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json index eb03f0f..f8d747f 100644 --- a/.sqlx/query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json +++ b/.sqlx/query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json @@ -27,6 +27,11 @@ "ordinal": 4, "name": "created_at", "type_info": "Timestamptz" + }, + { + "ordinal": 5, + "name": "short_id", + "type_info": "Int4" } ], "parameters": { @@ -42,6 +47,7 @@ true, true, false, + false, false ] }, diff --git a/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json b/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json index 44006c7..950b148 100644 --- a/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json +++ b/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json @@ -27,6 +27,11 @@ "ordinal": 4, "name": "created_at", "type_info": "Timestamptz" + }, + { + "ordinal": 5, + "name": "short_id", + "type_info": "Int4" } ], "parameters": { @@ -39,6 +44,7 @@ true, true, false, + false, false ] }, diff --git a/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json b/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json index a7b8b6a..7ecd529 100644 --- a/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json +++ b/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json @@ -27,6 +27,11 @@ "ordinal": 4, "name": "created_at", "type_info": "Timestamptz" + }, + { + "ordinal": 5, + "name": "short_id", + "type_info": "Int4" } ], "parameters": { @@ -39,6 +44,7 @@ true, true, false, + false, false ] }, diff --git a/migrations/20240711222015_item_add_short_id.down.sql b/migrations/20240711222015_item_add_short_id.down.sql new file mode 100644 index 0000000..f197239 --- /dev/null +++ b/migrations/20240711222015_item_add_short_id.down.sql @@ -0,0 +1,6 @@ +-- SPDX-FileCopyrightText: 2024 Simon Bruder +-- +-- SPDX-License-Identifier: AGPL-3.0-or-later + +ALTER TABLE items + DROP short_id; diff --git a/migrations/20240711222015_item_add_short_id.up.sql b/migrations/20240711222015_item_add_short_id.up.sql new file mode 100644 index 0000000..d6f75bb --- /dev/null +++ b/migrations/20240711222015_item_add_short_id.up.sql @@ -0,0 +1,6 @@ +-- SPDX-FileCopyrightText: 2024 Simon Bruder +-- +-- SPDX-License-Identifier: AGPL-3.0-or-later + +ALTER TABLE items + ADD short_id INTEGER UNIQUE GENERATED ALWAYS AS IDENTITY; diff --git a/src/api/v1/label.rs b/src/api/v1/label.rs index 4fdd977..4a36ee2 100644 --- a/src/api/v1/label.rs +++ b/src/api/v1/label.rs @@ -5,9 +5,11 @@ use actix_web::http::header::{ContentDisposition, ContentType, DispositionParam, DispositionType}; use actix_web::{error, get, web, HttpResponse, Responder}; use serde::Deserialize; +use sqlx::PgPool; use uuid::Uuid; use crate::label::{Label, LabelPage, LabelPreset}; +use crate::manage; pub fn config(cfg: &mut web::ServiceConfig) { cfg.service(items); @@ -21,7 +23,10 @@ struct QueryParams { } #[get("/label/items")] -async fn items(params: web::Query) -> actix_web::Result { +async fn items( + pool: web::Data, + params: web::Query, +) -> actix_web::Result { let ids = params .ids .split(',') @@ -30,14 +35,18 @@ async fn items(params: web::Query) -> actix_web::Result, uuid::Error>>() .map_err(error::ErrorInternalServerError)?; + let items = manage::item::get_multiple(&pool, &ids) + .await + .map_err(error::ErrorInternalServerError)?; + let label_config = params.preset.clone().into(); let label = Label { - pages: ids + pages: items .into_iter() - .map(|id| LabelPage { - id: Some(id), - short_id: None, + .map(|item| LabelPage { + id: Some(item.id), + short_id: Some(format!("{:06}", item.short_id)), }) .collect(), config: label_config, diff --git a/src/frontend/item.rs b/src/frontend/item.rs index 96878e9..af6e49f 100644 --- a/src/frontend/item.rs +++ b/src/frontend/item.rs @@ -74,6 +74,10 @@ async fn show_item( th { "UUID" } td { (item.id) } } + tr { + th { "Short ID" } + td { (item.short_id) } + } tr { th { "Name" } td { (item_name.clone().terse()) } diff --git a/src/manage/item.rs b/src/manage/item.rs index 62f38d6..1a33f01 100644 --- a/src/manage/item.rs +++ b/src/manage/item.rs @@ -33,6 +33,12 @@ pub async fn get_all(pool: &PgPool) -> Result, sqlx::Error> { .await } +pub async fn get_multiple(pool: &PgPool, ids: &[Uuid]) -> Result, sqlx::Error> { + query_as!(Item, "SELECT * FROM items WHERE id = ANY ($1)", ids) + .fetch_all(pool) + .await +} + pub async fn get_all_as_map(pool: &PgPool) -> Result, sqlx::Error> { Ok(get_all(pool) .await? diff --git a/src/models.rs b/src/models.rs index 80a9950..b743a9c 100644 --- a/src/models.rs +++ b/src/models.rs @@ -14,6 +14,7 @@ pub struct Item { pub class: Uuid, #[serde(with = "time::serde::iso8601")] pub created_at: OffsetDateTime, + pub short_id: i32, } #[derive(Debug, Deserialize)]