Improve database errors (NotFound -> Option)

This commit is contained in:
Tobias Reisinger 2023-11-27 13:33:04 +01:00
parent be7f31906c
commit 8dab4b9a50
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
9 changed files with 96 additions and 78 deletions
emgauwa-lib/src/handlers/v1

View file

@ -39,7 +39,9 @@ pub async fn tagged(
let mut pool_conn = pool.acquire().await?;
let (tag,) = path.into_inner();
let tag_db = DbTag::get_by_tag(&mut pool_conn, &tag).await?;
let tag_db = DbTag::get_by_tag(&mut pool_conn, &tag)
.await?
.ok_or(DatabaseError::NotFound)?;
let schedules = DbSchedule::get_by_tag(&mut pool_conn, &tag_db).await?;
@ -61,7 +63,9 @@ pub async fn show(
let (schedule_uid,) = path.into_inner();
let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid).await?;
let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid)
.await?
.ok_or(DatabaseError::NotFound)?;
let return_schedule = Schedule::from_schedule(schedule, &mut pool_conn);
Ok(HttpResponse::Ok().json(return_schedule))
@ -136,7 +140,9 @@ pub async fn update(
let (schedule_uid,) = path.into_inner();
let uid = ScheduleUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid).await?;
let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid)
.await?
.ok_or(DatabaseError::NotFound)?;
let schedule = schedule
.update(&mut pool_conn, data.name.as_str(), &data.periods)