-
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
3432703
commit 15596c6
Showing
6 changed files
with
147 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use cqrs::domain::query_bus_response::QueryBusResponse; | ||
|
||
use crate::{cves::domain::entities::cve::Cve, shared::domain::errors::DomainError}; | ||
|
||
|
||
pub struct CveQueryResponse { | ||
pub error: Option<DomainError>, | ||
pub cve: Option<Cve>, | ||
} | ||
|
||
impl CveQueryResponse { | ||
pub const RES_TYPE: &'static str = "CveQueryResponse"; | ||
|
||
pub fn ok(cve: Cve) -> CveQueryResponse { | ||
CveQueryResponse { error: None, cve: Some(cve) } | ||
} | ||
|
||
pub fn boxed_ok(cve: Cve) -> Box<CveQueryResponse> { | ||
let res = CveQueryResponse::ok(cve); | ||
Box::new(res) | ||
} | ||
|
||
pub fn err(error: DomainError) -> CveQueryResponse { | ||
CveQueryResponse { error: Some(error), cve: None } | ||
} | ||
|
||
pub fn boxed_err(error: DomainError) -> Box<CveQueryResponse> { | ||
let res = CveQueryResponse::err(error); | ||
Box::new(res) | ||
} | ||
|
||
pub fn is_err(&self) -> bool { | ||
self.error.is_some() | ||
} | ||
|
||
pub fn is_ok(&self) -> bool { | ||
self.error.is_none() | ||
} | ||
} | ||
|
||
impl QueryBusResponse for CveQueryResponse { | ||
fn response_type(&self) -> String { | ||
Self::RES_TYPE.to_string() | ||
} | ||
|
||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
} |
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,25 @@ | ||
use tracing::debug; | ||
|
||
use crate::{cves::domain::{entities::{cve::Cve, cve_id::CveId}, repositories::cve_repository::CveRepository}, shared::domain::errors::DomainError}; | ||
use std::sync::Arc; | ||
|
||
pub struct CveFinder<R: CveRepository> { | ||
repository: Arc<R>, | ||
} | ||
|
||
impl<R: CveRepository> CveFinder<R> { | ||
pub fn new(cve_repository: Arc<R>) -> CveFinder<R> { | ||
CveFinder { repository: cve_repository } | ||
} | ||
|
||
pub async fn run(&self, id: CveId) -> Result<Cve, DomainError> { | ||
debug!("Finding CVE with id: {}.", id); | ||
let res = self.repository.find_by_id(&id).await; | ||
if res.is_err() { | ||
debug!("Error finding CVE with id: {}.", id); | ||
return Err(res.err().unwrap()); | ||
} | ||
debug!("CVE with id: {} found.", id); | ||
Ok(res.unwrap()) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
libs/cti/src/cves/application/find_one/find_cve_q_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,42 @@ | ||
use std::ops::Deref; | ||
|
||
use async_trait::async_trait; | ||
use cqrs::domain::{query::Query, query_bus_response::QueryBusResponse, query_handler::QueryHandler}; | ||
|
||
use crate::cves::{application::cve_query_response::CveQueryResponse, domain::{entities::cve_id::CveId, repositories::cve_repository::CveRepository}}; | ||
|
||
use super::{cve_finder::CveFinder, find_cve_query::FindCveQuery}; | ||
|
||
|
||
pub struct FindCveQueryHandler<R: CveRepository> { | ||
crypto_keys_by_user_finder: CveFinder<R>, | ||
} | ||
|
||
impl<R: CveRepository> FindCveQueryHandler<R> { | ||
pub fn new(crypto_keys_by_user_finder: CveFinder<R>) -> FindCveQueryHandler<R> { | ||
FindCveQueryHandler { crypto_keys_by_user_finder } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl <R: CveRepository> QueryHandler for FindCveQueryHandler<R> { | ||
async fn handle(&self, query: Box<dyn Query>) -> Box<dyn QueryBusResponse> { | ||
let query = query.deref().as_any().downcast_ref::<FindCveQuery>().unwrap(); | ||
|
||
let id = match CveId::from_optional(&query.id) { | ||
Ok(id) => id, | ||
Err(err) => return CveQueryResponse::boxed_err(err), | ||
}; | ||
|
||
let res = self.crypto_keys_by_user_finder.run(id).await; | ||
if res.is_err() { | ||
return CveQueryResponse::boxed_err(res.err().unwrap()); | ||
} | ||
|
||
CveQueryResponse::boxed_ok(res.ok().unwrap()) | ||
} | ||
|
||
fn subscribet_to(&self) -> String { | ||
return FindCveQuery::QUERY_TYPE.to_string(); | ||
} | ||
} |
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,26 @@ | ||
use std::any::Any; | ||
|
||
use cqrs::domain::query::Query; | ||
|
||
|
||
pub struct FindCveQuery { | ||
pub id: Option<String>, | ||
} | ||
|
||
impl FindCveQuery { | ||
pub const QUERY_TYPE: &'static str = "FindCveQuery"; | ||
|
||
pub fn new(id: Option<String>) -> FindCveQuery { | ||
FindCveQuery { id } | ||
} | ||
} | ||
|
||
impl Query for FindCveQuery { | ||
fn get_type(&self) -> String { | ||
FindCveQuery::QUERY_TYPE.to_string() | ||
} | ||
|
||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
} |
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,3 @@ | ||
pub mod cve_finder; | ||
pub mod find_cve_query; | ||
pub mod find_cve_q_handler; |
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