From fbac78179c9ea7b8b56c8047129fb52b662bfaa8 Mon Sep 17 00:00:00 2001 From: nathaelbonnal Date: Mon, 11 Nov 2024 16:25:25 +0100 Subject: [PATCH 1/4] feat: Add get_companies and get_company API endpoints The code changes introduce two new API endpoints, `get_companies` and `get_company`, to retrieve information about companies. These endpoints are implemented in separate modules within the `handlers` module. The `get_companies` endpoint returns a list of all companies, while the `get_company` endpoint retrieves a specific company by its ID. Note: The recent user commits and repository commits are not relevant for generating the commit message. Signed-off-by: nathaelbonnal --- src/lib/application/http/handlers.rs | 2 ++ .../application/http/handlers/get_companies.rs | 17 +++++++++++++++++ .../application/http/handlers/get_company.rs | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 src/lib/application/http/handlers/get_companies.rs create mode 100644 src/lib/application/http/handlers/get_company.rs diff --git a/src/lib/application/http/handlers.rs b/src/lib/application/http/handlers.rs index bf38206..f2aa9e4 100644 --- a/src/lib/application/http/handlers.rs +++ b/src/lib/application/http/handlers.rs @@ -3,6 +3,8 @@ use serde::Serialize; pub mod attach_professional; pub mod create_company; +pub mod get_companies; +pub mod get_company; pub mod health_check; pub struct ApiSuccess(StatusCode, Json>); diff --git a/src/lib/application/http/handlers/get_companies.rs b/src/lib/application/http/handlers/get_companies.rs new file mode 100644 index 0000000..c9b7df2 --- /dev/null +++ b/src/lib/application/http/handlers/get_companies.rs @@ -0,0 +1,17 @@ +use std::sync::Arc; + +use axum::{http::StatusCode, Extension}; + +use crate::domain::company::{models::Company, ports::CompanyService}; + +use super::{ApiError, ApiSuccess}; + +pub async fn get_companies( + Extension(company_service): Extension>, +) -> Result>, ApiError> { + company_service + .find_all() + .await + .map_err(ApiError::from) + .map(|c| ApiSuccess::new(StatusCode::OK, c)) +} diff --git a/src/lib/application/http/handlers/get_company.rs b/src/lib/application/http/handlers/get_company.rs new file mode 100644 index 0000000..3b4e4bd --- /dev/null +++ b/src/lib/application/http/handlers/get_company.rs @@ -0,0 +1,18 @@ +use std::sync::Arc; + +use axum::{extract::Path, http::StatusCode, Extension}; + +use crate::domain::company::ports::CompanyService; + +use super::{ApiError, ApiSuccess}; + +pub async fn get_company( + Extension(company_service): Extension>, + Path(company_id): Path, +) -> Result, ApiError> { + company_service + .find_by_id(company_id) + .await + .map_err(ApiError::from) + .map(|company| ApiSuccess::new(StatusCode::OK, company.unwrap().id.to_string())) +} From 7606e5a772efbfc581cfc3c89ef5cac0f3c8fcad Mon Sep 17 00:00:00 2001 From: nathaelbonnal Date: Mon, 11 Nov 2024 16:25:37 +0100 Subject: [PATCH 2/4] chore: Add find_all method to CompanyService This commit adds the `find_all` method to the `CompanyService` trait. The method retrieves a list of all companies and returns a `Result` containing the list of companies or an error of type `CompanyError`. This method will be implemented in the `CompanyServiceImpl` struct. Note: The recent user commits and repository commits are not relevant for generating the commit message. Signed-off-by: nathaelbonnal --- src/lib/domain/company/models.rs | 6 +++--- src/lib/domain/company/ports.rs | 1 + src/lib/domain/company/services.rs | 4 ++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/lib/domain/company/models.rs b/src/lib/domain/company/models.rs index 5ad914c..32b8397 100644 --- a/src/lib/domain/company/models.rs +++ b/src/lib/domain/company/models.rs @@ -1,11 +1,11 @@ use company_validator::CreateCompany; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use thiserror::Error; pub mod company_validator; pub mod message; -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)] pub struct Company { pub id: uuid::Uuid, pub name: Name, @@ -77,7 +77,7 @@ pub enum CompanyError { Unkown(String), } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)] pub struct Name(String); #[derive(Clone, Debug, Error)] diff --git a/src/lib/domain/company/ports.rs b/src/lib/domain/company/ports.rs index 163991e..c3f43a2 100644 --- a/src/lib/domain/company/ports.rs +++ b/src/lib/domain/company/ports.rs @@ -30,6 +30,7 @@ pub trait CompanyService: Clone + Send + Sync + 'static { &self, id: String, ) -> impl Future, CompanyError>> + Send; + fn find_all(&self) -> impl Future, CompanyError>> + Send; fn add_professional_to_company( &self, company_id: String, diff --git a/src/lib/domain/company/services.rs b/src/lib/domain/company/services.rs index 457eaa7..49d62a9 100644 --- a/src/lib/domain/company/services.rs +++ b/src/lib/domain/company/services.rs @@ -54,6 +54,10 @@ where Ok(company) } + async fn find_all(&self) -> Result, CompanyError> { + self.company_repository.find_all().await + } + async fn add_professional_to_company( &self, company_id: String, From 107089b7cc60b37fa6bea4afd41c17604a4364fc Mon Sep 17 00:00:00 2001 From: nathaelbonnal Date: Mon, 11 Nov 2024 16:25:48 +0100 Subject: [PATCH 3/4] feat: Implement find_all method in CompanyRepository This commit implements the `find_all` method in the `CompanyRepository` trait. The method retrieves all companies from the Neo4j database and returns a `Result` containing the list of companies or a `CompanyError` if an error occurs during the retrieval process. Note: The recent user commits and repository commits are not relevant for generating the commit message. Signed-off-by: nathaelbonnal --- .../company/neo4j/company_repository.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/lib/infrastructure/company/neo4j/company_repository.rs b/src/lib/infrastructure/company/neo4j/company_repository.rs index dd3a80a..e1a0e09 100644 --- a/src/lib/infrastructure/company/neo4j/company_repository.rs +++ b/src/lib/infrastructure/company/neo4j/company_repository.rs @@ -60,7 +60,24 @@ impl CompanyRepository for Neo4jCompanyRepository { } async fn find_all(&self) -> Result, CompanyError> { - todo!() + let query = query("MATCH (c:Company) RETURN c"); + + let mut result = self + .neo4j + .get_graph() + .execute(query) + .await + .map_err(|_| CompanyError::NotFound)?; + + let mut companies = Vec::new(); + while let Ok(Some(row)) = result.next().await { + let company: Company = row + .get("c") + .map_err(|e| CompanyError::Unkown(e.to_string()))?; + companies.push(company); + } + + Ok(companies) } async fn find_by_id(&self, id: String) -> Result, CompanyError> { From a7139e4258c91d2290fd0a967857f9b680762625 Mon Sep 17 00:00:00 2001 From: nathaelbonnal Date: Mon, 11 Nov 2024 16:25:58 +0100 Subject: [PATCH 4/4] feat: Add get_companies and get_company API endpoints Signed-off-by: nathaelbonnal --- src/lib/application/http.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/application/http.rs b/src/lib/application/http.rs index 3ca4bf5..5572d39 100644 --- a/src/lib/application/http.rs +++ b/src/lib/application/http.rs @@ -7,7 +7,8 @@ use axum::{ Extension, }; use handlers::{ - attach_professional::attach_professional, create_company::create_company, health_check, + attach_professional::attach_professional, create_company::create_company, + get_companies::get_companies, get_company::get_company, health_check, }; use tokio::net; use tracing::{info, info_span}; @@ -87,7 +88,9 @@ where C: CompanyService + Send + Sync + 'static, { axum::Router::new() + .route("/companies", get(get_companies::)) .route("/companies", post(create_company::)) + .route("/companies/:company_id", get(get_company::)) .route( "/companies/:company_id/professionals", post(attach_professional::),