Skip to content

Commit

Permalink
add type conversioon
Browse files Browse the repository at this point in the history
  • Loading branch information
n1nj4t4nuk1 committed Dec 3, 2024
1 parent 26c904a commit ce95355
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::sync::Arc;

use async_trait::async_trait;
use cqrs::domain::{command::Command, command_bus_response::CommandBusResponse, command_handler::CommandHandler};
use events::domain::event_bus::EventBus;

use crate::cves::{application::cve_command_response::CveCommandResponse, domain::{entities::cve_id::CveId, repositories::cve_repository::CveRepository}};

use super::{create_cve_command::CreateCveCommand, cve_creator::CveCreator};


pub struct CreateCveCommandHandler<R: CveRepository, E: EventBus> {
creator: Arc<CveCreator<R, E>>,
}

impl<R: CveRepository, E: EventBus> CreateCveCommandHandler<R, E> {
pub fn new(creator: Arc<CveCreator<R, E>>) -> CreateCveCommandHandler<R, E> {
CreateCveCommandHandler { creator }
}
}

#[async_trait]
impl <R: CveRepository, E: EventBus> CommandHandler for CreateCveCommandHandler<R, E> {
async fn handle(&self, command: Box<dyn Command>) -> Box<dyn CommandBusResponse> {
let command = command.as_any().downcast_ref::<CreateCveCommand>().unwrap();

let id = match CveId::from_optional(&command.id) {
Ok(id) => id,
Err(err) => return CveCommandResponse::boxed_err(err)
};


// let res = self.creator.run(id).await;

/*match res {
Ok(_) => CveCommandResponse::boxed_ok(),
Err(err) => CryptoKeyCommandResponse::boxed_err(err)
}*/
unimplemented!()
}

fn subscribet_to(&self) -> String {
return CreateCveCommand::COMMAND_TYPE.to_string();
}
}


2 changes: 1 addition & 1 deletion libs/cti/src/cves/application/create_one/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod create_cve_command;

pub mod create_cve_command_handler;
pub mod cve_creator;
18 changes: 11 additions & 7 deletions libs/cti/src/cves/domain/entities/cve_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ pub struct CveId {
}

impl CveId {
pub fn new(id: Option<String>) -> Result<Self, DomainError> {
if id.is_none() {
return Err(DomainError::ValueObjectError { value: "CVE id must not be empty".to_string() })
}
let id = id.unwrap();
pub fn new(id: &String) -> Result<Self, DomainError> {
if id.contains(" ") {
return Err(DomainError::ValueObjectError { value: "CVE id must not contain blank spaces".to_string() })
}
Ok(Self { value: id })
Ok(Self { value: id.clone() })
}

pub fn from_optional(id: &Option<String>) -> Result<Self, DomainError> {
if id.is_none() {
return Err(DomainError::ValueObjectError { value: "CVE id must not be empty".to_string() })
}
let id = id.as_ref().unwrap();
Self::new(&id)
}

pub fn value(&self) -> String {
Expand All @@ -29,7 +33,7 @@ impl CveId {

impl Clone for CveId {
fn clone(&self) -> Self {
Self::new(Some(self.value.clone())).unwrap()
Self::new(&self.value).unwrap()
}
}

Expand Down

0 comments on commit ce95355

Please sign in to comment.