From 5e0b8bb8515439125ffa0846eea4908a8f0628a7 Mon Sep 17 00:00:00 2001 From: Jim Hodapp Date: Thu, 30 Nov 2023 21:42:53 -0600 Subject: [PATCH] Reconfigure the Entity API find* methods for an Organization to use the ? operator --- entity_api/src/organization.rs | 9 +++------ web/src/controller/organization_controller.rs | 4 +++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/entity_api/src/organization.rs b/entity_api/src/organization.rs index d399e25..7b6458b 100644 --- a/entity_api/src/organization.rs +++ b/entity_api/src/organization.rs @@ -5,17 +5,14 @@ use sea_orm::{entity::prelude::*, ActiveValue}; use serde_json::json; use service::AppState; -pub async fn find_all(app_state: &AppState) -> Vec { +pub async fn find_all(app_state: &AppState) -> Result, Error> { let db = app_state.database_connection.as_ref().unwrap(); - Entity::find().all(db).await.unwrap_or(vec![]) + Ok(Entity::find().all(db).await?) } 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()) + Ok(Entity::find_by_id(id).one(db).await?) } pub(crate) async fn seed_database(app_state: &AppState) { diff --git a/web/src/controller/organization_controller.rs b/web/src/controller/organization_controller.rs index ba87283..498b632 100644 --- a/web/src/controller/organization_controller.rs +++ b/web/src/controller/organization_controller.rs @@ -22,7 +22,9 @@ 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).await; + let organizations = OrganizationApi::find_all(&app_state) + .await + .unwrap_or_default(); Json(organizations) }