189 lines
4.6 KiB
Rust
189 lines
4.6 KiB
Rust
use uuid::Uuid;
|
|
|
|
use super::{QueryConversionError, RepositoryError, dtos::Upsert};
|
|
use crate::{
|
|
database::{self, Constraint, DatabaseError, dtos::UserAppRow},
|
|
domain::UserApp,
|
|
repository::DecodeError,
|
|
};
|
|
|
|
pub struct Query {
|
|
pub limit: Option<u64>,
|
|
pub offset: Option<u64>,
|
|
}
|
|
|
|
pub async fn list<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
query: Query,
|
|
) -> Result<Vec<UserApp>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let query = database::user_apps::Query {
|
|
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 = database::user_apps::list(executor, user_id, query)
|
|
.await?
|
|
.into_iter()
|
|
.map(decode)
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
Ok(apps)
|
|
}
|
|
|
|
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>,
|
|
{
|
|
let app = database::user_apps::get_by_id(executor, user_id, id)
|
|
.await?
|
|
.map(decode)
|
|
.transpose()?;
|
|
|
|
Ok(app)
|
|
}
|
|
|
|
pub async fn get_by_id_unscoped<'e, E>(
|
|
executor: E,
|
|
id: Uuid,
|
|
) -> Result<Option<UserApp>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let app = database::user_apps::get_by_id_unscoped(executor, id)
|
|
.await?
|
|
.map(decode)
|
|
.transpose()?;
|
|
|
|
Ok(app)
|
|
}
|
|
|
|
pub async fn upsert<'e, E>(
|
|
executor: E,
|
|
app: UserApp,
|
|
) -> Result<Option<Upsert<UserApp>>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let app = database::user_apps::upsert(executor, encode(app))
|
|
.await
|
|
.map_err(classify_upsert_error)?
|
|
.map(|upserted| upserted.try_map(decode))
|
|
.transpose()?
|
|
.map(Into::into);
|
|
|
|
Ok(app)
|
|
}
|
|
|
|
pub async fn delete_by_id<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
id: Uuid,
|
|
) -> Result<Option<UserApp>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let app = database::user_apps::delete_by_id(executor, user_id, id)
|
|
.await?
|
|
.map(decode)
|
|
.transpose()?;
|
|
|
|
Ok(app)
|
|
}
|
|
|
|
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),
|
|
}
|
|
}
|
|
|
|
fn decode(row: UserAppRow) -> Result<UserApp, DecodeError> {
|
|
Ok(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).unwrap();
|
|
|
|
assert_eq!(app.id, id);
|
|
assert_eq!(app.user_id, user_id);
|
|
}
|
|
|
|
#[test]
|
|
fn round_trips_through_rows() {
|
|
let app = decode(row()).unwrap();
|
|
let round_trip = decode(encode(app.clone())).unwrap();
|
|
assert_eq!(round_trip, app);
|
|
}
|
|
}
|