Repo layer refactor.
This commit is contained in:
@@ -21,24 +21,23 @@ pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<Account>, Repo
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let accounts = db::list(
|
||||
executor,
|
||||
db::Query {
|
||||
name: query.name,
|
||||
limit: query
|
||||
.limit
|
||||
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
|
||||
.transpose()?,
|
||||
offset: query
|
||||
.offset
|
||||
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
||||
.transpose()?,
|
||||
},
|
||||
)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<_, DecodeError>>()?;
|
||||
let query = db::Query {
|
||||
name: query.name,
|
||||
limit: query
|
||||
.limit
|
||||
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
|
||||
.transpose()?,
|
||||
offset: query
|
||||
.offset
|
||||
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
||||
.transpose()?,
|
||||
};
|
||||
|
||||
let accounts = db::list(executor, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
Ok(accounts)
|
||||
}
|
||||
@@ -67,12 +66,14 @@ where
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<Vec<_>, DecodeError>>()?;
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
Ok(accounts
|
||||
let account_map = accounts
|
||||
.into_iter()
|
||||
.map(|account| (account.id, account))
|
||||
.collect())
|
||||
.collect();
|
||||
|
||||
Ok(account_map)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
@@ -84,20 +85,22 @@ where
|
||||
{
|
||||
let account = db::upsert(executor, encode(account))
|
||||
.await?
|
||||
.try_map(decode)?;
|
||||
.try_map(decode)?
|
||||
.into();
|
||||
|
||||
Ok(account.into())
|
||||
Ok(account)
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = db::delete_by_id(executor, id)
|
||||
let result = db::delete_by_id(executor, id)
|
||||
.await
|
||||
.map_err(classify_delete_error)?;
|
||||
.map_err(classify_delete_error)?
|
||||
.into();
|
||||
|
||||
Ok(deleted.into())
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
@@ -202,6 +205,7 @@ mod tests {
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let account = decode(row()).unwrap();
|
||||
assert_eq!(decode(encode(account.clone())).unwrap(), account);
|
||||
let round_trip = decode(encode(account.clone())).unwrap();
|
||||
assert_eq!(round_trip, account);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user