2024-06-24 22:46:04 +02:00
|
|
|
// SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2024-07-05 12:38:45 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
use sqlx::{query, query_as, query_scalar, PgPool};
|
2024-06-24 22:46:04 +02:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
use crate::models::{Item, NewItem};
|
2024-06-24 22:46:04 +02:00
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
pub async fn add(pool: &PgPool, new_item: NewItem) -> Result<Item, sqlx::Error> {
|
|
|
|
query_as!(
|
|
|
|
Item,
|
2024-07-14 15:17:04 +02:00
|
|
|
"INSERT INTO items (name, parent, class, original_packaging, description) VALUES ($1, $2, $3, $4, $5) RETURNING *",
|
2024-07-11 17:25:01 +02:00
|
|
|
new_item.name,
|
|
|
|
new_item.parent,
|
2024-07-13 16:16:30 +02:00
|
|
|
new_item.class,
|
2024-07-14 15:17:04 +02:00
|
|
|
new_item.original_packaging,
|
|
|
|
new_item.description
|
2024-07-11 17:25:01 +02:00
|
|
|
)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
2024-06-24 22:46:04 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 12:48:19 +02:00
|
|
|
pub async fn add_multiple(
|
|
|
|
pool: &PgPool,
|
|
|
|
new_item: NewItem,
|
|
|
|
quantity: usize,
|
|
|
|
) -> Result<Vec<Item>, sqlx::Error> {
|
|
|
|
query_as!(
|
|
|
|
Item,
|
|
|
|
r#"INSERT INTO items (name, parent, class, original_packaging, description)
|
|
|
|
SELECT * FROM UNNEST($1::VARCHAR[], $2::UUID[], $3::UUID[], $4::UUID[], $5::VARCHAR[])
|
|
|
|
RETURNING *"#,
|
|
|
|
&vec![new_item.name; quantity] as &[Option<String>],
|
|
|
|
&vec![new_item.parent; quantity] as &[Option<Uuid>],
|
|
|
|
&vec![new_item.class; quantity],
|
|
|
|
&vec![new_item.original_packaging; quantity] as &[Option<Uuid>],
|
|
|
|
&vec![new_item.description; quantity]
|
|
|
|
)
|
|
|
|
.fetch_all(pool)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
pub async fn get(pool: &PgPool, id: Uuid) -> Result<Item, sqlx::Error> {
|
|
|
|
query_as!(Item, "SELECT * FROM items WHERE id = $1", id)
|
|
|
|
.fetch_one(pool)
|
2024-07-07 14:55:05 +02:00
|
|
|
.await
|
2024-06-24 22:46:04 +02:00
|
|
|
}
|
|
|
|
|
2024-07-12 17:23:17 +02:00
|
|
|
pub async fn get_by_short_id(pool: &PgPool, short_id: i32) -> Result<Item, sqlx::Error> {
|
|
|
|
query_as!(Item, "SELECT * FROM items WHERE short_id = $1", short_id)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
pub async fn get_all(pool: &PgPool) -> Result<Vec<Item>, sqlx::Error> {
|
2024-07-11 21:50:03 +02:00
|
|
|
query_as!(Item, "SELECT * FROM items ORDER BY created_at")
|
|
|
|
.fetch_all(pool)
|
|
|
|
.await
|
2024-06-24 22:46:04 +02:00
|
|
|
}
|
2024-07-03 18:47:29 +02:00
|
|
|
|
2024-07-12 00:50:32 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
pub async fn get_all_as_map(pool: &PgPool) -> Result<HashMap<Uuid, Item>, sqlx::Error> {
|
|
|
|
Ok(get_all(pool)
|
2024-07-08 22:39:54 +02:00
|
|
|
.await?
|
|
|
|
.into_iter()
|
|
|
|
.map(|i| (i.id, i))
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
pub async fn update(pool: &PgPool, id: Uuid, modified_item: NewItem) -> Result<Item, sqlx::Error> {
|
|
|
|
query_as!(
|
|
|
|
Item,
|
2024-07-14 15:17:04 +02:00
|
|
|
"UPDATE items SET name = $2, parent = $3, class = $4, original_packaging = $5, description = $6 WHERE id = $1 RETURNING *",
|
2024-07-11 17:25:01 +02:00
|
|
|
id,
|
|
|
|
modified_item.name,
|
|
|
|
modified_item.parent,
|
2024-07-13 16:16:30 +02:00
|
|
|
modified_item.class,
|
2024-07-14 15:17:04 +02:00
|
|
|
modified_item.original_packaging,
|
|
|
|
modified_item.description
|
2024-07-11 17:25:01 +02:00
|
|
|
)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
2024-07-03 18:47:29 +02:00
|
|
|
}
|
2024-07-05 12:38:45 +02:00
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<(), sqlx::Error> {
|
|
|
|
let res = query!("DELETE FROM items WHERE id = $1", id)
|
|
|
|
.execute(pool)
|
2024-07-07 14:55:05 +02:00
|
|
|
.await?;
|
2024-07-11 17:25:01 +02:00
|
|
|
assert_eq!(res.rows_affected(), 1);
|
2024-07-07 14:04:31 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
pub async fn get_parents(pool: &PgPool, id: Uuid) -> Result<Vec<Uuid>, sqlx::Error> {
|
|
|
|
// force nullable is required for all columns in views
|
|
|
|
query_scalar!(
|
|
|
|
r#"SELECT unnest(parents) as "parents!" FROM item_tree WHERE id = $1"#,
|
|
|
|
id
|
|
|
|
)
|
|
|
|
.fetch_all(pool)
|
|
|
|
.await
|
2024-07-05 12:38:45 +02:00
|
|
|
}
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
pub async fn get_all_parents(pool: &PgPool) -> Result<HashMap<Uuid, Vec<Uuid>>, sqlx::Error> {
|
|
|
|
let mut parents = HashMap::new();
|
|
|
|
for row in query!(r#"SELECT id as "id!", parents as "parents!" FROM item_tree"#)
|
|
|
|
.fetch_all(pool)
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
parents.insert(row.id, row.parents);
|
|
|
|
}
|
|
|
|
Ok(parents)
|
2024-07-05 12:38:45 +02:00
|
|
|
}
|
2024-07-08 22:39:54 +02:00
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
pub async fn get_parents_details(pool: &PgPool, id: Uuid) -> Result<Vec<Item>, sqlx::Error> {
|
|
|
|
query_as!(
|
|
|
|
Item,
|
2024-07-16 19:00:48 +02:00
|
|
|
"SELECT items.*
|
|
|
|
FROM items
|
|
|
|
INNER JOIN
|
|
|
|
unnest((SELECT parents FROM item_tree WHERE id = $1))
|
|
|
|
WITH ORDINALITY AS parents(id, n)
|
|
|
|
ON items.id = parents.id
|
|
|
|
ORDER BY parents.n;",
|
2024-07-11 17:25:01 +02:00
|
|
|
id
|
|
|
|
)
|
|
|
|
.fetch_all(pool)
|
|
|
|
.await
|
2024-07-08 22:39:54 +02:00
|
|
|
}
|
2024-07-08 22:45:28 +02:00
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
pub async fn get_children(pool: &PgPool, id: Uuid) -> Result<Vec<Item>, sqlx::Error> {
|
|
|
|
query_as!(Item, "SELECT * FROM items WHERE parent = $1", id)
|
|
|
|
.fetch_all(pool)
|
2024-07-08 22:45:28 +02:00
|
|
|
.await
|
|
|
|
}
|
2024-07-13 16:16:30 +02:00
|
|
|
|
|
|
|
pub async fn original_packaging_contents(
|
|
|
|
pool: &PgPool,
|
|
|
|
id: Uuid,
|
|
|
|
) -> Result<Vec<Item>, sqlx::Error> {
|
|
|
|
query_as!(
|
|
|
|
Item,
|
|
|
|
"SELECT * FROM items WHERE original_packaging = $1",
|
|
|
|
id
|
|
|
|
)
|
|
|
|
.fetch_all(pool)
|
|
|
|
.await
|
|
|
|
}
|