Skip to content

Commit

Permalink
add cve creator
Browse files Browse the repository at this point in the history
  • Loading branch information
n1nj4t4nuk1 committed Dec 3, 2024
1 parent 0caeac1 commit 34d2bc8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
33 changes: 33 additions & 0 deletions libs/cti/src/cves/application/create_one/create_cve_command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use cqrs::domain::command::Command;


pub struct CreateCveCommand {
pub id: Option<String>,
pub state: Option<String>,
pub date_published: Option<String>,
pub description: Option<String>,
}

impl CreateCveCommand {
pub const COMMAND_TYPE: &'static str = "[email protected]";

pub fn new(id: Option<String>, state: Option<String>, date_published: Option<String>, description: Option<String>) -> CreateCveCommand {

CreateCveCommand {
id,
state,
date_published,
description,
}
}
}

impl Command for CreateCveCommand {
fn command_type(&self) -> String {
CreateCveCommand::COMMAND_TYPE.to_string()
}

fn as_any(&self) -> &dyn std::any::Any {
self
}
}
39 changes: 39 additions & 0 deletions libs/cti/src/cves/application/create_one/cve_creator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::sync::Arc;

use events::domain::event_bus::EventBus;
use tracing::debug;

use crate::{cves::domain::{entities::{cve::Cve, cve_description::CveDescription, cve_id::CveId, cve_publication_date::CvePublicationDate, cve_state::CveState}, events::cve_created_event::CveCreatedEvent, repositories::cve_repository::CveRepository}, shared::domain::errors::DomainError};



pub struct CryptoKeyCreator<R: CveRepository, E: EventBus> {
repository: Arc<R>,
event_bus: Arc<E>,
}

impl<R: CveRepository, E: EventBus> CryptoKeyCreator<R, E> {
pub fn new(cve_repository: Arc<R>, event_bus: Arc<E>) -> CryptoKeyCreator<R, E> {
CryptoKeyCreator { repository: cve_repository, event_bus }
}

pub async fn run(
&self,
id: CveId,
state: CveState,
date_published: CvePublicationDate,
description: CveDescription,
) -> Result<(), DomainError> {
debug!("Starting CVE creation");
let key = Cve::new(id.clone(), state.clone(), date_published.clone(), description.clone());
let res = self.repository.create_one(&key).await;
if res.is_err() {
debug!("Error creating CVE with id: {}", id.value());
return Err(res.err().unwrap());
}
let created_event = CveCreatedEvent::new_shared(id.clone(), state, date_published, description);
self.event_bus.publish(created_event).await;
debug!("CVE with id: {} created", id.value());
Ok(())
}
}
3 changes: 3 additions & 0 deletions libs/cti/src/cves/application/create_one/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod create_cve_command;

pub mod cve_creator;
1 change: 1 addition & 0 deletions libs/shared/cqrs/src/domain/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

pub trait Command {
fn command_type(&self) -> String;
fn as_any (&self) -> &dyn std::any::Any;
}

0 comments on commit 34d2bc8

Please sign in to comment.