Standardized the upsert and delete pattern across the repo.
This commit is contained in:
@@ -4,7 +4,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
dtos::Upsert,
|
||||
};
|
||||
use crate::{
|
||||
database::{Constraint, DatabaseError, accounts as db, dtos::AccountRow},
|
||||
@@ -91,16 +91,17 @@ where
|
||||
Ok(account)
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<Account>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::delete_by_id(executor, id)
|
||||
let account = db::delete_by_id(executor, id)
|
||||
.await
|
||||
.map_err(classify_delete_error)?
|
||||
.into();
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(result)
|
||||
Ok(account)
|
||||
}
|
||||
|
||||
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
|
||||
@@ -1,43 +1,15 @@
|
||||
use crate::database;
|
||||
|
||||
pub enum Upsert<T> {
|
||||
Created(T),
|
||||
Updated(T),
|
||||
}
|
||||
|
||||
impl<T> From<database::dtos::Upsert<T>> for Upsert<T> {
|
||||
fn from(value: database::dtos::Upsert<T>) -> Self {
|
||||
match value {
|
||||
database::dtos::Upsert::Created(v) => Upsert::Created(v),
|
||||
database::dtos::Upsert::Updated(v) => Upsert::Updated(v),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Upserted<T> {
|
||||
pub struct Upsert<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 {
|
||||
impl<T> From<database::dtos::Upsert<T>> for Upsert<T> {
|
||||
fn from(value: database::dtos::Upsert<T>) -> Self {
|
||||
Self {
|
||||
current: value.current,
|
||||
previous: value.previous,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Delete {
|
||||
Deleted,
|
||||
NotFound,
|
||||
}
|
||||
|
||||
impl From<database::dtos::Delete> for Delete {
|
||||
fn from(value: database::dtos::Delete) -> Self {
|
||||
match value {
|
||||
database::dtos::Delete::Deleted => Delete::Deleted,
|
||||
database::dtos::Delete::NotFound => Delete::NotFound,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
dtos::Upsert,
|
||||
encoding::{
|
||||
decode_direction, decode_recurrence_unit, decode_weekend_adjustment, encode_direction,
|
||||
encode_recurrence_unit, encode_weekend_adjustment,
|
||||
@@ -94,13 +94,19 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Option<RecurringTransaction>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::delete_by_id(executor, id).await?.into();
|
||||
let transaction = db::delete_by_id(executor, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(result)
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
fn decode(row: RecurringTransactionRow) -> Result<RecurringTransaction, DecodeError> {
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
dtos::Upsert,
|
||||
encoding::{decode_direction, encode_direction},
|
||||
};
|
||||
use crate::{
|
||||
@@ -78,13 +78,19 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Option<Transaction>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::delete_by_id(executor, id).await?.into();
|
||||
let transaction = db::delete_by_id(executor, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(result)
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
fn decode(row: TransactionRow) -> Result<Transaction, DecodeError> {
|
||||
|
||||
@@ -103,13 +103,16 @@ pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
app_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<super::dtos::Delete, RepositoryError>
|
||||
) -> Result<Option<UserAppSecret>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::delete_by_id(executor, app_id, id).await?.into();
|
||||
let secret = db::delete_by_id(executor, app_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(result)
|
||||
Ok(secret)
|
||||
}
|
||||
|
||||
fn decode(row: UserAppSecretRow) -> Result<UserAppSecret, DecodeError> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{QueryConversionError, RepositoryError, dtos::Upserted};
|
||||
use super::{QueryConversionError, RepositoryError, dtos::Upsert};
|
||||
use crate::{
|
||||
database::{Constraint, DatabaseError, dtos::UserAppRow, user_apps as db},
|
||||
domain::UserApp,
|
||||
@@ -59,7 +59,7 @@ where
|
||||
Ok(owner)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upserted<UserApp>, RepositoryError>
|
||||
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upsert<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
};
|
||||
use super::{DecodeError, QueryConversionError, RepositoryError, dtos::Upsert};
|
||||
use crate::{
|
||||
database::{DatabaseError, dtos::UserRow, users as db},
|
||||
domain::User,
|
||||
@@ -59,16 +56,17 @@ where
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<User>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::delete_by_id(executor, id)
|
||||
let user = db::delete_by_id(executor, id)
|
||||
.await
|
||||
.map_err(classify_delete_error)?
|
||||
.into();
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(result)
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
|
||||
Reference in New Issue
Block a user