diff --git a/src/ordering/domain/add_order_command.rs b/src/ordering/domain/add_order_command.rs new file mode 100644 index 0000000..72a3526 --- /dev/null +++ b/src/ordering/domain/add_order_command.rs @@ -0,0 +1,108 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use derive_builder::Builder; +use derive_getters::Getters; +use derive_more::{Display, Error}; +use serde::{Deserialize, Serialize}; +use time::OffsetDateTime; +use uuid::Uuid; + +use crate::utils::string::empty_string_err; + +#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] +pub enum AddOrderCommandError { + CustomerNameIsEmpty, +} + +#[derive( + Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder, +)] +pub struct UnvalidatedAddOrderCommand { + adding_by: Uuid, + + #[builder(default = "OffsetDateTime::now_utc()")] + created_time: OffsetDateTime, + customer_name: String, +} + +impl UnvalidatedAddOrderCommand { + pub fn validate(self) -> Result { + let customer_name = empty_string_err( + self.customer_name, + AddOrderCommandError::CustomerNameIsEmpty, + )?; + + Ok(AddOrderCommand { + created_time: self.created_time, + customer_name, + adding_by: self.adding_by, + }) + } +} + +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)] +pub struct AddOrderCommand { + created_time: OffsetDateTime, + customer_name: String, + adding_by: Uuid, +} + +#[cfg(test)] +mod tests { + use time::macros::datetime; + + use crate::utils::uuid::tests::UUID; + + use super::*; + + impl AddOrderCommand { + pub fn get_cmd() -> Self { + let customer_name = "foo"; + let adding_by = UUID; + + UnvalidatedAddOrderCommandBuilder::default() + .customer_name(customer_name.into()) + .adding_by(adding_by) + .created_time(datetime!(1970-01-01 0:00 UTC)) + .build() + .unwrap() + .validate() + .unwrap() + } + } + + #[test] + fn test_cmd() { + let customer_name = "foo"; + let adding_by = UUID; + + let cmd = UnvalidatedAddOrderCommandBuilder::default() + .customer_name(customer_name.into()) + .adding_by(adding_by) + .build() + .unwrap() + .validate() + .unwrap(); + + assert_eq!(*cmd.adding_by(), adding_by); + assert_eq!(cmd.customer_name(), customer_name); + } + + #[test] + fn test_cmd_customer_name_empty() { + let customer_name = ""; + let adding_by = UUID; + + assert_eq!( + UnvalidatedAddOrderCommandBuilder::default() + .customer_name(customer_name.into()) + .adding_by(adding_by) + .build() + .unwrap() + .validate(), + Err(AddOrderCommandError::CustomerNameIsEmpty) + ); + } +} diff --git a/src/ordering/domain/commands.rs b/src/ordering/domain/commands.rs index 6a74d43..d68ec89 100644 --- a/src/ordering/domain/commands.rs +++ b/src/ordering/domain/commands.rs @@ -6,7 +6,8 @@ use mockall::predicate::*; use serde::{Deserialize, Serialize}; use super::{ - add_line_item_command::AddLineItemCommand, delete_line_item_command::DeleteLineItemCommand, + add_line_item_command::AddLineItemCommand, add_order_command::AddOrderCommand, + delete_line_item_command::DeleteLineItemCommand, update_line_item_command::UpdateLineItemCommand, }; @@ -15,4 +16,5 @@ pub enum OrderingCommand { AddLineItem(AddLineItemCommand), UpdateLineItem(UpdateLineItemCommand), DeleteLineItem(DeleteLineItemCommand), + AddOrder(AddOrderCommand), } diff --git a/src/ordering/domain/events.rs b/src/ordering/domain/events.rs index c82ab04..0d6a6ef 100644 --- a/src/ordering/domain/events.rs +++ b/src/ordering/domain/events.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; use super::{ line_item_added_event::LineItemAddedEvent, line_item_deleted_event::LineItemDeletedEvent, - line_item_updated_event::LineItemUpdatedEvent, + line_item_updated_event::LineItemUpdatedEvent, order_added_event::OrderAddedEvent, }; #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] @@ -15,6 +15,7 @@ pub enum OrderingEvent { LineItemAdded(LineItemAddedEvent), LineItemUpdated(LineItemUpdatedEvent), LineItemDeleted(LineItemDeletedEvent), + OrderAdded(OrderAddedEvent), } impl DomainEvent for OrderingEvent { @@ -27,6 +28,7 @@ impl DomainEvent for OrderingEvent { OrderingEvent::LineItemAdded { .. } => "OrderingLineItemAdded", OrderingEvent::LineItemUpdated { .. } => "OrderingLineItemUpdated", OrderingEvent::LineItemDeleted { .. } => "OrderingLineItemDeleted", + OrderingEvent::OrderAdded { .. } => "OrderingOrderAdded", }; e.to_string() diff --git a/src/ordering/domain/mod.rs b/src/ordering/domain/mod.rs index 4758903..1a806b2 100644 --- a/src/ordering/domain/mod.rs +++ b/src/ordering/domain/mod.rs @@ -5,9 +5,11 @@ // aggregates pub mod kot_aggregate; pub mod line_item_aggregate; +pub mod order_aggregate; // commands pub mod add_line_item_command; +pub mod add_order_command; pub mod commands; pub mod delete_line_item_command; pub mod update_line_item_command; @@ -17,3 +19,4 @@ pub mod events; pub mod line_item_added_event; pub mod line_item_deleted_event; pub mod line_item_updated_event; +pub mod order_added_event; diff --git a/src/ordering/domain/order_added_event.rs b/src/ordering/domain/order_added_event.rs new file mode 100644 index 0000000..8d2be36 --- /dev/null +++ b/src/ordering/domain/order_added_event.rs @@ -0,0 +1,41 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use derive_builder::Builder; +use derive_getters::Getters; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +use super::order_aggregate::Order; + +#[derive( + Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd, +)] +pub struct OrderAddedEvent { + added_by_user: Uuid, + + order: Order, +} + +#[cfg(test)] +pub mod tests { + use crate::ordering::domain::add_order_command::AddOrderCommand; + + use super::*; + + pub fn get_added_order_event_from_command(cmd: &AddOrderCommand) -> OrderAddedEvent { + let order = Order::get_order(); + + OrderAddedEventBuilder::default() + .added_by_user(cmd.adding_by().clone()) + .order(order) + .build() + .unwrap() + } + + #[test] + fn test_event() { + get_added_order_event_from_command(&AddOrderCommand::get_cmd()); + } +}