Skip to content

Commit

Permalink
format all files
Browse files Browse the repository at this point in the history
  • Loading branch information
n1nj4t4nuk1 committed Dec 5, 2024
1 parent 77d5334 commit 0641ac5
Show file tree
Hide file tree
Showing 71 changed files with 441 additions and 258 deletions.
29 changes: 24 additions & 5 deletions apps/cti/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
pub mod v1;

use std::sync::{atomic::{AtomicU16, Ordering}, Arc};
use std::sync::{
atomic::{AtomicU16, Ordering},
Arc,
};

use actix_web::{web::Data, App, HttpServer};
use cqrs::{domain::{command_bus::CommandBus, query_bus::QueryBus}, infrastructure::inmemory::{inmemory_command_bus::InMemoryCommandBus, inmemory_query_bus::InMemoryQueryBus}};
use cti::cves::{application::{create_one::{create_cve_command_handler::CreateCveCommandHandler, cve_creator::CveCreator}, delete_one::{cve_deleter::CveDeleter, delete_cve_command_handler::DeleteCveCommandHandler}, find_one::{cve_finder::CveFinder, find_cve_q_handler::FindCveQueryHandler}}, infrastructure::sqlx::sqlx_postgres_cve_repository::SqlxPostgresCveRepository};
use cqrs::{
domain::{command_bus::CommandBus, query_bus::QueryBus},
infrastructure::inmemory::{
inmemory_command_bus::InMemoryCommandBus, inmemory_query_bus::InMemoryQueryBus,
},
};
use cti::cves::{
application::{
create_one::{
create_cve_command_handler::CreateCveCommandHandler, cve_creator::CveCreator,
},
delete_one::{
cve_deleter::CveDeleter, delete_cve_command_handler::DeleteCveCommandHandler,
},
find_one::{cve_finder::CveFinder, find_cve_q_handler::FindCveQueryHandler},
},
infrastructure::sqlx::sqlx_postgres_cve_repository::SqlxPostgresCveRepository,
};
use events::infrastructure::inmemory::inmemory_event_bus::InMemoryEventBus;
use tracing::{self as logger};
use v1::health::health_controller;
Expand Down Expand Up @@ -52,7 +71,7 @@ async fn main() -> std::io::Result<()> {

let query_bus_ref: Data<Arc<dyn QueryBus>> = Data::new(Arc::new(query_bus));
let command_bus_ref: Data<Arc<dyn CommandBus>> = Data::new(Arc::new(command_bus));

HttpServer::new(move || {
let thread_counter = thread_counter.fetch_add(1, Ordering::SeqCst);
logger::info!("Thread {} started.", thread_counter);
Expand All @@ -76,4 +95,4 @@ async fn main() -> std::io::Result<()> {
})
.run()
.await
}
}
29 changes: 17 additions & 12 deletions apps/cti/src/v1/cves/cve_get_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ use std::sync::Arc;

use actix_web::{web, HttpRequest, HttpResponse};
use cqrs::domain::query_bus::QueryBus;
use cti::{cves::{application::{cve_query_response::CveQueryResponse, find_one::find_cve_query::FindCveQuery}, domain::repositories::cve_repository::CveRepository, infrastructure::dtos::cve_json_dto::parse_to_dto}, shared::domain::errors::DomainError};
use cti::{
cves::{
application::{
cve_query_response::CveQueryResponse, find_one::find_cve_query::FindCveQuery,
},
domain::repositories::cve_repository::CveRepository,
infrastructure::dtos::cve_json_dto::parse_to_dto,
},
shared::domain::errors::DomainError,
};
use tracing::{self as logger};

pub async fn controller<R: CveRepository>(
Expand All @@ -11,9 +20,7 @@ pub async fn controller<R: CveRepository>(
query_bus: web::Data<Arc<dyn QueryBus>>,
) -> HttpResponse {
logger::debug!("GET /api/v1/cves/{} called.", cve_id);
let q = FindCveQuery::new_boxed(
Some(cve_id.clone()),
);
let q = FindCveQuery::new_boxed(Some(cve_id.clone()));

let res = query_bus.ask(q).await;
let res = match res.as_any().downcast_ref::<CveQueryResponse>() {
Expand All @@ -22,20 +29,18 @@ pub async fn controller<R: CveRepository>(
};

if res.is_ok() {
let cve= res.cve.as_ref().unwrap();
let cve = res.cve.as_ref().unwrap();
let json_cve = parse_to_dto(&cve);
return HttpResponse::Ok().json(json_cve);
}

match &res.error {
None => HttpResponse::InternalServerError().finish(),
Some(err) => {
match err {
DomainError::CveNotFound { id } => {
HttpResponse::NotFound().body(format!("CVE with id <{}>, not found..", id))
},
_ => HttpResponse::InternalServerError().finish()
Some(err) => match err {
DomainError::CveNotFound { id } => {
HttpResponse::NotFound().body(format!("CVE with id <{}>, not found..", id))
}
}
_ => HttpResponse::InternalServerError().finish(),
},
}
}
34 changes: 21 additions & 13 deletions apps/cti/src/v1/cves/cve_post_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ use std::sync::Arc;

use actix_web::{web, HttpRequest, HttpResponse};
use cqrs::domain::command_bus::CommandBus;
use cti::{cves::{application::{create_one::create_cve_command::CreateCveCommand, cve_command_response::CveCommandResponse}, domain::repositories::cve_repository::CveRepository, infrastructure::dtos::cve_json_dto::CveJsonDto}, shared::domain::errors::DomainError};
use cti::{
cves::{
application::{
create_one::create_cve_command::CreateCveCommand,
cve_command_response::CveCommandResponse,
},
domain::repositories::cve_repository::CveRepository,
infrastructure::dtos::cve_json_dto::CveJsonDto,
},
shared::domain::errors::DomainError,
};
use events::domain::event_bus::EventBus;

pub async fn controller<R: CveRepository, E: EventBus>(
Expand All @@ -11,10 +21,10 @@ pub async fn controller<R: CveRepository, E: EventBus>(
command_bus: web::Data<Arc<dyn CommandBus>>,
) -> HttpResponse {
let cmd = CreateCveCommand::new_boxed(
dto.id.clone(),
dto.state.clone(),
dto.date_published.clone(),
dto.description.clone()
dto.id.clone(),
dto.state.clone(),
dto.date_published.clone(),
dto.description.clone(),
);

let res = command_bus.dispatch(cmd).await;
Expand All @@ -24,18 +34,16 @@ pub async fn controller<R: CveRepository, E: EventBus>(
};

if res.is_ok() {
return HttpResponse::Accepted().finish()
return HttpResponse::Accepted().finish();
}

match &res.error {
None => HttpResponse::InternalServerError().finish(),
Some(err) => {
match err {
DomainError::CveNotFound { id } => {
HttpResponse::NotFound().body(format!("CVE with id <{}>, not found..", id))
},
_ => HttpResponse::InternalServerError().finish()
Some(err) => match err {
DomainError::CveNotFound { id } => {
HttpResponse::NotFound().body(format!("CVE with id <{}>, not found..", id))
}
}
_ => HttpResponse::InternalServerError().finish(),
},
}
}
15 changes: 11 additions & 4 deletions apps/cti/src/v1/cves/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ use actix_web::web::{self, ServiceConfig};
use cti::cves::domain::repositories::cve_repository::CveRepository;
use events::domain::event_bus::EventBus;

pub mod cve_post_controller;
pub mod cve_get_controller;
pub mod cve_post_controller;

pub fn router<R: CveRepository, E: EventBus>(cfg: &mut ServiceConfig) {
cfg.service(
web::scope("/api/v1/cves")
.configure(|cfg: &mut ServiceConfig| -> () { cfg.route("/{cve_id}", web::get().to(cve_get_controller::controller::<R>)); })
.configure(|cfg: &mut ServiceConfig| -> () { cfg.route("/", web::post().to(cve_post_controller::controller::<R, E>)); })
.configure(|cfg: &mut ServiceConfig| -> () {
cfg.route(
"/{cve_id}",
web::get().to(cve_get_controller::controller::<R>),
);
})
.configure(|cfg: &mut ServiceConfig| -> () {
cfg.route("/", web::post().to(cve_post_controller::controller::<R, E>));
}),
);
}
}
2 changes: 1 addition & 1 deletion apps/cti/src/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod cves;
pub mod health;
pub mod cves;
26 changes: 20 additions & 6 deletions libs/cti/src/cves/application/create_one/create_cve_command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use cqrs::domain::command::Command;


pub struct CreateCveCommand {
pub id: Option<String>,
pub state: Option<String>,
Expand All @@ -11,7 +10,12 @@ pub struct CreateCveCommand {
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 {
pub fn new(
id: Option<String>,
state: Option<String>,
date_published: Option<String>,
description: Option<String>,
) -> CreateCveCommand {
CreateCveCommand {
id,
state,
Expand All @@ -20,17 +24,27 @@ impl CreateCveCommand {
}
}

pub fn new_boxed(id: Option<String>, state: Option<String>, date_published: Option<String>, description: Option<String>) -> Box<dyn Command> {
Box::new(CreateCveCommand::new(id, state, date_published, description))
pub fn new_boxed(
id: Option<String>,
state: Option<String>,
date_published: Option<String>,
description: Option<String>,
) -> Box<dyn Command> {
Box::new(CreateCveCommand::new(
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
}
}
}
Original file line number Diff line number Diff line change
@@ -1,57 +1,68 @@

use async_trait::async_trait;
use cqrs::domain::{command::Command, command_bus_response::CommandBusResponse, command_handler::CommandHandler};
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_description::CveDescription, cve_id::CveId, cve_publication_date::CvePublicationDate, cve_state::CveState}, repositories::cve_repository::CveRepository}};
use crate::cves::{
application::cve_command_response::CveCommandResponse,
domain::{
entities::{
cve_description::CveDescription, cve_id::CveId,
cve_publication_date::CvePublicationDate, cve_state::CveState,
},
repositories::cve_repository::CveRepository,
},
};

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


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

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

#[async_trait]
impl <R: CveRepository, E: EventBus> CommandHandler for CreateCveCommandHandler<R, E> {
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)
Err(err) => return CveCommandResponse::boxed_err(err),
};

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

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

let description = match CveDescription::new(&command.description) {
Ok(description) => description,
Err(err) => return CveCommandResponse::boxed_err(err)
Err(err) => return CveCommandResponse::boxed_err(err),
};

match self.creator.run(id, state, publication_date, description).await {
match self
.creator
.run(id, state, publication_date, description)
.await
{
Ok(_) => CveCommandResponse::boxed_ok(),
Err(err) => CveCommandResponse::boxed_err(err)
Err(err) => CveCommandResponse::boxed_err(err),
}
}

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


18 changes: 15 additions & 3 deletions libs/cti/src/cves/application/create_one/cve_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ use std::sync::Arc;

use events::domain::event_bus::EventBus;

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};

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 CveCreator<R: CveRepository, E: EventBus> {
repository: Arc<R>,
Expand All @@ -12,7 +21,10 @@ pub struct CveCreator<R: CveRepository, E: EventBus> {

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

pub async fn run(
Expand Down
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;
pub mod cve_creator;
5 changes: 2 additions & 3 deletions libs/cti/src/cves/application/cve_command_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use cqrs::domain::command_bus_response::CommandBusResponse;

use crate::shared::domain::errors::DomainError;


pub struct CveCommandResponse {
pub error: Option<DomainError>,
}
Expand Down Expand Up @@ -45,8 +44,8 @@ impl CommandBusResponse for CveCommandResponse {
fn response_type(&self) -> String {
Self::RES_TYPE.to_string()
}

fn as_any(&self) -> &dyn std::any::Any {
self
}
}
}
Loading

0 comments on commit 0641ac5

Please sign in to comment.