From 7a065c522b74060f2aa0eeced9891b8a4e5d02f6 Mon Sep 17 00:00:00 2001 From: Jim Hodapp Date: Thu, 30 Nov 2023 17:17:41 -0600 Subject: [PATCH] No longer pass around a DBConnection for EntityApi, but AppState instead --- Cargo.lock | 1 + entity_api/Cargo.toml | 1 + entity_api/src/error.rs | 3 ++- entity_api/src/lib.rs | 6 +++--- entity_api/src/organization.rs | 13 +++++++++---- src/main.rs | 2 +- web/src/controller/organization_controller.rs | 5 ++--- web/src/error.rs | 7 +++++++ 8 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 62ac544..2bd77d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -793,6 +793,7 @@ dependencies = [ "log", "sea-orm", "serde_json", + "service", ] [[package]] diff --git a/entity_api/Cargo.toml b/entity_api/Cargo.toml index 54e47b0..0caf146 100644 --- a/entity_api/Cargo.toml +++ b/entity_api/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] entity = { path = "../entity" } +service = { path = "../service" } serde_json = "1.0.107" log = "0.4.20" diff --git a/entity_api/src/error.rs b/entity_api/src/error.rs index 09c3311..32d35b7 100644 --- a/entity_api/src/error.rs +++ b/entity_api/src/error.rs @@ -17,6 +17,7 @@ pub struct Error { #[derive(Debug)] pub enum EntityApiErrorType { + DatabaseConnectionLost, // Record not found RecordNotFound, // Record not updated @@ -50,7 +51,7 @@ impl From for Error { }, DbErr::Conn(_) => Error { inner: err, - error_type: EntityApiErrorType::SystemError, + error_type: EntityApiErrorType::DatabaseConnectionLost, }, DbErr::Exec(_) => Error { inner: err, diff --git a/entity_api/src/lib.rs b/entity_api/src/lib.rs index 159daad..08d7600 100644 --- a/entity_api/src/lib.rs +++ b/entity_api/src/lib.rs @@ -1,8 +1,8 @@ -use sea_orm::DatabaseConnection; +use service::AppState; pub mod error; pub mod organization; -pub async fn seed_database(db: &DatabaseConnection) { - organization::seed_database(db).await; +pub async fn seed_database(app_state: &AppState) { + organization::seed_database(app_state).await; } diff --git a/entity_api/src/organization.rs b/entity_api/src/organization.rs index d07b17a..d399e25 100644 --- a/entity_api/src/organization.rs +++ b/entity_api/src/organization.rs @@ -1,27 +1,32 @@ use super::error::Error; use entity::organization; use organization::{Entity, Model}; -use sea_orm::{entity::prelude::*, ActiveValue, DatabaseConnection}; +use sea_orm::{entity::prelude::*, ActiveValue}; use serde_json::json; +use service::AppState; -pub async fn find_all(db: &DatabaseConnection) -> Vec { +pub async fn find_all(app_state: &AppState) -> Vec { + let db = app_state.database_connection.as_ref().unwrap(); Entity::find().all(db).await.unwrap_or(vec![]) } -pub async fn find_by_id(db: &DatabaseConnection, id: i32) -> Result, Error> { +pub async fn find_by_id(app_state: &AppState, id: i32) -> Result, Error> { + let db = app_state.database_connection.as_ref().unwrap(); Entity::find_by_id(id) .one(db) .await .map_err(|err| err.into()) } -pub(crate) async fn seed_database(db: &DatabaseConnection) { +pub(crate) async fn seed_database(app_state: &AppState) { let organization_names = [ "Jim Hodapp Coaching", "Caleb Coaching", "Enterprise Software", ]; + let db = app_state.database_connection.as_ref().unwrap(); + for name in organization_names { let organization = organization::ActiveModel::from_json(json!({ "name": name, diff --git a/src/main.rs b/src/main.rs index 7b1c475..28c3d44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ async fn main() { let mut app_state = AppState::new(config); app_state = service::init_database(app_state).await.unwrap(); - entity_api::seed_database(app_state.database_connection.as_ref().unwrap()).await; + entity_api::seed_database(&app_state).await; web::init_server(app_state).await.unwrap(); } diff --git a/web/src/controller/organization_controller.rs b/web/src/controller/organization_controller.rs index 98bd456..ba87283 100644 --- a/web/src/controller/organization_controller.rs +++ b/web/src/controller/organization_controller.rs @@ -22,8 +22,7 @@ impl OrganizationController { /// --request GET \ /// http://localhost:4000/organizations pub async fn index(State(app_state): State) -> impl IntoResponse { - let organizations = - OrganizationApi::find_all(&app_state.database_connection.unwrap()).await; + let organizations = OrganizationApi::find_all(&app_state).await; Json(organizations) } @@ -36,7 +35,7 @@ impl OrganizationController { debug!("GET Organization by id: {}", id); let organization: Result, Error> = - OrganizationApi::find_by_id(&app_state.database_connection.unwrap(), id) + OrganizationApi::find_by_id(&app_state, id) .await .map_err(|err| err.into()); diff --git a/web/src/error.rs b/web/src/error.rs index 0e76bd9..8a5f1a7 100644 --- a/web/src/error.rs +++ b/web/src/error.rs @@ -12,6 +12,7 @@ pub type Result = core::result::Result; #[derive(Debug, Serialize)] pub enum Error { + DatabaseConnectionLost, InternalServer, EntityNotFound, UnprocessableEntity, @@ -28,6 +29,11 @@ impl std::fmt::Display for Error { impl IntoResponse for Error { fn into_response(self) -> Response { match self { + Error::DatabaseConnectionLost => ( + StatusCode::INTERNAL_SERVER_ERROR, + "DB CONNECTION LOST - INTERNAL SERVER ERROR", + ) + .into_response(), Error::InternalServer => { (StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL SERVER ERROR").into_response() } @@ -42,6 +48,7 @@ impl IntoResponse for Error { impl From for Error { fn from(err: EntityApiError) -> Self { match err.error_type { + EntityApiErrorType::DatabaseConnectionLost => Error::DatabaseConnectionLost, EntityApiErrorType::RecordNotFound => Error::EntityNotFound, EntityApiErrorType::RecordNotUpdated => Error::UnprocessableEntity, EntityApiErrorType::SystemError => Error::InternalServer,