Skip to content

Commit

Permalink
apply change in find_by_id pattern to other entities
Browse files Browse the repository at this point in the history
  • Loading branch information
calebbourg committed Feb 24, 2025
1 parent 2df1bd3 commit d774660
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 105 deletions.
41 changes: 7 additions & 34 deletions entity_api/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,43 +106,16 @@ pub async fn update_status(
pub async fn delete_by_id(db: &DatabaseConnection, id: Id) -> Result<(), Error> {
let result = find_by_id(db, id).await?;

match result {
Some(action_model) => {
debug!("Existing Action model to be deleted: {:?}", action_model);
result.delete(db).await?;

action_model.delete(db).await?;
Ok(())
}
None => Err(Error {
source: None,
error_kind: EntityApiErrorKind::RecordNotFound,
}),
}
Ok(())
}

pub async fn find_by_id(db: &DatabaseConnection, id: Id) -> Result<Option<Model>, Error> {
match Entity::find_by_id(id).one(db).await {
Ok(Some(action)) => {
debug!("Action found: {:?}", action);

Ok(Some(action))
}
Ok(None) => {
error!("Action with id {} not found", id);

Err(Error {
source: None,
error_kind: EntityApiErrorKind::RecordNotFound,
})
}
Err(err) => {
error!("Action with id {} not found and returned error {}", id, err);
Err(Error {
source: None,
error_kind: EntityApiErrorKind::RecordNotFound,
})
}
}
pub async fn find_by_id(db: &DatabaseConnection, id: Id) -> Result<Model, Error> {
Entity::find_by_id(id).one(db).await?.ok_or_else(|| Error {
source: None,
error_kind: EntityApiErrorKind::RecordNotFound,
})
}

pub async fn find_by(
Expand Down
46 changes: 7 additions & 39 deletions entity_api/src/agreement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,47 +63,15 @@ pub async fn update(db: &DatabaseConnection, id: Id, model: Model) -> Result<Mod
pub async fn delete_by_id(db: &DatabaseConnection, id: Id) -> Result<(), Error> {
let result = find_by_id(db, id).await?;

match result {
Some(agreement_model) => {
debug!(
"Existing Agreement model to be deleted: {:?}",
agreement_model
);

agreement_model.delete(db).await?;
Ok(())
}
None => Err(Error {
source: None,
error_kind: EntityApiErrorKind::RecordNotFound,
}),
}
result.delete(db).await?;
Ok(())
}

pub async fn find_by_id(db: &DatabaseConnection, id: Id) -> Result<Option<Model>, Error> {
match Entity::find_by_id(id).one(db).await {
Ok(Some(agreement)) => {
debug!("Agreement found: {:?}", agreement);

Ok(Some(agreement))
}
Ok(None) => {
error!("Agreement with id {} not found", id);

Err(Error {
source: None,
error_kind: EntityApiErrorKind::RecordNotFound,
})
}
Err(err) => {
error!("Error finding Agreement with id {}: {:?}", id, err);

Err(Error {
source: Some(err),
error_kind: EntityApiErrorKind::RecordNotFound,
})
}
}
pub async fn find_by_id(db: &DatabaseConnection, id: Id) -> Result<Model, Error> {
Entity::find_by_id(id).one(db).await?.ok_or_else(|| Error {
source: None,
error_kind: EntityApiErrorKind::RecordNotFound,
})
}

pub async fn find_by(
Expand Down
31 changes: 5 additions & 26 deletions entity_api/src/overarching_goal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,32 +129,11 @@ pub async fn update_status(
}
}

pub async fn find_by_id(db: &DatabaseConnection, id: Id) -> Result<Option<Model>, Error> {
match Entity::find_by_id(id).one(db).await {
Ok(Some(overarching_goal)) => {
debug!("Overarching Goal found: {:?}", overarching_goal);

Ok(Some(overarching_goal))
}
Ok(None) => {
error!("Overarching Goal with id {} not found", id);

Err(Error {
source: None,
error_kind: EntityApiErrorKind::RecordNotFound,
})
}
Err(err) => {
error!(
"Overarching Goal with id {} not found and returned error {}",
id, err
);
Err(Error {
source: None,
error_kind: EntityApiErrorKind::RecordNotFound,
})
}
}
pub async fn find_by_id(db: &DatabaseConnection, id: Id) -> Result<Model, Error> {
Entity::find_by_id(id).one(db).await?.ok_or_else(|| Error {
source: None,
error_kind: EntityApiErrorKind::RecordNotFound,
})
}

pub async fn find_by(
Expand Down
4 changes: 2 additions & 2 deletions web/src/controller/action_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ pub async fn read(
) -> Result<impl IntoResponse, Error> {
debug!("GET Action by id: {}", id);

let note: Option<Model> = ActionApi::find_by_id(app_state.db_conn_ref(), id).await?;
let action = ActionApi::find_by_id(app_state.db_conn_ref(), id).await?;

Ok(Json(ApiResponse::new(StatusCode::OK.into(), note)))
Ok(Json(ApiResponse::new(StatusCode::OK.into(), action)))
}

#[utoipa::path(
Expand Down
4 changes: 2 additions & 2 deletions web/src/controller/agreement_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ pub async fn read(
) -> Result<impl IntoResponse, Error> {
debug!("GET Agreement by id: {}", id);

let note: Option<Model> = AgreementApi::find_by_id(app_state.db_conn_ref(), id).await?;
let agreement = AgreementApi::find_by_id(app_state.db_conn_ref(), id).await?;

Ok(Json(ApiResponse::new(StatusCode::OK.into(), note)))
Ok(Json(ApiResponse::new(StatusCode::OK.into(), agreement)))
}

#[utoipa::path(
Expand Down
7 changes: 5 additions & 2 deletions web/src/controller/overarching_goal_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ pub async fn read(
) -> Result<impl IntoResponse, Error> {
debug!("GET Overarching Goal by id: {}", id);

let note: Option<Model> = OverarchingGoalApi::find_by_id(app_state.db_conn_ref(), id).await?;
let overarching_goal = OverarchingGoalApi::find_by_id(app_state.db_conn_ref(), id).await?;

Ok(Json(ApiResponse::new(StatusCode::OK.into(), note)))
Ok(Json(ApiResponse::new(
StatusCode::OK.into(),
overarching_goal,
)))
}

#[utoipa::path(
Expand Down

0 comments on commit d774660

Please sign in to comment.