From c56d13b196a4db08636d16b2656ad70e79b1a17a Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 13 Jul 2024 19:37:30 +0530 Subject: [PATCH] feat: cqrs_es scaffolding --- Cargo.lock | 10 ++++-- Cargo.toml | 3 +- ...40713080804_cqrs_inventory_store_query.sql | 32 +++++++++++++++++ src/inventory/adapters/output/db/mod.rs | 1 + src/inventory/adapters/output/mod.rs | 2 ++ src/inventory/application/mod.rs | 4 +-- src/inventory/application/port/mod.rs | 4 +-- .../application/port/output/db/mod.rs | 2 +- src/inventory/application/port/output/mod.rs | 2 ++ src/inventory/application/services/mod.rs | 12 +++---- src/inventory/domain/commands.rs | 17 ++++++++++ src/inventory/domain/events.rs | 34 +++++++++++++++++++ src/inventory/domain/mod.rs | 16 +++++++++ 13 files changed, 125 insertions(+), 14 deletions(-) create mode 100644 migrations/20240713080804_cqrs_inventory_store_query.sql create mode 100644 src/inventory/adapters/output/db/mod.rs create mode 100644 src/inventory/domain/commands.rs create mode 100644 src/inventory/domain/events.rs diff --git a/Cargo.lock b/Cargo.lock index 324e8cd..a393bec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3228,6 +3228,7 @@ dependencies = [ "tokio-stream", "tracing", "url", + "uuid", "webpki-roots 0.25.4", ] @@ -3310,6 +3311,7 @@ dependencies = [ "thiserror", "time", "tracing", + "uuid", "whoami", ] @@ -3349,6 +3351,7 @@ dependencies = [ "thiserror", "time", "tracing", + "uuid", "whoami", ] @@ -3374,6 +3377,7 @@ dependencies = [ "tracing", "url", "urlencoding", + "uuid", ] [[package]] @@ -3952,11 +3956,12 @@ checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" [[package]] name = "uuid" -version = "1.8.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0" +checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" dependencies = [ "getrandom", + "serde", ] [[package]] @@ -4062,6 +4067,7 @@ dependencies = [ "tracing", "tracing-actix-web", "url", + "uuid", "validator 0.18.1", ] diff --git a/Cargo.toml b/Cargo.toml index 69d620c..bbab54e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,12 +28,13 @@ rand = "0.8.5" rust-embed = { version = "8.4.0", features = ["include-exclude"] } serde = { version = "1.0.201", features = ["derive"] } serde_json = "1.0.117" -sqlx = { version = "0.7.4", features = ["runtime-tokio-rustls", "postgres", "time"] } +sqlx = { version = "0.7.4", features = ["runtime-tokio-rustls", "postgres", "time", "uuid"] } tera = "1.19.1" time = { version = "0.3.36", features = ["serde"] } tracing = { version = "0.1.40", features = ["log"] } tracing-actix-web = "0.7.10" url = { version = "2.5.0", features = ["serde"] } +uuid = { version = "1.10.0", features = ["v4", "serde"] } validator = { version = "0.18.1", features = ["derive"] } [dev-dependencies] diff --git a/migrations/20240713080804_cqrs_inventory_store_query.sql b/migrations/20240713080804_cqrs_inventory_store_query.sql new file mode 100644 index 0000000..3d2d7b9 --- /dev/null +++ b/migrations/20240713080804_cqrs_inventory_store_query.sql @@ -0,0 +1,32 @@ +--- SPDX-FileCopyrightText: 2024 Aravinth Manivannan +-- +-- SPDX-License-Identifier: AGPL-3.0-or-later + +CREATE TABLE IF NOT EXISTS categoty_events +( + aggregate_type text NOT NULL, + aggregate_id text NOT NULL, + sequence bigint CHECK (sequence >= 0) NOT NULL, + event_type text NOT NULL, + event_version text NOT NULL, + payload json NOT NULL, + metadata json NOT NULL, + timestamp timestamp with time zone DEFAULT (CURRENT_TIMESTAMP), + PRIMARY KEY (aggregate_type, aggregate_id, sequence) +); + +CREATE TABLE IF NOT EXISTS cqrs_inventory_store_query +( + view_id text NOT NULL, + version bigint CHECK (version >= 0) NOT NULL, + + name TEXT NOT NULL, + address TEXT, + owner TEXT NOT NULL, + store_id UUID NOT NULL UNIQUE, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + + PRIMARY KEY (view_id) +); + +CREATE UNIQUE INDEX IF NOT EXISTS store_store_id_index ON cqrs_inventory_store_query (store_id); diff --git a/src/inventory/adapters/output/db/mod.rs b/src/inventory/adapters/output/db/mod.rs new file mode 100644 index 0000000..26e9103 --- /dev/null +++ b/src/inventory/adapters/output/db/mod.rs @@ -0,0 +1 @@ +pub mod postgres; diff --git a/src/inventory/adapters/output/mod.rs b/src/inventory/adapters/output/mod.rs index 56f60de..4589484 100644 --- a/src/inventory/adapters/output/mod.rs +++ b/src/inventory/adapters/output/mod.rs @@ -1,3 +1,5 @@ // SPDX-FileCopyrightText: 2024 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later + +mod db; diff --git a/src/inventory/application/mod.rs b/src/inventory/application/mod.rs index 357da8f..2f75b72 100644 --- a/src/inventory/application/mod.rs +++ b/src/inventory/application/mod.rs @@ -2,5 +2,5 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later -mod port; -mod services; +pub mod port; +pub mod services; diff --git a/src/inventory/application/port/mod.rs b/src/inventory/application/port/mod.rs index 9b25f58..f571c4e 100644 --- a/src/inventory/application/port/mod.rs +++ b/src/inventory/application/port/mod.rs @@ -2,5 +2,5 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later -mod input; -mod output; +pub mod input; +pub mod output; diff --git a/src/inventory/application/port/output/db/mod.rs b/src/inventory/application/port/output/db/mod.rs index 7e35ef6..efa524e 100644 --- a/src/inventory/application/port/output/db/mod.rs +++ b/src/inventory/application/port/output/db/mod.rs @@ -2,6 +2,6 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later -pub mod category_exists; +//pub mod category_exists; pub mod errors; pub mod store_id_exists; diff --git a/src/inventory/application/port/output/mod.rs b/src/inventory/application/port/output/mod.rs index 56f60de..1589173 100644 --- a/src/inventory/application/port/output/mod.rs +++ b/src/inventory/application/port/output/mod.rs @@ -1,3 +1,5 @@ // SPDX-FileCopyrightText: 2024 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later + +pub mod db; diff --git a/src/inventory/application/services/mod.rs b/src/inventory/application/services/mod.rs index 7f5e3e0..85d1758 100644 --- a/src/inventory/application/services/mod.rs +++ b/src/inventory/application/services/mod.rs @@ -8,26 +8,26 @@ use mockall::*; pub mod errors; // services -pub mod add_category_service; +//pub mod add_category_service; pub mod add_store_service; #[automock] pub trait InventoryServicesInterface: Send + Sync { fn add_store(&self) -> add_store_service::AddStoreServiceObj; - fn add_category(&self) -> add_category_service::AddCategoryServiceObj; + // fn add_category(&self) -> add_category_service::AddCategoryServiceObj; } #[derive(Clone, Builder)] pub struct InventoryServices { add_store: add_store_service::AddStoreServiceObj, - add_category: add_category_service::AddCategoryServiceObj, + // add_category: add_category_service::AddCategoryServiceObj, } impl InventoryServicesInterface for InventoryServices { fn add_store(&self) -> add_store_service::AddStoreServiceObj { self.add_store.clone() } - fn add_category(&self) -> add_category_service::AddCategoryServiceObj { - self.add_category.clone() - } + // fn add_category(&self) -> add_category_service::AddCategoryServiceObj { + // self.add_category.clone() + // } } diff --git a/src/inventory/domain/commands.rs b/src/inventory/domain/commands.rs new file mode 100644 index 0000000..8f6db77 --- /dev/null +++ b/src/inventory/domain/commands.rs @@ -0,0 +1,17 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use mockall::predicate::*; +use serde::{Deserialize, Serialize}; + +use super::{ + // add_category_command::AddCategoryCommand, + add_store_command::AddStoreCommand, +}; + +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] +pub enum InventoryCommand { + //AddCategory(AddCategoryCommand), + AddStore(AddStoreCommand), +} diff --git a/src/inventory/domain/events.rs b/src/inventory/domain/events.rs new file mode 100644 index 0000000..4f468f0 --- /dev/null +++ b/src/inventory/domain/events.rs @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use cqrs_es::DomainEvent; +use serde::{Deserialize, Serialize}; + +use super::{ + // category_added_event::*, + store_added_event::StoreAddedEvent, +}; + +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] +pub enum InventoryEvent { + // CategoryAdded(CategoryAddedEvent), + StoreAdded(StoreAddedEvent), +} + +//TODO: define password type that takes string and converts to hash + +impl DomainEvent for InventoryEvent { + fn event_version(&self) -> String { + "1.0".to_string() + } + + fn event_type(&self) -> String { + let e: &str = match self { + // InventoryEvent::CategoryAdded { .. } => "InventoryCategoryAdded", + InventoryEvent::StoreAdded { .. } => "InventoryStoredded", + }; + + e.to_string() + } +} diff --git a/src/inventory/domain/mod.rs b/src/inventory/domain/mod.rs index 56f60de..8d475b8 100644 --- a/src/inventory/domain/mod.rs +++ b/src/inventory/domain/mod.rs @@ -1,3 +1,19 @@ // SPDX-FileCopyrightText: 2024 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later + +// aggregates +//pub mod money_aggregate; +//pub mod product_aggregate; +//pub mod stock_aggregate; +pub mod store_aggregate; + +// commands +//pub mod add_category_command; +pub mod add_store_command; +pub mod commands; + +// events +//pub mod category_added_event; +pub mod events; +pub mod store_added_event;