fixup! Move database operations to separate module
This commit is contained in:
parent
e06531e1cd
commit
089e7ffd78
|
@ -26,8 +26,6 @@ impl ItemClassRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Common
|
|
||||||
|
|
||||||
pub struct ItemClassPreview {
|
pub struct ItemClassPreview {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
|
@ -8,6 +8,7 @@ mod delete;
|
||||||
mod edit;
|
mod edit;
|
||||||
mod label;
|
mod label;
|
||||||
mod list;
|
mod list;
|
||||||
|
mod name;
|
||||||
mod show;
|
mod show;
|
||||||
|
|
||||||
use sqlx::{query, PgPool};
|
use sqlx::{query, PgPool};
|
||||||
|
@ -17,6 +18,7 @@ use crate::database::item_states::ItemState;
|
||||||
|
|
||||||
pub use add::{default_quantity, ItemAddForm, ItemAddFormPrefilled};
|
pub use add::{default_quantity, ItemAddForm, ItemAddFormPrefilled};
|
||||||
pub use edit::ItemEditForm;
|
pub use edit::ItemEditForm;
|
||||||
|
pub use name::ItemName;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ItemRepository {
|
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 struct ItemPreview {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub name: ItemName,
|
pub name: ItemName,
|
||||||
|
@ -85,22 +61,6 @@ impl ItemPreview {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ItemRepository {
|
impl ItemRepository {
|
||||||
pub async fn name(&self, id: Uuid) -> sqlx::Result<ItemName> {
|
|
||||||
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<Vec<ItemPreview>> {
|
pub async fn parents(&self, id: Uuid) -> sqlx::Result<Vec<ItemPreview>> {
|
||||||
query!(
|
query!(
|
||||||
r#"SELECT items.id, items.name, item_classes.name AS "class_name"
|
r#"SELECT items.id, items.name, item_classes.name AS "class_name"
|
||||||
|
|
50
src/database/items/name.rs
Normal file
50
src/database/items/name.rs
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
|
||||||
|
//
|
||||||
|
// 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<ItemName> {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,8 +5,7 @@
|
||||||
use sqlx::query;
|
use sqlx::query;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use super::ItemRepository;
|
use super::{ItemRepository, ItemName, ItemPreview};
|
||||||
use super::{ItemName, ItemPreview};
|
|
||||||
use crate::database::item_states::ItemState;
|
use crate::database::item_states::ItemState;
|
||||||
|
|
||||||
pub struct ItemDetails {
|
pub struct ItemDetails {
|
||||||
|
|
Loading…
Reference in a new issue