-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5deb19c
commit ce35d13
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod breach_updater; | ||
pub mod update_breach_command; | ||
pub mod update_breach_command_handler; |
133 changes: 133 additions & 0 deletions
133
libs/cti/src/breaches/application/update_one/update_breach_command_handler.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
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::{ | ||
breaches::{ | ||
application::breach_command_response::BreachCommandResponse, | ||
domain::{ | ||
entities::{ | ||
breach_id::BreachId, breach_product::BreachProduct, | ||
breach_product_type::BreachProductType, | ||
breach_product_version::BreachProductVersion, breach_vendor::BreachVendor, | ||
}, | ||
repositories::breach_repository::BreachRepository, | ||
}, | ||
}, | ||
cves::domain::entities::{ | ||
cve_assigner_id::CveAssignerId, cve_assigner_name::CveAssignerName, | ||
cve_description::CveDescription, cve_id::CveId, cve_publication_date::CvePublicationDate, | ||
cve_state::CveState, cve_updated_date::CveUpdatedDate, | ||
}, | ||
}; | ||
|
||
use super::{breach_updater::BreachUpdater, update_breach_command::UpdateBreachCommand}; | ||
|
||
pub struct UpdateBreachCommandHandler<R: BreachRepository, E: EventBus> { | ||
updater: BreachUpdater<R, E>, | ||
} | ||
|
||
impl<R: BreachRepository, E: EventBus> UpdateBreachCommandHandler<R, E> { | ||
pub fn new(updater: BreachUpdater<R, E>) -> UpdateBreachCommandHandler<R, E> { | ||
UpdateBreachCommandHandler { updater } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<R: BreachRepository, E: EventBus> CommandHandler for UpdateBreachCommandHandler<R, E> { | ||
async fn handle(&self, command: Box<dyn Command>) -> Box<dyn CommandBusResponse> { | ||
let command = command | ||
.as_any() | ||
.downcast_ref::<UpdateBreachCommand>() | ||
.unwrap(); | ||
|
||
let id = match BreachId::from_optional(&command.id) { | ||
Ok(id) => id, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
let vendor = match BreachVendor::from_optional(&command.vendor) { | ||
Ok(vendor) => vendor, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
let product = match BreachProduct::from_optional(&command.product) { | ||
Ok(product) => product, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
let product_version = match BreachProductVersion::from_optional(&command.product_version) { | ||
Ok(product_version) => product_version, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
let product_type = match BreachProductType::from_optional(&command.product_type) { | ||
Ok(id) => id, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
let cve_id = match CveId::from_optional(&command.cve_id) { | ||
Ok(id) => id, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
let cve_state = match CveState::from_optional(&command.cve_state) { | ||
Ok(state) => state, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
let cve_description = match CveDescription::new(&command.cve_description) { | ||
Ok(description) => description, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
let cve_assigner_id = match CveAssignerId::from_optional(&command.cve_assigner_id) { | ||
Ok(assigner_id) => assigner_id, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
let cve_assigner_name = match CveAssignerName::from_optional(&command.cve_assigner_name) { | ||
Ok(assigner_name) => assigner_name, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
let cve_publication_date = | ||
match CvePublicationDate::from_optional(&command.cve_date_published) { | ||
Ok(publication_date) => publication_date, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
let cve_updated_date = match CveUpdatedDate::from_optional(&command.cve_date_updated) { | ||
Ok(updated_date) => updated_date, | ||
Err(err) => return BreachCommandResponse::boxed_err(err), | ||
}; | ||
|
||
match self | ||
.updater | ||
.run( | ||
id, | ||
vendor, | ||
product, | ||
product_version, | ||
product_type, | ||
cve_id, | ||
cve_state, | ||
cve_description, | ||
cve_assigner_id, | ||
cve_assigner_name, | ||
cve_publication_date, | ||
cve_updated_date, | ||
) | ||
.await | ||
{ | ||
Ok(_) => BreachCommandResponse::boxed_ok(), | ||
Err(err) => BreachCommandResponse::boxed_err(err), | ||
} | ||
} | ||
|
||
fn subscribet_to(&self) -> String { | ||
return UpdateBreachCommand::COMMAND_TYPE.to_string(); | ||
} | ||
} |