diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-10-08 11:54:04 +0200 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-10-08 11:54:04 +0200 |
| commit | 08cf86a44a9a7c513cd12cbc4a0bac7c029b9ded (patch) | |
| tree | 88b202b25ec22b86f3b4df9f2022b7b23ec3cba1 /crates/rocie-server/src/storage/sql/get | |
| parent | chore(crates/rocie-client): Regenerate (diff) | |
| download | server-08cf86a44a9a7c513cd12cbc4a0bac7c029b9ded.zip | |
feat(crates/rocie-server/unit-property): Init
Diffstat (limited to 'crates/rocie-server/src/storage/sql/get')
6 files changed, 137 insertions, 12 deletions
diff --git a/crates/rocie-server/src/storage/sql/get/barcode/mod.rs b/crates/rocie-server/src/storage/sql/get/barcode/mod.rs index 7b656b1..4eba105 100644 --- a/crates/rocie-server/src/storage/sql/get/barcode/mod.rs +++ b/crates/rocie-server/src/storage/sql/get/barcode/mod.rs @@ -1,8 +1,8 @@ use crate::{ app::App, storage::sql::{ - barcode::{Barcode, BarcodeId, UnitAmount}, - unit::UnitId, + barcode::{Barcode, BarcodeId}, + unit::{UnitAmount, UnitId}, }, }; diff --git a/crates/rocie-server/src/storage/sql/get/mod.rs b/crates/rocie-server/src/storage/sql/get/mod.rs index 048cb3d..62047b8 100644 --- a/crates/rocie-server/src/storage/sql/get/mod.rs +++ b/crates/rocie-server/src/storage/sql/get/mod.rs @@ -1,4 +1,5 @@ pub(crate) mod product; pub(crate) mod product_amount; pub(crate) mod unit; +pub(crate) mod unit_property; pub(crate) mod barcode; diff --git a/crates/rocie-server/src/storage/sql/get/product/mod.rs b/crates/rocie-server/src/storage/sql/get/product/mod.rs index 541f388..01047b2 100644 --- a/crates/rocie-server/src/storage/sql/get/product/mod.rs +++ b/crates/rocie-server/src/storage/sql/get/product/mod.rs @@ -1,9 +1,10 @@ use crate::{ app::App, storage::sql::{ - barcode::{Barcode, BarcodeId, UnitAmount}, + barcode::{Barcode, BarcodeId}, product::{Product, ProductId}, - unit::UnitId, + unit::{UnitAmount, UnitId}, + unit_property::UnitPropertyId, }, }; @@ -13,7 +14,7 @@ impl Product { pub(crate) async fn from_id(app: &App, id: ProductId) -> Result<Option<Self>, from_id::Error> { let record = query!( " - SELECT name, description, parent + SELECT name, unit_property, description, parent FROM products WHERE id = ? ", @@ -25,6 +26,7 @@ impl Product { if let Some(record) = record { Ok(Some(Self { id, + unit_property: UnitPropertyId::from_db(&record.unit_property), name: record.name, description: record.description, associated_bar_codes: vec![], // todo @@ -37,7 +39,7 @@ impl Product { pub(crate) async fn get_all(app: &App) -> Result<Vec<Self>, get_all::Error> { let records = query!( " - SELECT id, name, description, parent + SELECT id, unit_property, name, description, parent FROM products " ) @@ -59,6 +61,7 @@ impl Product { all.push(Self { id: ProductId::from_db(&record.id), + unit_property: UnitPropertyId::from_db(&record.unit_property), name: record.name, description: record.description, associated_bar_codes: barcodes diff --git a/crates/rocie-server/src/storage/sql/get/product_amount/mod.rs b/crates/rocie-server/src/storage/sql/get/product_amount/mod.rs index 7700274..f82c2a0 100644 --- a/crates/rocie-server/src/storage/sql/get/product_amount/mod.rs +++ b/crates/rocie-server/src/storage/sql/get/product_amount/mod.rs @@ -1,8 +1,6 @@ use crate::{ app::App, - storage::sql::{ - barcode::UnitAmount, product::ProductId, product_amount::ProductAmount, unit::UnitId, - }, + storage::sql::{product::ProductId, product_amount::ProductAmount, unit::{UnitAmount, UnitId}}, }; use sqlx::query; @@ -20,6 +18,7 @@ SELECT FROM barcodes JOIN products ON products.id = ? JOIN buys ON buys.barcode_id = barcodes.id +WHERE barcodes.product_id = products.id GROUP BY barcodes.unit; "#, product_id diff --git a/crates/rocie-server/src/storage/sql/get/unit/mod.rs b/crates/rocie-server/src/storage/sql/get/unit/mod.rs index 6c2bbcc..6f5d297 100644 --- a/crates/rocie-server/src/storage/sql/get/unit/mod.rs +++ b/crates/rocie-server/src/storage/sql/get/unit/mod.rs @@ -1,6 +1,9 @@ use crate::{ app::App, - storage::sql::unit::{Unit, UnitId}, + storage::sql::{ + unit::{Unit, UnitId}, + unit_property::UnitPropertyId, + }, }; use sqlx::query; @@ -9,7 +12,7 @@ impl Unit { pub(crate) async fn get_all(app: &App) -> Result<Vec<Self>, get_all::Error> { let records = query!( " - SELECT id, full_name_singular, full_name_plural, short_name, description + SELECT id, unit_property, full_name_singular, full_name_plural, short_name, description FROM units " ) @@ -20,6 +23,7 @@ impl Unit { .into_iter() .map(|record| Self { id: UnitId::from_db(&record.id), + unit_property: UnitPropertyId::from_db(&record.unit_property), full_name_singular: record.full_name_singular, full_name_plural: record.full_name_plural, short_name: record.short_name, @@ -31,7 +35,7 @@ impl Unit { pub(crate) async fn from_id(app: &App, id: UnitId) -> Result<Option<Self>, from_id::Error> { let record = query!( " - SELECT full_name_singular, full_name_plural, short_name, description + SELECT full_name_singular, unit_property, full_name_plural, short_name, description FROM units WHERE id = ? ", @@ -43,6 +47,7 @@ impl Unit { if let Some(record) = record { Ok(Some(Self { id, + unit_property: UnitPropertyId::from_db(&record.unit_property), full_name_singular: record.full_name_singular, full_name_plural: record.full_name_plural, short_name: record.short_name, diff --git a/crates/rocie-server/src/storage/sql/get/unit_property/mod.rs b/crates/rocie-server/src/storage/sql/get/unit_property/mod.rs new file mode 100644 index 0000000..be24181 --- /dev/null +++ b/crates/rocie-server/src/storage/sql/get/unit_property/mod.rs @@ -0,0 +1,117 @@ +use crate::{ + app::App, + storage::sql::{ + unit::UnitId, + unit_property::{UnitProperty, UnitPropertyId}, + }, +}; + +use sqlx::query; + +impl UnitProperty { + pub(crate) async fn get_all(app: &App) -> Result<Vec<Self>, get_all::Error> { + let records = query!( + " + SELECT id, name, description + FROM unit_properties +" + ) + .fetch_all(&app.db) + .await?; + + let mut output = Vec::with_capacity(records.len()); + for record in records { + let units = query!( + " + SELECT id + FROM units + WHERE units.unit_property = ? + ", + record.id + ) + .fetch_all(&app.db) + .await?; + + let units = units + .into_iter() + .map(|record| UnitId::from_db(&record.id)) + .collect(); + + output.push(Self { + id: UnitPropertyId::from_db(&record.id), + units, + name: record.name, + description: record.description, + }) + } + + Ok(output) + } + + pub(crate) async fn from_id( + app: &App, + id: UnitPropertyId, + ) -> Result<Option<Self>, from_id::Error> { + let record = query!( + " + SELECT name, description + FROM unit_properties + WHERE id = ? +", + id + ) + .fetch_optional(&app.db) + .await?; + + if let Some(record) = record { + let units = query!( + " + SELECT id + FROM units + WHERE units.unit_property = ? + ", + id + ) + .fetch_all(&app.db) + .await?; + + let units = units + .into_iter() + .map(|record| UnitId::from_db(&record.id)) + .collect(); + + Ok(Some(Self { + id, + units, + name: record.name, + description: record.description, + })) + } else { + Ok(None) + } + } +} + +pub(crate) mod get_all { + use actix_web::ResponseError; + + #[derive(thiserror::Error, Debug)] + pub(crate) enum Error { + #[error("Failed to execute the sql query")] + SqlError(#[from] sqlx::Error), + } + + impl ResponseError for Error {} +} + +pub(crate) mod from_id { + use actix_web::ResponseError; + + #[derive(thiserror::Error, Debug)] + pub(crate) enum Error { + #[error("Failed to execute the sql query")] + SqlError(#[from] sqlx::Error), + } + + impl ResponseError for Error {} +} |
