158 lines
4.1 KiB
Rust
158 lines
4.1 KiB
Rust
use uuid::Uuid;
|
|
|
|
use super::{QueryConversionError, RepositoryError, dtos::Upsert};
|
|
use crate::{
|
|
database::{Constraint, DatabaseError, dtos::UserAppRow, user_apps as db},
|
|
domain::UserApp,
|
|
};
|
|
|
|
pub struct Query {
|
|
pub user_id: Uuid,
|
|
pub limit: Option<u64>,
|
|
pub offset: Option<u64>,
|
|
}
|
|
|
|
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserApp>, RepositoryError>
|
|
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())
|
|
}
|
|
|
|
pub async fn get_by_id<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
id: Uuid,
|
|
) -> Result<Option<UserApp>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
Ok(db::get_by_id(executor, user_id, id).await?.map(decode))
|
|
}
|
|
|
|
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))
|
|
.await
|
|
.map_err(classify_upsert_error)?;
|
|
|
|
Ok(match upsert {
|
|
crate::database::dtos::Upsert::Created(row) => Upsert::Created(decode(row)),
|
|
crate::database::dtos::Upsert::Updated(row) => Upsert::Updated(decode(row)),
|
|
})
|
|
}
|
|
|
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
|
match &error {
|
|
DatabaseError::ForeignKeyViolation {
|
|
constraint: Constraint::UserAppUser,
|
|
..
|
|
} => RepositoryError::ReferenceViolation {
|
|
relation: super::ReferenceRelation::User,
|
|
kind: super::ReferenceViolationKind::Missing,
|
|
source: error,
|
|
},
|
|
_ => RepositoryError::Database(error),
|
|
}
|
|
}
|
|
|
|
pub async fn delete_by_id<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
id: Uuid,
|
|
) -> Result<super::dtos::Delete, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
Ok(db::delete_by_id(executor, user_id, id).await?.into())
|
|
}
|
|
|
|
fn decode(row: UserAppRow) -> UserApp {
|
|
UserApp {
|
|
id: row.id,
|
|
user_id: row.user_id,
|
|
}
|
|
}
|
|
|
|
fn encode(app: UserApp) -> UserAppRow {
|
|
UserAppRow {
|
|
id: app.id,
|
|
user_id: app.user_id,
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
|
DatabaseError::ForeignKeyViolation {
|
|
constraint,
|
|
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn classifies_user_app_upsert_foreign_key_violation() {
|
|
let error = classify_upsert_error(foreign_key_violation(Constraint::UserAppUser));
|
|
assert!(matches!(
|
|
error,
|
|
RepositoryError::ReferenceViolation {
|
|
relation: super::super::ReferenceRelation::User,
|
|
kind: super::super::ReferenceViolationKind::Missing,
|
|
..
|
|
}
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn keeps_unknown_upsert_foreign_keys_generic() {
|
|
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
|
assert!(matches!(error, RepositoryError::Database(_)));
|
|
}
|
|
|
|
fn row() -> UserAppRow {
|
|
UserAppRow {
|
|
id: Uuid::new_v4(),
|
|
user_id: Uuid::new_v4(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn decodes_valid_rows() {
|
|
let row = row();
|
|
let (id, user_id) = (row.id, row.user_id);
|
|
let app = decode(row);
|
|
|
|
assert_eq!(app.id, id);
|
|
assert_eq!(app.user_id, user_id);
|
|
}
|
|
|
|
#[test]
|
|
fn round_trips_through_rows() {
|
|
let app = decode(row());
|
|
assert_eq!(decode(encode(app.clone())), app);
|
|
}
|
|
}
|