Add short id to item for usage with labels

This commit is contained in:
Simon Bruder 2024-07-12 00:50:32 +02:00
parent eb8b952ba0
commit 1427ed9bf4
Signed by: simon
GPG key ID: 347FF8699CDA0776
14 changed files with 131 additions and 5 deletions

View file

@ -27,6 +27,11 @@
"ordinal": 4, "ordinal": 4,
"name": "created_at", "name": "created_at",
"type_info": "Timestamptz" "type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "short_id",
"type_info": "Int4"
} }
], ],
"parameters": { "parameters": {
@ -41,6 +46,7 @@
true, true,
true, true,
false, false,
false,
false false
] ]
}, },

View file

@ -27,6 +27,11 @@
"ordinal": 4, "ordinal": 4,
"name": "created_at", "name": "created_at",
"type_info": "Timestamptz" "type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "short_id",
"type_info": "Int4"
} }
], ],
"parameters": { "parameters": {
@ -39,6 +44,7 @@
true, true,
true, true,
false, false,
false,
false false
] ]
}, },

View file

@ -27,6 +27,11 @@
"ordinal": 4, "ordinal": 4,
"name": "created_at", "name": "created_at",
"type_info": "Timestamptz" "type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "short_id",
"type_info": "Int4"
} }
], ],
"parameters": { "parameters": {
@ -39,6 +44,7 @@
true, true,
true, true,
false, false,
false,
false false
] ]
}, },

View file

@ -27,6 +27,11 @@
"ordinal": 4, "ordinal": 4,
"name": "created_at", "name": "created_at",
"type_info": "Timestamptz" "type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "short_id",
"type_info": "Int4"
} }
], ],
"parameters": { "parameters": {
@ -37,6 +42,7 @@
true, true,
true, true,
false, false,
false,
false false
] ]
}, },

View file

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

View file

@ -27,6 +27,11 @@
"ordinal": 4, "ordinal": 4,
"name": "created_at", "name": "created_at",
"type_info": "Timestamptz" "type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "short_id",
"type_info": "Int4"
} }
], ],
"parameters": { "parameters": {
@ -42,6 +47,7 @@
true, true,
true, true,
false, false,
false,
false false
] ]
}, },

View file

@ -27,6 +27,11 @@
"ordinal": 4, "ordinal": 4,
"name": "created_at", "name": "created_at",
"type_info": "Timestamptz" "type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "short_id",
"type_info": "Int4"
} }
], ],
"parameters": { "parameters": {
@ -39,6 +44,7 @@
true, true,
true, true,
false, false,
false,
false false
] ]
}, },

View file

@ -27,6 +27,11 @@
"ordinal": 4, "ordinal": 4,
"name": "created_at", "name": "created_at",
"type_info": "Timestamptz" "type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "short_id",
"type_info": "Int4"
} }
], ],
"parameters": { "parameters": {
@ -39,6 +44,7 @@
true, true,
true, true,
false, false,
false,
false false
] ]
}, },

View file

@ -0,0 +1,6 @@
-- SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later
ALTER TABLE items
DROP short_id;

View file

@ -0,0 +1,6 @@
-- SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later
ALTER TABLE items
ADD short_id INTEGER UNIQUE GENERATED ALWAYS AS IDENTITY;

View file

@ -5,9 +5,11 @@
use actix_web::http::header::{ContentDisposition, ContentType, DispositionParam, DispositionType}; use actix_web::http::header::{ContentDisposition, ContentType, DispositionParam, DispositionType};
use actix_web::{error, get, web, HttpResponse, Responder}; use actix_web::{error, get, web, HttpResponse, Responder};
use serde::Deserialize; use serde::Deserialize;
use sqlx::PgPool;
use uuid::Uuid; use uuid::Uuid;
use crate::label::{Label, LabelPage, LabelPreset}; use crate::label::{Label, LabelPage, LabelPreset};
use crate::manage;
pub fn config(cfg: &mut web::ServiceConfig) { pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(items); cfg.service(items);
@ -21,7 +23,10 @@ struct QueryParams {
} }
#[get("/label/items")] #[get("/label/items")]
async fn items(params: web::Query<QueryParams>) -> actix_web::Result<impl Responder> { async fn items(
pool: web::Data<PgPool>,
params: web::Query<QueryParams>,
) -> actix_web::Result<impl Responder> {
let ids = params let ids = params
.ids .ids
.split(',') .split(',')
@ -30,14 +35,18 @@ async fn items(params: web::Query<QueryParams>) -> actix_web::Result<impl Respon
.collect::<Result<Vec<Uuid>, uuid::Error>>() .collect::<Result<Vec<Uuid>, uuid::Error>>()
.map_err(error::ErrorInternalServerError)?; .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_config = params.preset.clone().into();
let label = Label { let label = Label {
pages: ids pages: items
.into_iter() .into_iter()
.map(|id| LabelPage { .map(|item| LabelPage {
id: Some(id), id: Some(item.id),
short_id: None, short_id: Some(format!("{:06}", item.short_id)),
}) })
.collect(), .collect(),
config: label_config, config: label_config,

View file

@ -74,6 +74,10 @@ async fn show_item(
th { "UUID" } th { "UUID" }
td { (item.id) } td { (item.id) }
} }
tr {
th { "Short ID" }
td { (item.short_id) }
}
tr { tr {
th { "Name" } th { "Name" }
td { (item_name.clone().terse()) } td { (item_name.clone().terse()) }

View file

@ -33,6 +33,12 @@ pub async fn get_all(pool: &PgPool) -> Result<Vec<Item>, sqlx::Error> {
.await .await
} }
pub async fn get_multiple(pool: &PgPool, ids: &[Uuid]) -> Result<Vec<Item>, 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<HashMap<Uuid, Item>, sqlx::Error> { pub async fn get_all_as_map(pool: &PgPool) -> Result<HashMap<Uuid, Item>, sqlx::Error> {
Ok(get_all(pool) Ok(get_all(pool)
.await? .await?

View file

@ -14,6 +14,7 @@ pub struct Item {
pub class: Uuid, pub class: Uuid,
#[serde(with = "time::serde::iso8601")] #[serde(with = "time::serde::iso8601")]
pub created_at: OffsetDateTime, pub created_at: OffsetDateTime,
pub short_id: i32,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]