From 089e7ffd7878948724ebf6535751691ce45221de Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Thu, 25 Jul 2024 20:53:48 +0200 Subject: [PATCH] fixup! Move database operations to separate module --- src/database/item_classes/mod.rs | 2 -- src/database/items/mod.rs | 44 ++-------------------------- src/database/items/name.rs | 50 ++++++++++++++++++++++++++++++++ src/database/items/show.rs | 3 +- 4 files changed, 53 insertions(+), 46 deletions(-) create mode 100644 src/database/items/name.rs diff --git a/src/database/item_classes/mod.rs b/src/database/item_classes/mod.rs index 4725eba..c6e95ff 100644 --- a/src/database/item_classes/mod.rs +++ b/src/database/item_classes/mod.rs @@ -26,8 +26,6 @@ impl ItemClassRepository { } } -// Common - pub struct ItemClassPreview { pub id: Uuid, pub name: String, diff --git a/src/database/items/mod.rs b/src/database/items/mod.rs index 40e3767..9f2ead0 100644 --- a/src/database/items/mod.rs +++ b/src/database/items/mod.rs @@ -8,6 +8,7 @@ mod delete; mod edit; mod label; mod list; +mod name; mod show; use sqlx::{query, PgPool}; @@ -17,6 +18,7 @@ use crate::database::item_states::ItemState; pub use add::{default_quantity, ItemAddForm, ItemAddFormPrefilled}; pub use edit::ItemEditForm; +pub use name::ItemName; #[derive(Clone)] pub struct ItemRepository { @@ -29,32 +31,6 @@ impl ItemRepository { } } -// Common - -#[derive(Clone)] -pub enum ItemName { - Item(String), - Class(String), - None, -} - -impl ItemName { - pub fn new(item_name: Option<&String>, class_name: &String) -> Self { - if let Some(ref name) = item_name { - Self::Item(name.to_string()) - } else { - Self::Class(class_name.to_string()) - } - } - - pub fn terse(self) -> Self { - match self { - Self::Item(_) => self, - Self::Class(_) | Self::None => Self::None, - } - } -} - pub struct ItemPreview { pub id: Uuid, pub name: ItemName, @@ -85,22 +61,6 @@ impl ItemPreview { } impl ItemRepository { - pub async fn name(&self, id: Uuid) -> sqlx::Result { - query!( - r#"SELECT - items.name, - item_classes.name AS "class_name" - FROM items - JOIN item_classes - ON items.class = item_classes.id - WHERE items.id = $1"#, - id - ) - .map(|row| ItemName::new(row.name.as_ref(), &row.class_name)) - .fetch_one(&self.pool) - .await - } - pub async fn parents(&self, id: Uuid) -> sqlx::Result> { query!( r#"SELECT items.id, items.name, item_classes.name AS "class_name" diff --git a/src/database/items/name.rs b/src/database/items/name.rs new file mode 100644 index 0000000..bfb385d --- /dev/null +++ b/src/database/items/name.rs @@ -0,0 +1,50 @@ +// SPDX-FileCopyrightText: 2024 Simon Bruder +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use sqlx::query; +use uuid::Uuid; + +use super::ItemRepository; + +#[derive(Clone)] +pub enum ItemName { + Item(String), + Class(String), + None, +} + +impl ItemName { + pub fn new(item_name: Option<&String>, class_name: &String) -> Self { + if let Some(ref name) = item_name { + Self::Item(name.to_string()) + } else { + Self::Class(class_name.to_string()) + } + } + + pub fn terse(self) -> Self { + match self { + Self::Item(_) => self, + Self::Class(_) | Self::None => Self::None, + } + } +} + +impl ItemRepository { + pub async fn name(&self, id: Uuid) -> sqlx::Result { + query!( + r#"SELECT + items.name, + item_classes.name AS "class_name" + FROM items + JOIN item_classes + ON items.class = item_classes.id + WHERE items.id = $1"#, + id + ) + .map(|row| ItemName::new(row.name.as_ref(), &row.class_name)) + .fetch_one(&self.pool) + .await + } +} diff --git a/src/database/items/show.rs b/src/database/items/show.rs index b166661..87ccd43 100644 --- a/src/database/items/show.rs +++ b/src/database/items/show.rs @@ -5,8 +5,7 @@ use sqlx::query; use uuid::Uuid; -use super::ItemRepository; -use super::{ItemName, ItemPreview}; +use super::{ItemRepository, ItemName, ItemPreview}; use crate::database::item_states::ItemState; pub struct ItemDetails {