diff options
Diffstat (limited to 'crates/rocie-server/src/storage/sql/insert')
5 files changed, 139 insertions, 16 deletions
diff --git a/crates/rocie-server/src/storage/sql/insert/barcode/mod.rs b/crates/rocie-server/src/storage/sql/insert/barcode/mod.rs index 62a2e11..11707b9 100644 --- a/crates/rocie-server/src/storage/sql/insert/barcode/mod.rs +++ b/crates/rocie-server/src/storage/sql/insert/barcode/mod.rs @@ -9,9 +9,9 @@ use crate::{ storage::{ migrate::get_current_date, sql::{ - barcode::{Barcode, BarcodeId, UnitAmount}, + barcode::{Barcode, BarcodeId}, insert::{Operations, Transactionable}, - unit::Unit, + unit::{Unit, UnitAmount}, }, }, }; @@ -208,10 +208,7 @@ pub(crate) mod consume { use crate::storage::{ self, - sql::{ - barcode::UnitAmount, - unit::{Unit, UnitId}, - }, + sql::unit::{Unit, UnitAmount, UnitId}, }; #[derive(thiserror::Error, Debug)] diff --git a/crates/rocie-server/src/storage/sql/insert/mod.rs b/crates/rocie-server/src/storage/sql/insert/mod.rs index e6728d9..3b2d702 100644 --- a/crates/rocie-server/src/storage/sql/insert/mod.rs +++ b/crates/rocie-server/src/storage/sql/insert/mod.rs @@ -10,6 +10,7 @@ use sqlx::{SqliteConnection, query}; pub(crate) mod barcode; pub(crate) mod product; pub(crate) mod unit; +pub(crate) mod unit_property; pub(crate) trait Transactionable: Sized + std::fmt::Debug + Serialize + DeserializeOwned diff --git a/crates/rocie-server/src/storage/sql/insert/product/mod.rs b/crates/rocie-server/src/storage/sql/insert/product/mod.rs index e14d3f4..d762e9b 100644 --- a/crates/rocie-server/src/storage/sql/insert/product/mod.rs +++ b/crates/rocie-server/src/storage/sql/insert/product/mod.rs @@ -6,6 +6,7 @@ use crate::storage::sql::{ barcode::Barcode, insert::{Operations, Transactionable}, product::{Product, ProductId}, + unit_property::UnitPropertyId, }; #[derive(Debug, Deserialize, Serialize)] @@ -15,6 +16,7 @@ pub(crate) enum Operation { name: String, description: Option<String>, parent: Option<ProductId>, + unit_property: UnitPropertyId, }, AssociateBarcode { id: ProductId, @@ -33,13 +35,15 @@ impl Transactionable for Operation { name, description, parent, + unit_property, } => { query!( " - INSERT INTO products (id, name, description, parent) - VALUES (?,?,?,?) + INSERT INTO products (id, unit_property, name, description, parent) + VALUES (?,?,?,?,?) ", id, + unit_property, name, description, parent @@ -75,17 +79,19 @@ impl Transactionable for Operation { id, name, description, + unit_property, parent, } => { query!( " DELETE FROM products - WHERE id = ? AND name = ? AND description = ? AND parent = ?; + WHERE id = ? AND name = ? AND description = ? AND parent = ? AND unit_property = ?; ", id, name, description, - parent + parent, + unit_property ) .execute(txn) .await?; @@ -133,6 +139,7 @@ impl Product { name: String, description: Option<String>, parent: Option<ProductId>, + unit_property: UnitPropertyId, ops: &mut Operations<Operation>, ) -> Self { let id = ProductId::from(Uuid::new_v4()); @@ -141,6 +148,7 @@ impl Product { id, name: name.clone(), description: description.clone(), + unit_property, parent, }); @@ -148,6 +156,7 @@ impl Product { id, name, description, + unit_property, associated_bar_codes: vec![], } } diff --git a/crates/rocie-server/src/storage/sql/insert/unit/mod.rs b/crates/rocie-server/src/storage/sql/insert/unit/mod.rs index ba08487..815cb1e 100644 --- a/crates/rocie-server/src/storage/sql/insert/unit/mod.rs +++ b/crates/rocie-server/src/storage/sql/insert/unit/mod.rs @@ -2,7 +2,11 @@ use serde::{Deserialize, Serialize}; use sqlx::query; use uuid::Uuid; -use crate::storage::sql::{insert::{Operations, Transactionable}, unit::{Unit, UnitId}}; +use crate::storage::sql::{ + insert::{Operations, Transactionable}, + unit::{Unit, UnitId}, + unit_property::UnitPropertyId, +}; #[derive(Debug, Deserialize, Serialize)] pub(crate) enum Operation { @@ -11,6 +15,7 @@ pub(crate) enum Operation { full_name_singular: String, full_name_plural: String, short_name: String, + unit_property: UnitPropertyId, description: Option<String>, }, } @@ -27,13 +32,14 @@ impl Transactionable for Operation { full_name_plural, short_name, description, + unit_property, } => { query!( " - INSERT INTO units (id, full_name_singular, full_name_plural, short_name, description) - VALUES (?,?,?,?,?) + INSERT INTO units (id, unit_property, full_name_singular, full_name_plural, short_name, description) + VALUES (?,?,?,?,?,?) ", - id, full_name_singular, full_name_plural, short_name, description, + id, unit_property, full_name_singular, full_name_plural, short_name, description, ) .execute(txn) .await?; @@ -50,13 +56,14 @@ impl Transactionable for Operation { full_name_plural, short_name, description, + unit_property, } => { query!( " DELETE FROM units - WHERE id = ? AND full_name_singular = ? AND full_name_plural = ? AND short_name = ? AND description = ?; + WHERE id = ? AND full_name_singular = ? AND full_name_plural = ? AND short_name = ? AND description = ? AND unit_property = ?; ", - id, full_name_singular, full_name_plural, short_name, description, + id, full_name_singular, full_name_plural, short_name, description, unit_property ) .execute(txn) .await?; @@ -87,6 +94,7 @@ impl Unit { full_name_plural: String, short_name: String, description: Option<String>, + unit_property: UnitPropertyId, ops: &mut Operations<Operation>, ) -> Self { let id = UnitId::from(Uuid::new_v4()); @@ -97,6 +105,7 @@ impl Unit { full_name_plural: full_name_plural.clone(), short_name: short_name.clone(), description: description.clone(), + unit_property, }); Self { @@ -105,6 +114,7 @@ impl Unit { full_name_plural, short_name, description, + unit_property, } } } diff --git a/crates/rocie-server/src/storage/sql/insert/unit_property/mod.rs b/crates/rocie-server/src/storage/sql/insert/unit_property/mod.rs new file mode 100644 index 0000000..d340465 --- /dev/null +++ b/crates/rocie-server/src/storage/sql/insert/unit_property/mod.rs @@ -0,0 +1,106 @@ +use serde::{Deserialize, Serialize}; +use sqlx::query; +use uuid::Uuid; + +use crate::storage::sql::{ + insert::{Operations, Transactionable}, + unit_property::{UnitProperty, UnitPropertyId}, +}; + +#[derive(Debug, Deserialize, Serialize)] +pub(crate) enum Operation { + RegisterUnitProperty { + id: UnitPropertyId, + name: String, + description: Option<String>, + }, +} + +impl Transactionable for Operation { + type ApplyError = apply::Error; + type UndoError = undo::Error; + + async fn apply(self, txn: &mut sqlx::SqliteConnection) -> Result<(), apply::Error> { + match self { + Operation::RegisterUnitProperty { + id, + name, + description, + } => { + query!( + " + INSERT INTO unit_properties (id, name, description) + VALUES (?,?,?) +", + id, + name, + description, + ) + .execute(txn) + .await?; + } + } + Ok(()) + } + + async fn undo(self, txn: &mut sqlx::SqliteConnection) -> Result<(), undo::Error> { + match self { + Operation::RegisterUnitProperty { + id, + name, + description, + } => { + query!( + " + DELETE FROM unit_properties + WHERE id = ? AND name = ? AND description = ?; +", + id, + name, + description + ) + .execute(txn) + .await?; + } + } + Ok(()) + } +} + +pub(crate) mod undo { + #[derive(thiserror::Error, Debug)] + pub(crate) enum Error { + #[error("Failed to execute undo sql statments: {0}")] + SqlError(#[from] sqlx::Error), + } +} +pub(crate) mod apply { + #[derive(thiserror::Error, Debug)] + pub(crate) enum Error { + #[error("Failed to execute apply sql statments: {0}")] + SqlError(#[from] sqlx::Error), + } +} + +impl UnitProperty { + pub(crate) fn register( + name: String, + description: Option<String>, + ops: &mut Operations<Operation>, + ) -> Self { + let id = UnitPropertyId::from(Uuid::new_v4()); + + ops.push(Operation::RegisterUnitProperty { + id, + name: name.clone(), + description: description.clone(), + }); + + Self { + id, + units: vec![], + name, + description, + } + } +} |
