feat: update order service #67
16 changed files with 359 additions and 85 deletions
|
@ -24,7 +24,6 @@ impl From<SqlxError> for OrderingDBError {
|
|||
return Self::DuplicateLineItemID;
|
||||
} else if msg.contains("cqrs_ordering_store_query_order_id_key") {
|
||||
return Self::DuplicateOrderID;
|
||||
|
||||
} else {
|
||||
println!("{msg}");
|
||||
}
|
||||
|
|
|
@ -10,8 +10,8 @@ use crate::db::{migrate::RunMigrations, sqlx_postgres::Postgres};
|
|||
|
||||
mod errors;
|
||||
mod line_item_id_exists;
|
||||
mod order_id_exists;
|
||||
mod line_item_view;
|
||||
mod order_id_exists;
|
||||
mod order_view;
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
|
@ -71,18 +71,12 @@ pub mod tests {
|
|||
let order = Order::default();
|
||||
|
||||
// state doesn't exist
|
||||
assert!(!db
|
||||
.order_id_exists(order.order_id())
|
||||
.await
|
||||
.unwrap());
|
||||
assert!(!db.order_id_exists(order.order_id()).await.unwrap());
|
||||
|
||||
create_dummy_order(&order, &db).await;
|
||||
|
||||
// state exists
|
||||
assert!(db
|
||||
.order_id_exists(order.order_id())
|
||||
.await
|
||||
.unwrap());
|
||||
assert!(db.order_id_exists(order.order_id()).await.unwrap());
|
||||
|
||||
settings.drop_db().await;
|
||||
}
|
||||
|
|
|
@ -43,7 +43,6 @@ impl Default for OrderView {
|
|||
|
||||
impl From<OrderView> for Order {
|
||||
fn from(v: OrderView) -> Self {
|
||||
|
||||
OrderBuilder::default()
|
||||
.customer_name(v.customer_name)
|
||||
.order_id(v.order_id)
|
||||
|
@ -212,12 +211,7 @@ impl Query<Order> for OrderingDBPostgresAdapter {
|
|||
let res = self
|
||||
.load_with_context(order_id)
|
||||
.await
|
||||
.unwrap_or_else(|_| {
|
||||
Some((
|
||||
OrderView::default(),
|
||||
ViewContext::new(order_id.into(), 0),
|
||||
))
|
||||
});
|
||||
.unwrap_or_else(|_| Some((OrderView::default(), ViewContext::new(order_id.into(), 0))));
|
||||
let (mut view, view_context): (OrderView, ViewContext) = res.unwrap();
|
||||
for event in events {
|
||||
view.update(event);
|
||||
|
|
|
@ -14,5 +14,4 @@ pub enum OrderingDBError {
|
|||
InternalError,
|
||||
DuplicateOrderID,
|
||||
OrderIDNotFound,
|
||||
|
||||
}
|
||||
|
|
|
@ -25,9 +25,7 @@ pub mod tests {
|
|||
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn mock_order_id_exists_db_port_false(
|
||||
times: Option<usize>,
|
||||
) -> OrderIDExistsDBPortObj {
|
||||
pub fn mock_order_id_exists_db_port_false(times: Option<usize>) -> OrderIDExistsDBPortObj {
|
||||
let mut m = MockOrderIDExistsDBPort::new();
|
||||
if let Some(times) = times {
|
||||
m.expect_order_id_exists()
|
||||
|
@ -40,9 +38,7 @@ pub mod tests {
|
|||
Arc::new(m)
|
||||
}
|
||||
|
||||
pub fn mock_order_id_exists_db_port_true(
|
||||
times: Option<usize>,
|
||||
) -> OrderIDExistsDBPortObj {
|
||||
pub fn mock_order_id_exists_db_port_true(times: Option<usize>) -> OrderIDExistsDBPortObj {
|
||||
let mut m = MockOrderIDExistsDBPort::new();
|
||||
if let Some(times) = times {
|
||||
m.expect_order_id_exists()
|
||||
|
|
|
@ -36,11 +36,7 @@ impl AddOrderUseCase for AddOrderService {
|
|||
let mut order_id = self.get_uuid.get_uuid();
|
||||
|
||||
loop {
|
||||
if self
|
||||
.db_order_id_exists
|
||||
.order_id_exists(&order_id)
|
||||
.await?
|
||||
{
|
||||
if self.db_order_id_exists.order_id_exists(&order_id).await? {
|
||||
order_id = self.get_uuid.get_uuid();
|
||||
continue;
|
||||
} else {
|
||||
|
|
|
@ -10,9 +10,10 @@ pub mod errors;
|
|||
|
||||
//services
|
||||
pub mod add_line_item_service;
|
||||
pub mod add_order_service;
|
||||
pub mod delete_line_item_service;
|
||||
pub mod update_line_item_service;
|
||||
pub mod add_order_service;
|
||||
pub mod update_order_service;
|
||||
|
||||
#[automock]
|
||||
pub trait OrderingServicesInterface: Send + Sync {
|
||||
|
@ -20,6 +21,7 @@ pub trait OrderingServicesInterface: Send + Sync {
|
|||
fn update_line_item(&self) -> update_line_item_service::UpdateLineItemServiceObj;
|
||||
fn delete_line_item(&self) -> delete_line_item_service::DeleteLineItemServiceObj;
|
||||
fn add_order(&self) -> add_order_service::AddOrderServiceObj;
|
||||
fn update_order(&self) -> update_order_service::UpdateOrderServiceObj;
|
||||
}
|
||||
|
||||
#[derive(Clone, Builder)]
|
||||
|
@ -28,6 +30,7 @@ pub struct OrderingServices {
|
|||
update_line_item: update_line_item_service::UpdateLineItemServiceObj,
|
||||
delete_line_item: delete_line_item_service::DeleteLineItemServiceObj,
|
||||
add_order: add_order_service::AddOrderServiceObj,
|
||||
update_order: update_order_service::UpdateOrderServiceObj,
|
||||
}
|
||||
|
||||
impl OrderingServicesInterface for OrderingServices {
|
||||
|
@ -45,4 +48,8 @@ impl OrderingServicesInterface for OrderingServices {
|
|||
fn add_order(&self) -> add_order_service::AddOrderServiceObj {
|
||||
self.add_order.clone()
|
||||
}
|
||||
|
||||
fn update_order(&self) -> update_order_service::UpdateOrderServiceObj {
|
||||
self.update_order.clone()
|
||||
}
|
||||
}
|
||||
|
|
119
src/ordering/application/services/update_order_service.rs
Normal file
119
src/ordering/application/services/update_order_service.rs
Normal file
|
@ -0,0 +1,119 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use derive_builder::Builder;
|
||||
use mockall::predicate::*;
|
||||
use mockall::*;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use super::errors::*;
|
||||
use crate::ordering::{
|
||||
application::port::output::db::order_id_exists::*,
|
||||
domain::{order_aggregate::*, order_updated_event::*, update_order_command::*},
|
||||
};
|
||||
use crate::utils::uuid::*;
|
||||
|
||||
#[automock]
|
||||
#[async_trait::async_trait]
|
||||
pub trait UpdateOrderUseCase: Send + Sync {
|
||||
async fn update_order(&self, cmd: UpdateOrderCommand) -> OrderingResult<OrderUpdatedEvent>;
|
||||
}
|
||||
|
||||
pub type UpdateOrderServiceObj = Arc<dyn UpdateOrderUseCase>;
|
||||
|
||||
#[derive(Clone, Builder)]
|
||||
pub struct UpdateOrderService {
|
||||
db_order_id_exists: OrderIDExistsDBPortObj,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl UpdateOrderUseCase for UpdateOrderService {
|
||||
async fn update_order(&self, cmd: UpdateOrderCommand) -> OrderingResult<OrderUpdatedEvent> {
|
||||
if !self
|
||||
.db_order_id_exists
|
||||
.order_id_exists(cmd.old_order().order_id())
|
||||
.await?
|
||||
{
|
||||
return Err(OrderingError::OrderIDNotFound);
|
||||
}
|
||||
|
||||
let new_order = OrderBuilder::default()
|
||||
.created_time(cmd.created_time().clone())
|
||||
.customer_name(cmd.customer_name().into())
|
||||
.order_id(*cmd.old_order().order_id())
|
||||
.deleted(false)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
Ok(OrderUpdatedEventBuilder::default()
|
||||
.added_by_user(*cmd.adding_by())
|
||||
.new_order(new_order)
|
||||
.old_order(cmd.old_order().clone())
|
||||
.build()
|
||||
.unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
|
||||
use crate::ordering::domain::order_updated_event::tests::get_updated_order_event_from_command;
|
||||
use crate::utils::uuid::tests::UUID;
|
||||
use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid};
|
||||
|
||||
pub fn mock_update_order_service(
|
||||
times: Option<usize>,
|
||||
cmd: UpdateOrderCommand,
|
||||
) -> UpdateOrderServiceObj {
|
||||
let mut m = MockUpdateOrderUseCase::new();
|
||||
|
||||
let res = get_updated_order_event_from_command(&cmd);
|
||||
if let Some(times) = times {
|
||||
m.expect_update_order()
|
||||
.times(times)
|
||||
.returning(move |_| Ok(res.clone()));
|
||||
} else {
|
||||
m.expect_update_order().returning(move |_| Ok(res.clone()));
|
||||
}
|
||||
|
||||
Arc::new(m)
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_service() {
|
||||
let cmd = UpdateOrderCommand::get_cmd();
|
||||
|
||||
let s = UpdateOrderServiceBuilder::default()
|
||||
.db_order_id_exists(mock_order_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let res = s.update_order(cmd.clone()).await.unwrap();
|
||||
assert_eq!(res.new_order().customer_name(), cmd.customer_name());
|
||||
assert_eq!(res.new_order().order_id(), cmd.old_order().order_id());
|
||||
assert!(!res.new_order().deleted());
|
||||
|
||||
assert_eq!(res.old_order(), cmd.old_order());
|
||||
|
||||
assert_eq!(res.added_by_user(), cmd.adding_by());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_service_order_id_doesnt_exist() {
|
||||
let cmd = UpdateOrderCommand::get_cmd();
|
||||
|
||||
let s = UpdateOrderServiceBuilder::default()
|
||||
.db_order_id_exists(mock_order_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
s.update_order(cmd.clone()).await,
|
||||
Err(OrderingError::OrderIDNotFound)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
|
|||
use super::{
|
||||
add_line_item_command::AddLineItemCommand, add_order_command::AddOrderCommand,
|
||||
delete_line_item_command::DeleteLineItemCommand,
|
||||
update_line_item_command::UpdateLineItemCommand,
|
||||
update_line_item_command::UpdateLineItemCommand, update_order_command::UpdateOrderCommand,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
|
||||
|
@ -17,4 +17,5 @@ pub enum OrderingCommand {
|
|||
UpdateLineItem(UpdateLineItemCommand),
|
||||
DeleteLineItem(DeleteLineItemCommand),
|
||||
AddOrder(AddOrderCommand),
|
||||
UpdateOrder(UpdateOrderCommand),
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
|
|||
use super::{
|
||||
line_item_added_event::LineItemAddedEvent, line_item_deleted_event::LineItemDeletedEvent,
|
||||
line_item_updated_event::LineItemUpdatedEvent, order_added_event::OrderAddedEvent,
|
||||
order_updated_event::OrderUpdatedEvent,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
|
||||
|
@ -16,6 +17,7 @@ pub enum OrderingEvent {
|
|||
LineItemUpdated(LineItemUpdatedEvent),
|
||||
LineItemDeleted(LineItemDeletedEvent),
|
||||
OrderAdded(OrderAddedEvent),
|
||||
OrderUpdated(OrderUpdatedEvent),
|
||||
}
|
||||
|
||||
impl DomainEvent for OrderingEvent {
|
||||
|
@ -29,6 +31,7 @@ impl DomainEvent for OrderingEvent {
|
|||
OrderingEvent::LineItemUpdated { .. } => "OrderingLineItemUpdated",
|
||||
OrderingEvent::LineItemDeleted { .. } => "OrderingLineItemDeleted",
|
||||
OrderingEvent::OrderAdded { .. } => "OrderingOrderAdded",
|
||||
OrderingEvent::OrderUpdated { .. } => "OrderingOrderUpdated",
|
||||
};
|
||||
|
||||
e.to_string()
|
||||
|
|
|
@ -25,6 +25,7 @@ pub struct LineItem {
|
|||
product_name: String,
|
||||
product_id: Uuid,
|
||||
line_item_id: Uuid,
|
||||
// TODO: add kot_id
|
||||
quantity: Quantity,
|
||||
#[builder(default = "false")]
|
||||
deleted: bool,
|
||||
|
|
|
@ -13,6 +13,7 @@ pub mod add_order_command;
|
|||
pub mod commands;
|
||||
pub mod delete_line_item_command;
|
||||
pub mod update_line_item_command;
|
||||
pub mod update_order_command;
|
||||
|
||||
// events
|
||||
pub mod events;
|
||||
|
@ -20,3 +21,4 @@ pub mod line_item_added_event;
|
|||
pub mod line_item_deleted_event;
|
||||
pub mod line_item_updated_event;
|
||||
pub mod order_added_event;
|
||||
pub mod order_updated_event;
|
||||
|
|
|
@ -15,7 +15,6 @@ use crate::ordering::{
|
|||
domain::{commands::*, events::*},
|
||||
};
|
||||
|
||||
|
||||
#[derive(
|
||||
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Builder, Getters,
|
||||
)]
|
||||
|
@ -60,7 +59,6 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[async_trait]
|
||||
impl Aggregate for Order {
|
||||
type Command = OrderingCommand;
|
||||
|
@ -85,6 +83,10 @@ impl Aggregate for Order {
|
|||
let res = services.add_order().add_order(cmd).await?;
|
||||
Ok(vec![OrderingEvent::OrderAdded(res)])
|
||||
}
|
||||
OrderingCommand::UpdateOrder(cmd) => {
|
||||
let res = services.update_order().update_order(cmd).await?;
|
||||
Ok(vec![OrderingEvent::OrderUpdated(res)])
|
||||
}
|
||||
_ => Ok(Vec::default()),
|
||||
}
|
||||
}
|
||||
|
@ -92,8 +94,8 @@ impl Aggregate for Order {
|
|||
fn apply(&mut self, event: Self::Event) {
|
||||
match event {
|
||||
OrderingEvent::OrderAdded(e) => *self = e.order().clone(),
|
||||
// OrderingEvent::OrderUpdated(e) => *self = e.new_order().clone(),
|
||||
// OrderingEvent::OrderDeleted(e) => *self = e.order().clone(),
|
||||
OrderingEvent::OrderUpdated(e) => *self = e.new_order().clone(),
|
||||
// OrderingEvent::OrderDeleted(e) => *self = e.order().clone(),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
@ -105,20 +107,19 @@ mod aggregate_tests {
|
|||
|
||||
use add_order_service::tests::mock_add_order_service;
|
||||
use cqrs_es::test::TestFramework;
|
||||
// use delete_order_service::tests::mock_delete_order_service;
|
||||
// use update_order_service::tests::mock_update_order_service;
|
||||
// use delete_order_service::tests::mock_delete_order_service;
|
||||
use update_order_service::tests::mock_update_order_service;
|
||||
|
||||
use super::*;
|
||||
|
||||
// use crate::ordering::domain::delete_order_command::DeleteOrderCommand;
|
||||
// use crate::ordering::domain::order_deleted_event::tests::get_deleted_order_event_from_command;
|
||||
// use crate::ordering::domain::order_updated_event::tests::get_updated_order_event_from_command;
|
||||
// use crate::ordering::domain::update_order_command::UpdateOrderCommand;
|
||||
// use crate::ordering::domain::delete_order_command::DeleteOrderCommand;
|
||||
// use crate::ordering::domain::order_deleted_event::tests::get_deleted_order_event_from_command;
|
||||
use crate::ordering::domain::order_updated_event::tests::get_updated_order_event_from_command;
|
||||
use crate::ordering::domain::update_order_command::UpdateOrderCommand;
|
||||
use crate::tests::bdd::*;
|
||||
|
||||
use crate::ordering::domain::{
|
||||
add_order_command::*,
|
||||
order_added_event::tests::get_added_order_event_from_command,
|
||||
add_order_command::*, order_added_event::tests::get_added_order_event_from_command,
|
||||
};
|
||||
|
||||
type OrderTestFramework = TestFramework<Order>;
|
||||
|
@ -140,46 +141,43 @@ mod aggregate_tests {
|
|||
.when(OrderingCommand::AddOrder(cmd))
|
||||
.then_expect_events(vec![expected]);
|
||||
}
|
||||
//
|
||||
// #[test]
|
||||
// fn test_update_order() {
|
||||
// let cmd = UpdateOrderCommand::get_cmd();
|
||||
// let expected = get_updated_order_event_from_command(&cmd);
|
||||
// let expected = OrderingEvent::OrderUpdated(expected);
|
||||
//
|
||||
// let mut services = MockOrderingServicesInterface::new();
|
||||
// services
|
||||
// .expect_update_order()
|
||||
// .times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||
// .return_const(mock_update_order_service(
|
||||
// IS_CALLED_ONLY_ONCE,
|
||||
// cmd.clone(),
|
||||
// ));
|
||||
//
|
||||
// OrderTestFramework::with(Arc::new(services))
|
||||
// .given_no_previous_events()
|
||||
// .when(OrderingCommand::UpdateOrder(cmd))
|
||||
// .then_expect_events(vec![expected]);
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn test_delete_order() {
|
||||
// let cmd = DeleteOrderCommand::get_cmd();
|
||||
// let expected = get_deleted_order_event_from_command(&cmd);
|
||||
// let expected = OrderingEvent::OrderDeleted(expected);
|
||||
//
|
||||
// let mut services = MockOrderingServicesInterface::new();
|
||||
// services
|
||||
// .expect_delete_order()
|
||||
// .times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||
// .return_const(mock_delete_order_service(
|
||||
// IS_CALLED_ONLY_ONCE,
|
||||
// cmd.clone(),
|
||||
// ));
|
||||
//
|
||||
// OrderTestFramework::with(Arc::new(services))
|
||||
// .given_no_previous_events()
|
||||
// .when(OrderingCommand::DeleteOrder(cmd))
|
||||
// .then_expect_events(vec![expected]);
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn test_update_order() {
|
||||
let cmd = UpdateOrderCommand::get_cmd();
|
||||
let expected = get_updated_order_event_from_command(&cmd);
|
||||
let expected = OrderingEvent::OrderUpdated(expected);
|
||||
|
||||
let mut services = MockOrderingServicesInterface::new();
|
||||
services
|
||||
.expect_update_order()
|
||||
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||
.return_const(mock_update_order_service(IS_CALLED_ONLY_ONCE, cmd.clone()));
|
||||
|
||||
OrderTestFramework::with(Arc::new(services))
|
||||
.given_no_previous_events()
|
||||
.when(OrderingCommand::UpdateOrder(cmd))
|
||||
.then_expect_events(vec![expected]);
|
||||
}
|
||||
//
|
||||
// #[test]
|
||||
// fn test_delete_order() {
|
||||
// let cmd = DeleteOrderCommand::get_cmd();
|
||||
// let expected = get_deleted_order_event_from_command(&cmd);
|
||||
// let expected = OrderingEvent::OrderDeleted(expected);
|
||||
//
|
||||
// let mut services = MockOrderingServicesInterface::new();
|
||||
// services
|
||||
// .expect_delete_order()
|
||||
// .times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||
// .return_const(mock_delete_order_service(
|
||||
// IS_CALLED_ONLY_ONCE,
|
||||
// cmd.clone(),
|
||||
// ));
|
||||
//
|
||||
// OrderTestFramework::with(Arc::new(services))
|
||||
// .given_no_previous_events()
|
||||
// .when(OrderingCommand::DeleteOrder(cmd))
|
||||
// .then_expect_events(vec![expected]);
|
||||
// }
|
||||
}
|
||||
|
|
48
src/ordering/domain/order_updated_event.rs
Normal file
48
src/ordering/domain/order_updated_event.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
// 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::*;
|
||||
|
||||
#[derive(
|
||||
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
|
||||
)]
|
||||
pub struct OrderUpdatedEvent {
|
||||
added_by_user: Uuid,
|
||||
|
||||
new_order: Order,
|
||||
old_order: Order,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use crate::ordering::domain::update_order_command::UpdateOrderCommand;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub fn get_updated_order_event_from_command(cmd: &UpdateOrderCommand) -> OrderUpdatedEvent {
|
||||
let new_order = OrderBuilder::default()
|
||||
.created_time(cmd.created_time().clone())
|
||||
.customer_name(cmd.customer_name().into())
|
||||
.order_id(*cmd.old_order().order_id())
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
OrderUpdatedEventBuilder::default()
|
||||
.added_by_user(cmd.adding_by().clone())
|
||||
.new_order(new_order)
|
||||
.old_order(cmd.old_order().clone())
|
||||
.build()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_event() {
|
||||
get_updated_order_event_from_command(&UpdateOrderCommand::get_cmd());
|
||||
}
|
||||
}
|
117
src/ordering/domain/update_order_command.rs
Normal file
117
src/ordering/domain/update_order_command.rs
Normal file
|
@ -0,0 +1,117 @@
|
|||
// 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 super::order_aggregate::*;
|
||||
use crate::utils::string::empty_string_err;
|
||||
|
||||
#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum UpdateOrderCommandError {
|
||||
CustomerNameIsEmpty,
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder,
|
||||
)]
|
||||
pub struct UnvalidatedUpdateOrderCommand {
|
||||
adding_by: Uuid,
|
||||
|
||||
#[builder(default = "OffsetDateTime::now_utc()")]
|
||||
created_time: OffsetDateTime,
|
||||
customer_name: String,
|
||||
old_order: Order,
|
||||
}
|
||||
|
||||
impl UnvalidatedUpdateOrderCommand {
|
||||
pub fn validate(self) -> Result<UpdateOrderCommand, UpdateOrderCommandError> {
|
||||
let customer_name = empty_string_err(
|
||||
self.customer_name,
|
||||
UpdateOrderCommandError::CustomerNameIsEmpty,
|
||||
)?;
|
||||
|
||||
Ok(UpdateOrderCommand {
|
||||
created_time: self.created_time,
|
||||
customer_name,
|
||||
adding_by: self.adding_by,
|
||||
old_order: self.old_order,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)]
|
||||
pub struct UpdateOrderCommand {
|
||||
created_time: OffsetDateTime,
|
||||
customer_name: String,
|
||||
adding_by: Uuid,
|
||||
old_order: Order,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use time::macros::datetime;
|
||||
|
||||
use crate::utils::uuid::tests::UUID;
|
||||
|
||||
use super::*;
|
||||
|
||||
impl UpdateOrderCommand {
|
||||
pub fn get_cmd() -> Self {
|
||||
let customer_name = "foo-new-name";
|
||||
let adding_by = UUID;
|
||||
|
||||
UnvalidatedUpdateOrderCommandBuilder::default()
|
||||
.customer_name(customer_name.into())
|
||||
.adding_by(adding_by)
|
||||
.created_time(datetime!(1970-01-01 0:00 UTC))
|
||||
.old_order(Order::default())
|
||||
.build()
|
||||
.unwrap()
|
||||
.validate()
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd() {
|
||||
let customer_name = "foo";
|
||||
let adding_by = UUID;
|
||||
let old_order = Order::default();
|
||||
|
||||
let cmd = UnvalidatedUpdateOrderCommandBuilder::default()
|
||||
.customer_name(customer_name.into())
|
||||
.adding_by(adding_by)
|
||||
.old_order(old_order.clone())
|
||||
.build()
|
||||
.unwrap()
|
||||
.validate()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(*cmd.adding_by(), adding_by);
|
||||
assert_eq!(cmd.customer_name(), customer_name);
|
||||
assert_eq!(cmd.old_order(), &old_order);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd_customer_name_empty() {
|
||||
let customer_name = "";
|
||||
let adding_by = UUID;
|
||||
|
||||
assert_eq!(
|
||||
UnvalidatedUpdateOrderCommandBuilder::default()
|
||||
.customer_name(customer_name.into())
|
||||
.adding_by(adding_by)
|
||||
.old_order(Order::default())
|
||||
.build()
|
||||
.unwrap()
|
||||
.validate(),
|
||||
Err(UpdateOrderCommandError::CustomerNameIsEmpty)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue