Skip to content

Commit

Permalink
add find cve query handler
Browse files Browse the repository at this point in the history
  • Loading branch information
n1nj4t4nuk1 committed Dec 4, 2024
1 parent 3432703 commit 15596c6
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 1 deletion.
49 changes: 49 additions & 0 deletions libs/cti/src/cves/application/cve_query_response.rs
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
}
}
25 changes: 25 additions & 0 deletions libs/cti/src/cves/application/find_one/cve_finder.rs
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 libs/cti/src/cves/application/find_one/find_cve_q_handler.rs
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();
}
}
26 changes: 26 additions & 0 deletions libs/cti/src/cves/application/find_one/find_cve_query.rs
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
}
}
3 changes: 3 additions & 0 deletions libs/cti/src/cves/application/find_one/mod.rs
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;
3 changes: 2 additions & 1 deletion libs/cti/src/cves/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod create_one;
pub mod update_one;
pub mod delete_one;

pub mod cve_command_response;
pub mod cve_command_response;
pub mod cve_query_response;

0 comments on commit 15596c6

Please sign in to comment.