Moved domain logic out of the database layer, to the user app route.
This commit is contained in:
@@ -14,6 +14,20 @@ impl<T> From<database::dtos::Upsert<T>> for Upsert<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Upserted<T> {
|
||||
pub current: T,
|
||||
pub previous: Option<T>,
|
||||
}
|
||||
|
||||
impl<T> From<database::dtos::Upserted<T>> for Upserted<T> {
|
||||
fn from(value: database::dtos::Upserted<T>) -> Self {
|
||||
Self {
|
||||
current: value.current,
|
||||
previous: value.previous,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Delete {
|
||||
Deleted,
|
||||
NotFound,
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{QueryConversionError, ReferenceRelation, RepositoryError, dtos::Upsert};
|
||||
use super::{QueryConversionError, RepositoryError, dtos::Upserted};
|
||||
use crate::{
|
||||
database::{
|
||||
Constraint, DatabaseError,
|
||||
dtos::{GuardedUpsert, UserAppRow},
|
||||
user_apps as db,
|
||||
},
|
||||
database::{Constraint, DatabaseError, dtos::UserAppRow, user_apps as db},
|
||||
domain::UserApp,
|
||||
repository::DecodeError,
|
||||
};
|
||||
@@ -42,18 +38,11 @@ where
|
||||
Ok(apps)
|
||||
}
|
||||
|
||||
pub async fn get_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserApp>, RepositoryError>
|
||||
pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let app = db::get_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
let app = db::get_by_id(executor, id).await?.map(decode).transpose()?;
|
||||
|
||||
Ok(app)
|
||||
}
|
||||
@@ -70,27 +59,17 @@ where
|
||||
Ok(owner)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upsert<UserApp>, RepositoryError>
|
||||
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upserted<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let outcome = 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();
|
||||
|
||||
map_upsert(outcome)
|
||||
}
|
||||
|
||||
fn map_upsert(outcome: GuardedUpsert<UserAppRow>) -> Result<Upsert<UserApp>, RepositoryError> {
|
||||
Ok(match outcome.try_map(decode)? {
|
||||
GuardedUpsert::Created(app) => Upsert::Created(app),
|
||||
GuardedUpsert::Updated(app) => Upsert::Updated(app),
|
||||
GuardedUpsert::Rejected => {
|
||||
return Err(RepositoryError::OwnershipConflict {
|
||||
relation: ReferenceRelation::UserApp,
|
||||
});
|
||||
}
|
||||
})
|
||||
Ok(app)
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
@@ -107,17 +86,16 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<super::dtos::Delete, RepositoryError>
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::delete_by_id(executor, user_id, id).await?.into();
|
||||
let app = db::delete_by_id(executor, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(result)
|
||||
Ok(app)
|
||||
}
|
||||
|
||||
fn decode(row: UserAppRow) -> Result<UserApp, DecodeError> {
|
||||
@@ -164,41 +142,6 @@ mod tests {
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maps_created_upsert() {
|
||||
let row = row();
|
||||
let id = row.id;
|
||||
|
||||
let Ok(Upsert::Created(app)) = map_upsert(GuardedUpsert::Created(row)) else {
|
||||
panic!("expected created upsert");
|
||||
};
|
||||
assert_eq!(app.id, id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maps_updated_upsert() {
|
||||
let row = row();
|
||||
let id = row.id;
|
||||
|
||||
let Ok(Upsert::Updated(app)) = map_upsert(GuardedUpsert::Updated(row)) else {
|
||||
panic!("expected updated upsert");
|
||||
};
|
||||
assert_eq!(app.id, id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maps_rejected_upsert_to_ownership_conflict() {
|
||||
let Err(error) = map_upsert(GuardedUpsert::Rejected) else {
|
||||
panic!("expected ownership conflict");
|
||||
};
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::OwnershipConflict {
|
||||
relation: ReferenceRelation::UserApp,
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
fn row() -> UserAppRow {
|
||||
UserAppRow {
|
||||
id: Uuid::new_v4(),
|
||||
|
||||
Reference in New Issue
Block a user