Skip to content

Commit

Permalink
Add db_conn_ref() to struct impl AppState to provide a clean and conv…
Browse files Browse the repository at this point in the history
…enient way of getting a reference to the DatabaseConnection instance
  • Loading branch information
jhodapp committed Dec 1, 2023
1 parent b7e05d8 commit 031615d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
4 changes: 4 additions & 0 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ impl AppState {
config: app_config,
}
}

pub fn db_conn_ref(&self) -> Option<&DatabaseConnection> {
self.database_connection.as_ref()
}
}
19 changes: 7 additions & 12 deletions web/src/controller/organization_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ impl OrganizationController {
/// --request GET \
/// http://localhost:4000/organizations
pub async fn index(State(app_state): State<AppState>) -> impl IntoResponse {
let organizations =
OrganizationApi::find_all(&app_state.database_connection.unwrap()).await;
let organizations = OrganizationApi::find_all(app_state.db_conn_ref().unwrap()).await;

Json(organizations)
}
Expand All @@ -34,7 +33,7 @@ impl OrganizationController {
debug!("GET Organization by id: {}", id);

let organization: Option<organization::Model> =
OrganizationApi::find_by_id(&app_state.database_connection.unwrap(), id).await?;
OrganizationApi::find_by_id(app_state.db_conn_ref().unwrap(), id).await?;

Ok(Json(organization))
}
Expand All @@ -51,8 +50,7 @@ impl OrganizationController {
debug!("CREATE new Organization: {}", organization_model.name);

let organization: organization::Model =
OrganizationApi::create(&app_state.database_connection.unwrap(), organization_model)
.await?;
OrganizationApi::create(app_state.db_conn_ref().unwrap(), organization_model).await?;

Ok(Json(organization))
}
Expand All @@ -71,12 +69,9 @@ impl OrganizationController {
id, organization_model.name
);

let updated_organization: organization::Model = OrganizationApi::update(
&app_state.database_connection.unwrap(),
id,
organization_model,
)
.await?;
let updated_organization: organization::Model =
OrganizationApi::update(app_state.db_conn_ref().unwrap(), id, organization_model)
.await?;

Ok(Json(updated_organization))
}
Expand All @@ -91,7 +86,7 @@ impl OrganizationController {
) -> Result<impl IntoResponse, Error> {
debug!("DELETE Organization by id: {}", id);

OrganizationApi::delete_by_id(&app_state.database_connection.unwrap(), id).await?;
OrganizationApi::delete_by_id(app_state.db_conn_ref().unwrap(), id).await?;
Ok(Json(json!({"id": id})))
}
}
1 change: 0 additions & 1 deletion web/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::error::Error as StdError;

use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use serde::Serialize;

use entity_api::error::EntityApiErrorType;
use entity_api::error::Error as EntityApiError;
Expand Down

0 comments on commit 031615d

Please sign in to comment.