Repo layer refactor.
This commit is contained in:
@@ -6,6 +6,7 @@ use super::{
|
||||
use crate::{
|
||||
database::{Constraint, DatabaseError, dtos::UserAppSecretRow, user_app_secrets as db},
|
||||
domain::UserAppSecret,
|
||||
repository::DecodeError,
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -18,26 +19,25 @@ pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserAppSecret>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::list(
|
||||
executor,
|
||||
db::Query {
|
||||
app_id: query.app_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 {
|
||||
app_id: query.app_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 secrets = db::list(executor, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<Vec<_>, DecodeError>>()?;
|
||||
|
||||
Ok(secrets)
|
||||
}
|
||||
|
||||
pub async fn get_by_id<'e, E>(
|
||||
@@ -48,7 +48,12 @@ pub async fn get_by_id<'e, E>(
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::get_by_id(executor, app_id, id).await?.map(decode))
|
||||
let secret = db::get_by_id(executor, app_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(secret)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
@@ -58,14 +63,13 @@ pub async fn upsert<'e, E>(
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let upsert = db::upsert(executor, encode(secret))
|
||||
let secret = db::upsert(executor, encode(secret))
|
||||
.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(secret)
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
@@ -90,7 +94,9 @@ pub async fn verify<'e, E>(
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::verify(executor, app_id, secret_hash).await?)
|
||||
let result = db::verify(executor, app_id, secret_hash).await?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
@@ -101,17 +107,19 @@ pub async fn delete_by_id<'e, E>(
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::delete_by_id(executor, app_id, id).await?.into())
|
||||
let result = db::delete_by_id(executor, app_id, id).await?.into();
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn decode(row: UserAppSecretRow) -> UserAppSecret {
|
||||
UserAppSecret {
|
||||
fn decode(row: UserAppSecretRow) -> Result<UserAppSecret, DecodeError> {
|
||||
Ok(UserAppSecret {
|
||||
id: row.id,
|
||||
app_id: row.app_id,
|
||||
secret_hash: row.secret_hash,
|
||||
expires_at: row.expires_at,
|
||||
created_at: row.created_at,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn encode(secret: UserAppSecret) -> UserAppSecretRow {
|
||||
@@ -168,12 +176,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let secret = decode(row());
|
||||
let round_trip = decode(encode(secret.clone()));
|
||||
|
||||
assert_eq!(round_trip.id, secret.id);
|
||||
assert_eq!(round_trip.app_id, secret.app_id);
|
||||
assert_eq!(round_trip.secret_hash, secret.secret_hash);
|
||||
assert_eq!(round_trip.expires_at, secret.expires_at);
|
||||
let secret = decode(row()).unwrap();
|
||||
let round_trip = decode(encode(secret.clone())).unwrap();
|
||||
assert_eq!(round_trip, secret);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user