Skip to content

Commit

Permalink
update find_by_id and find_all
Browse files Browse the repository at this point in the history
  • Loading branch information
calebbourg committed Dec 5, 2023
1 parent 42953b7 commit 738193a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions entity_api/src/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ pub async fn delete_by_id(db: &DatabaseConnection, id: i32) -> Result<(), Error>
}
}

pub async fn find_all(db: &DatabaseConnection) -> Vec<Model> {
Entity::find().all(db).await.unwrap_or(vec![])
pub async fn find_all(db: &DatabaseConnection) -> Result<Vec<Model>, Error> {
Ok(Entity::find().all(db).await?)
}

pub async fn find_by_id(db: &DatabaseConnection, id: i32) -> Result<Option<Model>, Error> {
Ok(Entity::find_by_id(id).one(db).await?)
let organization = Entity::find_by_id(id).one(db).await?;
debug!("Organization found: {:?}", organization);

Ok(organization)
}

pub(crate) async fn seed_database(db: &DatabaseConnection) {
Expand Down
6 changes: 3 additions & 3 deletions web/src/controller/organization_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ impl OrganizationController {
/// Test this with curl: curl --header "Content-Type: application/json" \ in zsh at 12:03:06
/// --request GET \
/// http://localhost:4000/organizations
pub async fn index(State(app_state): State<AppState>) -> impl IntoResponse {
let organizations = OrganizationApi::find_all(app_state.db_conn_ref().unwrap()).await;
pub async fn index(State(app_state): State<AppState>) -> Result<impl IntoResponse, Error> {
let organizations = OrganizationApi::find_all(app_state.db_conn_ref().unwrap()).await?;

Json(organizations)
Ok(Json(organizations))
}

/// GET a particular Organization entity specified by its primary key
Expand Down

0 comments on commit 738193a

Please sign in to comment.