91 lines
2.3 KiB
Rust
91 lines
2.3 KiB
Rust
// 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 uuid::Uuid;
|
|
|
|
#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
|
pub enum AddCustomizationCommandError {
|
|
NameIsEmpty,
|
|
}
|
|
|
|
#[derive(
|
|
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder,
|
|
)]
|
|
pub struct UnvalidatedAddCustomizationCommand {
|
|
name: String,
|
|
product_id: Uuid,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)]
|
|
pub struct AddCustomizationCommand {
|
|
name: String,
|
|
product_id: Uuid,
|
|
}
|
|
|
|
impl UnvalidatedAddCustomizationCommand {
|
|
pub fn validate(self) -> Result<AddCustomizationCommand, AddCustomizationCommandError> {
|
|
let name = self.name.trim().to_owned();
|
|
if name.is_empty() {
|
|
return Err(AddCustomizationCommandError::NameIsEmpty);
|
|
}
|
|
|
|
Ok(AddCustomizationCommand {
|
|
name,
|
|
product_id: self.product_id,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub mod tests {
|
|
use super::*;
|
|
|
|
use crate::utils::uuid::tests::UUID;
|
|
|
|
pub fn get_command() -> AddCustomizationCommand {
|
|
UnvalidatedAddCustomizationCommandBuilder::default()
|
|
.name("foo".into())
|
|
.product_id(UUID.clone())
|
|
.build()
|
|
.unwrap()
|
|
.validate()
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn test_cmd() {
|
|
let name = "foo";
|
|
let product_id = UUID;
|
|
|
|
let cmd = UnvalidatedAddCustomizationCommandBuilder::default()
|
|
.name(name.into())
|
|
.product_id(product_id.clone())
|
|
.build()
|
|
.unwrap()
|
|
.validate()
|
|
.unwrap();
|
|
|
|
assert_eq!(cmd.name(), name);
|
|
assert_eq!(cmd.product_id(), &product_id);
|
|
}
|
|
|
|
#[test]
|
|
fn test_cmd_name_is_empty() {
|
|
let product_id = UUID;
|
|
|
|
assert_eq!(
|
|
UnvalidatedAddCustomizationCommandBuilder::default()
|
|
.name("".into())
|
|
.product_id(product_id.clone())
|
|
.build()
|
|
.unwrap()
|
|
.validate(),
|
|
Err(AddCustomizationCommandError::NameIsEmpty)
|
|
);
|
|
}
|
|
}
|