Improve database errors (NotFound -> Option)
This commit is contained in:
parent
be7f31906c
commit
8dab4b9a50
9 changed files with 96 additions and 78 deletions
emgauwa-lib/src/db
|
@ -25,27 +25,30 @@ impl DbRelay {
|
|||
.await?)
|
||||
}
|
||||
|
||||
pub async fn get(conn: &mut PoolConnection<Sqlite>, id: i64) -> Result<DbRelay, DatabaseError> {
|
||||
sqlx::query_as!(DbRelay, "SELECT * FROM relays WHERE id = ?", id)
|
||||
.fetch_optional(conn.deref_mut())
|
||||
.await
|
||||
.map(|s| s.ok_or(DatabaseError::NotFound))?
|
||||
pub async fn get(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
id: i64,
|
||||
) -> Result<Option<DbRelay>, DatabaseError> {
|
||||
Ok(
|
||||
sqlx::query_as!(DbRelay, "SELECT * FROM relays WHERE id = ?", id)
|
||||
.fetch_optional(conn.deref_mut())
|
||||
.await?,
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn get_by_controller_and_num(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
controller: &DbController,
|
||||
number: i64,
|
||||
) -> Result<DbRelay, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
) -> Result<Option<DbRelay>, DatabaseError> {
|
||||
Ok(sqlx::query_as!(
|
||||
DbRelay,
|
||||
"SELECT * FROM relays WHERE controller_id = ? AND number = ?",
|
||||
controller.id,
|
||||
number
|
||||
)
|
||||
.fetch_optional(conn.deref_mut())
|
||||
.await
|
||||
.map(|s| s.ok_or(DatabaseError::NotFound))?
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn get_by_tag(
|
||||
|
@ -102,14 +105,18 @@ impl DbRelay {
|
|||
.execute(conn.deref_mut())
|
||||
.await?;
|
||||
|
||||
DbRelay::get(conn, self.id).await
|
||||
DbRelay::get(conn, self.id)
|
||||
.await?
|
||||
.ok_or(DatabaseError::UpdateGetError)
|
||||
}
|
||||
|
||||
pub async fn get_controller(
|
||||
&self,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
) -> Result<DbController, DatabaseError> {
|
||||
DbController::get(conn, self.controller_id).await
|
||||
DbController::get(conn, self.controller_id)
|
||||
.await?
|
||||
.ok_or(DatabaseError::NotFound)
|
||||
}
|
||||
|
||||
pub async fn get_tags(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue