Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement endpoints to get companies #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lib/application/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -87,7 +88,9 @@ where
C: CompanyService + Send + Sync + 'static,
{
axum::Router::new()
.route("/companies", get(get_companies::<C>))
.route("/companies", post(create_company::<C>))
.route("/companies/:company_id", get(get_company::<C>))
.route(
"/companies/:company_id/professionals",
post(attach_professional::<C>),
Expand Down
2 changes: 2 additions & 0 deletions src/lib/application/http/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Serialize + PartialEq>(StatusCode, Json<ApiResponseBody<T>>);
Expand Down
17 changes: 17 additions & 0 deletions src/lib/application/http/handlers/get_companies.rs
Original file line number Diff line number Diff line change
@@ -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<C: CompanyService>(
Extension(company_service): Extension<Arc<C>>,
) -> Result<ApiSuccess<Vec<Company>>, ApiError> {
company_service
.find_all()
.await
.map_err(ApiError::from)
.map(|c| ApiSuccess::new(StatusCode::OK, c))
}
18 changes: 18 additions & 0 deletions src/lib/application/http/handlers/get_company.rs
Original file line number Diff line number Diff line change
@@ -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<C: CompanyService>(
Extension(company_service): Extension<Arc<C>>,
Path(company_id): Path<String>,
) -> Result<ApiSuccess<String>, ApiError> {
company_service
.find_by_id(company_id)
.await
.map_err(ApiError::from)
.map(|company| ApiSuccess::new(StatusCode::OK, company.unwrap().id.to_string()))
}
6 changes: 3 additions & 3 deletions src/lib/domain/company/models.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions src/lib/domain/company/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub trait CompanyService: Clone + Send + Sync + 'static {
&self,
id: String,
) -> impl Future<Output = Result<Option<Company>, CompanyError>> + Send;
fn find_all(&self) -> impl Future<Output = Result<Vec<Company>, CompanyError>> + Send;
fn add_professional_to_company(
&self,
company_id: String,
Expand Down
4 changes: 4 additions & 0 deletions src/lib/domain/company/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ where
Ok(company)
}

async fn find_all(&self) -> Result<Vec<Company>, CompanyError> {
self.company_repository.find_all().await
}

async fn add_professional_to_company(
&self,
company_id: String,
Expand Down
19 changes: 18 additions & 1 deletion src/lib/infrastructure/company/neo4j/company_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,24 @@ impl CompanyRepository for Neo4jCompanyRepository {
}

async fn find_all(&self) -> Result<Vec<Company>, 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<Option<Company>, CompanyError> {
Expand Down