231 lines
5.7 KiB
Rust
231 lines
5.7 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use uuid::Uuid;
|
|
|
|
use super::{
|
|
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
|
dtos::Upsert,
|
|
};
|
|
use crate::{
|
|
database::{Constraint, DatabaseError, accounts as db, dtos::AccountRow},
|
|
domain::Account,
|
|
};
|
|
|
|
pub struct Query {
|
|
pub name: Option<String>,
|
|
pub limit: Option<u64>,
|
|
pub offset: Option<u64>,
|
|
}
|
|
|
|
pub async fn list<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
query: Query,
|
|
) -> Result<Vec<Account>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let query = db::Query {
|
|
name: query.name,
|
|
limit: query
|
|
.limit
|
|
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
|
|
.transpose()?,
|
|
offset: query
|
|
.offset
|
|
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
|
.transpose()?,
|
|
};
|
|
|
|
let accounts = db::list(executor, user_id, query)
|
|
.await?
|
|
.into_iter()
|
|
.map(decode)
|
|
.collect::<Result<_, _>>()?;
|
|
|
|
Ok(accounts)
|
|
}
|
|
|
|
pub async fn get_by_id<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
id: Uuid,
|
|
) -> Result<Option<Account>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let account = db::get_by_id(executor, user_id, id)
|
|
.await?
|
|
.map(decode)
|
|
.transpose()?;
|
|
|
|
Ok(account)
|
|
}
|
|
|
|
pub async fn get_by_ids<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
ids: &[Uuid],
|
|
) -> Result<HashMap<Uuid, Account>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
if ids.is_empty() {
|
|
return Ok(HashMap::new());
|
|
}
|
|
|
|
let accounts = db::get_by_ids(executor, user_id, ids)
|
|
.await?
|
|
.into_iter()
|
|
.map(decode)
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
let account_map = accounts
|
|
.into_iter()
|
|
.map(|account| (account.id, account))
|
|
.collect();
|
|
|
|
Ok(account_map)
|
|
}
|
|
|
|
pub async fn upsert<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
account: Account,
|
|
) -> Result<Option<Upsert<Account>>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let upserted = db::upsert(executor, user_id, encode(account))
|
|
.await?
|
|
.map(|upserted| upserted.try_map(decode))
|
|
.transpose()?
|
|
.map(Into::into);
|
|
|
|
Ok(upserted)
|
|
}
|
|
|
|
pub async fn delete_by_id<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
id: Uuid,
|
|
) -> Result<Option<Account>, RepositoryError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let account = db::delete_by_id(executor, user_id, id)
|
|
.await
|
|
.map_err(classify_delete_error)?
|
|
.map(decode)
|
|
.transpose()?;
|
|
|
|
Ok(account)
|
|
}
|
|
|
|
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
|
match &error {
|
|
DatabaseError::ForeignKeyViolation {
|
|
constraint: Constraint::TransactionAccount,
|
|
..
|
|
} => RepositoryError::ReferenceViolation {
|
|
relation: ReferenceRelation::Transaction,
|
|
kind: ReferenceViolationKind::StillReferenced,
|
|
source: error,
|
|
},
|
|
DatabaseError::ForeignKeyViolation {
|
|
constraint: Constraint::RecurringTransactionAccount,
|
|
..
|
|
} => RepositoryError::ReferenceViolation {
|
|
relation: ReferenceRelation::RecurringTransaction,
|
|
kind: ReferenceViolationKind::StillReferenced,
|
|
source: error,
|
|
},
|
|
_ => RepositoryError::Database(error),
|
|
}
|
|
}
|
|
|
|
fn decode(row: AccountRow) -> Result<Account, DecodeError> {
|
|
Ok(Account {
|
|
id: row.id,
|
|
name: row.name,
|
|
})
|
|
}
|
|
|
|
fn encode(account: Account) -> AccountRow {
|
|
AccountRow {
|
|
id: account.id,
|
|
name: account.name,
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use uuid::Uuid;
|
|
|
|
use super::*;
|
|
|
|
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
|
DatabaseError::ForeignKeyViolation {
|
|
constraint,
|
|
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn classifies_account_delete_foreign_key_constraint() {
|
|
let error = classify_delete_error(foreign_key_violation(
|
|
Constraint::RecurringTransactionAccount,
|
|
));
|
|
assert!(matches!(
|
|
&error,
|
|
RepositoryError::ReferenceViolation {
|
|
relation: ReferenceRelation::RecurringTransaction,
|
|
kind: ReferenceViolationKind::StillReferenced,
|
|
..
|
|
}
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn classifies_transaction_account_delete_foreign_key_constraint() {
|
|
let error = classify_delete_error(foreign_key_violation(Constraint::TransactionAccount));
|
|
assert!(matches!(
|
|
error,
|
|
RepositoryError::ReferenceViolation {
|
|
relation: ReferenceRelation::Transaction,
|
|
kind: ReferenceViolationKind::StillReferenced,
|
|
..
|
|
}
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn keeps_unknown_account_delete_foreign_keys_generic() {
|
|
let error = classify_delete_error(foreign_key_violation(Constraint::Unnamed));
|
|
assert!(matches!(error, RepositoryError::Database(_)));
|
|
}
|
|
|
|
fn row() -> AccountRow {
|
|
AccountRow {
|
|
id: Uuid::new_v4(),
|
|
name: "Savings".to_owned(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn decodes_valid_rows() {
|
|
let row = row();
|
|
let id = row.id;
|
|
let account = decode(row).unwrap();
|
|
assert_eq!(account.id, id);
|
|
assert_eq!(account.name, "Savings");
|
|
}
|
|
|
|
#[test]
|
|
fn round_trips_through_rows() {
|
|
let account = decode(row()).unwrap();
|
|
let round_trip = decode(encode(account.clone())).unwrap();
|
|
assert_eq!(round_trip, account);
|
|
}
|
|
}
|