feat: add order command and reslutant event
This commit is contained in:
parent
a47810e4e7
commit
3bb854f2ea
5 changed files with 158 additions and 2 deletions
108
src/ordering/domain/add_order_command.rs
Normal file
108
src/ordering/domain/add_order_command.rs
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// 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<AddOrderCommand, AddOrderCommandError> {
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,7 +6,8 @@ use mockall::predicate::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use super::{
|
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,
|
update_line_item_command::UpdateLineItemCommand,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -15,4 +16,5 @@ pub enum OrderingCommand {
|
||||||
AddLineItem(AddLineItemCommand),
|
AddLineItem(AddLineItemCommand),
|
||||||
UpdateLineItem(UpdateLineItemCommand),
|
UpdateLineItem(UpdateLineItemCommand),
|
||||||
DeleteLineItem(DeleteLineItemCommand),
|
DeleteLineItem(DeleteLineItemCommand),
|
||||||
|
AddOrder(AddOrderCommand),
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
line_item_added_event::LineItemAddedEvent, line_item_deleted_event::LineItemDeletedEvent,
|
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)]
|
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
|
@ -15,6 +15,7 @@ pub enum OrderingEvent {
|
||||||
LineItemAdded(LineItemAddedEvent),
|
LineItemAdded(LineItemAddedEvent),
|
||||||
LineItemUpdated(LineItemUpdatedEvent),
|
LineItemUpdated(LineItemUpdatedEvent),
|
||||||
LineItemDeleted(LineItemDeletedEvent),
|
LineItemDeleted(LineItemDeletedEvent),
|
||||||
|
OrderAdded(OrderAddedEvent),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DomainEvent for OrderingEvent {
|
impl DomainEvent for OrderingEvent {
|
||||||
|
@ -27,6 +28,7 @@ impl DomainEvent for OrderingEvent {
|
||||||
OrderingEvent::LineItemAdded { .. } => "OrderingLineItemAdded",
|
OrderingEvent::LineItemAdded { .. } => "OrderingLineItemAdded",
|
||||||
OrderingEvent::LineItemUpdated { .. } => "OrderingLineItemUpdated",
|
OrderingEvent::LineItemUpdated { .. } => "OrderingLineItemUpdated",
|
||||||
OrderingEvent::LineItemDeleted { .. } => "OrderingLineItemDeleted",
|
OrderingEvent::LineItemDeleted { .. } => "OrderingLineItemDeleted",
|
||||||
|
OrderingEvent::OrderAdded { .. } => "OrderingOrderAdded",
|
||||||
};
|
};
|
||||||
|
|
||||||
e.to_string()
|
e.to_string()
|
||||||
|
|
|
@ -5,9 +5,11 @@
|
||||||
// aggregates
|
// aggregates
|
||||||
pub mod kot_aggregate;
|
pub mod kot_aggregate;
|
||||||
pub mod line_item_aggregate;
|
pub mod line_item_aggregate;
|
||||||
|
pub mod order_aggregate;
|
||||||
|
|
||||||
// commands
|
// commands
|
||||||
pub mod add_line_item_command;
|
pub mod add_line_item_command;
|
||||||
|
pub mod add_order_command;
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
pub mod delete_line_item_command;
|
pub mod delete_line_item_command;
|
||||||
pub mod update_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_added_event;
|
||||||
pub mod line_item_deleted_event;
|
pub mod line_item_deleted_event;
|
||||||
pub mod line_item_updated_event;
|
pub mod line_item_updated_event;
|
||||||
|
pub mod order_added_event;
|
||||||
|
|
41
src/ordering/domain/order_added_event.rs
Normal file
41
src/ordering/domain/order_added_event.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// 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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue