Repo layer refactor.
This commit is contained in:
@@ -4,6 +4,7 @@ use super::{QueryConversionError, RepositoryError, dtos::Upsert};
|
||||
use crate::{
|
||||
database::{Constraint, DatabaseError, dtos::UserAppRow, user_apps as db},
|
||||
domain::UserApp,
|
||||
repository::DecodeError,
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -16,26 +17,25 @@ pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserApp>, Repo
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::list(
|
||||
executor,
|
||||
db::Query {
|
||||
user_id: query.user_id,
|
||||
limit: query
|
||||
.limit
|
||||
.map(|limit| i64::try_from(limit).map_err(|_| QueryConversionError::Limit(limit)))
|
||||
.transpose()?,
|
||||
offset: query
|
||||
.offset
|
||||
.map(|offset| {
|
||||
i64::try_from(offset).map_err(|_| QueryConversionError::Offset(offset))
|
||||
})
|
||||
.transpose()?,
|
||||
},
|
||||
)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect())
|
||||
let query = db::Query {
|
||||
user_id: query.user_id,
|
||||
limit: query
|
||||
.limit
|
||||
.map(|limit| i64::try_from(limit).map_err(|_| QueryConversionError::Limit(limit)))
|
||||
.transpose()?,
|
||||
offset: query
|
||||
.offset
|
||||
.map(|offset| i64::try_from(offset).map_err(|_| QueryConversionError::Offset(offset)))
|
||||
.transpose()?,
|
||||
};
|
||||
|
||||
let apps = db::list(executor, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
Ok(apps)
|
||||
}
|
||||
|
||||
pub async fn get_by_id<'e, E>(
|
||||
@@ -46,21 +46,25 @@ pub async fn get_by_id<'e, E>(
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::get_by_id(executor, user_id, id).await?.map(decode))
|
||||
let app = db::get_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(app)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upsert<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let upsert = db::upsert(executor, encode(app))
|
||||
let app = db::upsert(executor, encode(app))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?;
|
||||
.map_err(classify_upsert_error)?
|
||||
.try_map(decode)?
|
||||
.into();
|
||||
|
||||
Ok(match upsert {
|
||||
crate::database::dtos::Upsert::Created(row) => Upsert::Created(decode(row)),
|
||||
crate::database::dtos::Upsert::Updated(row) => Upsert::Updated(decode(row)),
|
||||
})
|
||||
Ok(app)
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
@@ -85,14 +89,16 @@ pub async fn delete_by_id<'e, E>(
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::delete_by_id(executor, user_id, id).await?.into())
|
||||
let result = db::delete_by_id(executor, user_id, id).await?.into();
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn decode(row: UserAppRow) -> UserApp {
|
||||
UserApp {
|
||||
fn decode(row: UserAppRow) -> Result<UserApp, DecodeError> {
|
||||
Ok(UserApp {
|
||||
id: row.id,
|
||||
user_id: row.user_id,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn encode(app: UserApp) -> UserAppRow {
|
||||
@@ -143,7 +149,7 @@ mod tests {
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let (id, user_id) = (row.id, row.user_id);
|
||||
let app = decode(row);
|
||||
let app = decode(row).unwrap();
|
||||
|
||||
assert_eq!(app.id, id);
|
||||
assert_eq!(app.user_id, user_id);
|
||||
@@ -151,7 +157,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let app = decode(row());
|
||||
assert_eq!(decode(encode(app.clone())), app);
|
||||
let app = decode(row()).unwrap();
|
||||
let round_trip = decode(encode(app.clone())).unwrap();
|
||||
assert_eq!(round_trip, app);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user