Standardized the upsert and delete pattern across the repo.
This commit is contained in:
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{AccountRow, Delete, Upsert},
|
||||
dtos::{AccountRow, Upsert},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -85,6 +85,7 @@ where
|
||||
name = EXCLUDED.name
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
COALESCE(old_row.name, new_row.name) AS "old_name!",
|
||||
new_row.id AS "id!",
|
||||
new_row.name AS "name!"
|
||||
"#,
|
||||
@@ -95,30 +96,33 @@ where
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let account = AccountRow {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
};
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
let previous = row.old_id.map(|id| AccountRow {
|
||||
id,
|
||||
name: row.old_name,
|
||||
});
|
||||
|
||||
if row.old_id.is_some() {
|
||||
Ok(Upsert::Updated(account))
|
||||
} else {
|
||||
Ok(Upsert::Created(account))
|
||||
}
|
||||
Ok(Upsert {
|
||||
previous,
|
||||
current: AccountRow {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<AccountRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!("DELETE FROM accounts WHERE id = $1 RETURNING id", id)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
} else {
|
||||
Delete::NotFound
|
||||
})
|
||||
sqlx::query_as!(
|
||||
AccountRow,
|
||||
"DELETE FROM accounts WHERE id = $1 RETURNING id, name",
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -1,25 +1,11 @@
|
||||
pub enum Upsert<T> {
|
||||
Created(T),
|
||||
Updated(T),
|
||||
}
|
||||
|
||||
impl<T> Upsert<T> {
|
||||
pub fn try_map<U, E>(self, f: impl FnOnce(T) -> Result<U, E>) -> Result<Upsert<U>, E> {
|
||||
Ok(match self {
|
||||
Self::Created(value) => Upsert::Created(f(value)?),
|
||||
Self::Updated(value) => Upsert::Updated(f(value)?),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Upserted<T> {
|
||||
pub struct Upsert<T> {
|
||||
pub current: T,
|
||||
pub previous: Option<T>,
|
||||
}
|
||||
|
||||
impl<T> Upserted<T> {
|
||||
pub fn try_map<U, E>(self, mut f: impl FnMut(T) -> Result<U, E>) -> Result<Upserted<U>, E> {
|
||||
Ok(Upserted {
|
||||
impl<T> Upsert<T> {
|
||||
pub fn try_map<U, E>(self, mut f: impl FnMut(T) -> Result<U, E>) -> Result<Upsert<U>, E> {
|
||||
Ok(Upsert {
|
||||
current: f(self.current)?,
|
||||
previous: match self.previous {
|
||||
Some(value) => Some(f(value)?),
|
||||
@@ -28,8 +14,3 @@ impl<T> Upserted<T> {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Delete {
|
||||
Deleted,
|
||||
NotFound,
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Delete, RecurringTransactionRow, Upsert},
|
||||
dtos::{RecurringTransactionRow, Upsert},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -108,6 +108,15 @@ where
|
||||
weekend_adjustment = EXCLUDED.weekend_adjustment
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
COALESCE(old_row.name, new_row.name) AS "old_name!",
|
||||
COALESCE(old_row.account_id, new_row.account_id) AS "old_account_id!",
|
||||
COALESCE(old_row.amount_cents, new_row.amount_cents) AS "old_amount_cents!",
|
||||
COALESCE(old_row.direction, new_row.direction) AS "old_direction!",
|
||||
COALESCE(old_row.starts_at, new_row.starts_at) AS "old_starts_at!",
|
||||
old_row.ends_at AS "old_ends_at?",
|
||||
COALESCE(old_row.recurrence_every, new_row.recurrence_every) AS "old_recurrence_every!",
|
||||
COALESCE(old_row.recurrence_unit, new_row.recurrence_unit) AS "old_recurrence_unit!",
|
||||
old_row.weekend_adjustment AS "old_weekend_adjustment?",
|
||||
new_row.id AS "id!",
|
||||
new_row.name AS "name!",
|
||||
new_row.account_id AS "account_id!",
|
||||
@@ -134,41 +143,66 @@ where
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let transaction = RecurringTransactionRow {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
account_id: row.account_id,
|
||||
amount_cents: row.amount_cents,
|
||||
direction: row.direction,
|
||||
starts_at: row.starts_at,
|
||||
ends_at: row.ends_at,
|
||||
recurrence_every: row.recurrence_every,
|
||||
recurrence_unit: row.recurrence_unit,
|
||||
weekend_adjustment: row.weekend_adjustment,
|
||||
};
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
let previous = row.old_id.map(|id| RecurringTransactionRow {
|
||||
id,
|
||||
name: row.old_name,
|
||||
account_id: row.old_account_id,
|
||||
amount_cents: row.old_amount_cents,
|
||||
direction: row.old_direction,
|
||||
starts_at: row.old_starts_at,
|
||||
ends_at: row.old_ends_at,
|
||||
recurrence_every: row.old_recurrence_every,
|
||||
recurrence_unit: row.old_recurrence_unit,
|
||||
weekend_adjustment: row.old_weekend_adjustment,
|
||||
});
|
||||
|
||||
if row.old_id.is_some() {
|
||||
Ok(Upsert::Updated(transaction))
|
||||
} else {
|
||||
Ok(Upsert::Created(transaction))
|
||||
}
|
||||
Ok(Upsert {
|
||||
previous,
|
||||
current: RecurringTransactionRow {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
account_id: row.account_id,
|
||||
amount_cents: row.amount_cents,
|
||||
direction: row.direction,
|
||||
starts_at: row.starts_at,
|
||||
ends_at: row.ends_at,
|
||||
recurrence_every: row.recurrence_every,
|
||||
recurrence_unit: row.recurrence_unit,
|
||||
weekend_adjustment: row.weekend_adjustment,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Option<RecurringTransactionRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!(
|
||||
"DELETE FROM recurring_transactions WHERE id = $1 RETURNING id",
|
||||
sqlx::query_as!(
|
||||
RecurringTransactionRow,
|
||||
r#"
|
||||
DELETE FROM recurring_transactions
|
||||
WHERE id = $1
|
||||
RETURNING
|
||||
id,
|
||||
name,
|
||||
account_id,
|
||||
amount_cents,
|
||||
direction,
|
||||
starts_at,
|
||||
ends_at,
|
||||
recurrence_every,
|
||||
recurrence_unit,
|
||||
weekend_adjustment
|
||||
"#,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
} else {
|
||||
Delete::NotFound
|
||||
})
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Delete, TransactionRow, Upsert},
|
||||
dtos::{TransactionRow, Upsert},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -71,6 +71,11 @@ where
|
||||
description = EXCLUDED.description
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
COALESCE(old_row.account_id, new_row.account_id) AS "old_account_id!",
|
||||
COALESCE(old_row.amount_cents, new_row.amount_cents) AS "old_amount_cents!",
|
||||
COALESCE(old_row.direction, new_row.direction) AS "old_direction!",
|
||||
COALESCE(old_row.date, new_row.date) AS "old_date!",
|
||||
COALESCE(old_row.description, new_row.description) AS "old_description!",
|
||||
new_row.id AS "id!",
|
||||
new_row.account_id AS "account_id!",
|
||||
new_row.amount_cents AS "amount_cents!",
|
||||
@@ -89,34 +94,48 @@ where
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let transaction = TransactionRow {
|
||||
id: row.id,
|
||||
account_id: row.account_id,
|
||||
amount_cents: row.amount_cents,
|
||||
direction: row.direction,
|
||||
date: row.date,
|
||||
description: row.description,
|
||||
};
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
let previous = row.old_id.map(|id| TransactionRow {
|
||||
id,
|
||||
account_id: row.old_account_id,
|
||||
amount_cents: row.old_amount_cents,
|
||||
direction: row.old_direction,
|
||||
date: row.old_date,
|
||||
description: row.old_description,
|
||||
});
|
||||
|
||||
if row.old_id.is_some() {
|
||||
Ok(Upsert::Updated(transaction))
|
||||
} else {
|
||||
Ok(Upsert::Created(transaction))
|
||||
}
|
||||
Ok(Upsert {
|
||||
previous,
|
||||
current: TransactionRow {
|
||||
id: row.id,
|
||||
account_id: row.account_id,
|
||||
amount_cents: row.amount_cents,
|
||||
direction: row.direction,
|
||||
date: row.date,
|
||||
description: row.description,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Option<TransactionRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!("DELETE FROM transactions WHERE id = $1 RETURNING id", id)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
} else {
|
||||
Delete::NotFound
|
||||
})
|
||||
sqlx::query_as!(
|
||||
TransactionRow,
|
||||
r#"
|
||||
DELETE FROM transactions
|
||||
WHERE id = $1
|
||||
RETURNING id, account_id, amount_cents, direction, date, description
|
||||
"#,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Delete, Upsert, UserAppSecretRow},
|
||||
dtos::{Upsert, UserAppSecretRow},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -73,6 +73,10 @@ where
|
||||
expires_at = EXCLUDED.expires_at
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
COALESCE(old_row.app_id, new_row.app_id) AS "old_app_id!",
|
||||
COALESCE(old_row.secret_hash, new_row.secret_hash) AS "old_secret_hash!",
|
||||
COALESCE(old_row.expires_at, new_row.expires_at) AS "old_expires_at!",
|
||||
COALESCE(old_row.created_at, new_row.created_at) AS "old_created_at!",
|
||||
new_row.id AS "id!",
|
||||
new_row.app_id AS "app_id!",
|
||||
new_row.secret_hash AS "secret_hash!",
|
||||
@@ -88,18 +92,26 @@ where
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let secret = UserAppSecretRow {
|
||||
id: row.id,
|
||||
app_id: row.app_id,
|
||||
secret_hash: row.secret_hash,
|
||||
expires_at: row.expires_at,
|
||||
created_at: row.created_at,
|
||||
};
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
let previous = row.old_id.map(|id| UserAppSecretRow {
|
||||
id,
|
||||
app_id: row.old_app_id,
|
||||
secret_hash: row.old_secret_hash,
|
||||
expires_at: row.old_expires_at,
|
||||
created_at: row.old_created_at,
|
||||
});
|
||||
|
||||
Ok(if row.old_id.is_some() {
|
||||
Upsert::Updated(secret)
|
||||
} else {
|
||||
Upsert::Created(secret)
|
||||
Ok(Upsert {
|
||||
previous,
|
||||
current: UserAppSecretRow {
|
||||
id: row.id,
|
||||
app_id: row.app_id,
|
||||
secret_hash: row.secret_hash,
|
||||
expires_at: row.expires_at,
|
||||
created_at: row.created_at,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -131,22 +143,21 @@ pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
app_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Delete, DatabaseError>
|
||||
) -> Result<Option<UserAppSecretRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!(
|
||||
"DELETE FROM user_app_secrets WHERE app_id = $1 AND id = $2 RETURNING id",
|
||||
sqlx::query_as!(
|
||||
UserAppSecretRow,
|
||||
r#"
|
||||
DELETE FROM user_app_secrets
|
||||
WHERE app_id = $1 AND id = $2
|
||||
RETURNING id, app_id, secret_hash, expires_at, created_at
|
||||
"#,
|
||||
app_id,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
} else {
|
||||
Delete::NotFound
|
||||
})
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Upserted, UserAppRow},
|
||||
dtos::{Upsert, UserAppRow},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -67,7 +67,7 @@ where
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
app: UserAppRow,
|
||||
) -> Result<Upserted<UserAppRow>, DatabaseError>
|
||||
) -> Result<Upsert<UserAppRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -92,14 +92,13 @@ where
|
||||
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing for the previous row instead of having to check nullability for each old field
|
||||
// individually.
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
let previous = row.old_id.map(|id| UserAppRow {
|
||||
id,
|
||||
user_id: row.old_user_id,
|
||||
});
|
||||
|
||||
Ok(Upserted {
|
||||
Ok(Upsert {
|
||||
previous,
|
||||
current: UserAppRow {
|
||||
id: row.id,
|
||||
@@ -112,16 +111,12 @@ pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserApp
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let row = sqlx::query!(
|
||||
sqlx::query_as!(
|
||||
UserAppRow,
|
||||
"DELETE FROM user_apps WHERE id = $1 RETURNING id, user_id",
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(row.map(|row| UserAppRow {
|
||||
id: row.id,
|
||||
user_id: row.user_id,
|
||||
}))
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Delete, Upsert, UserRow},
|
||||
dtos::{Upsert, UserRow},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -69,27 +69,18 @@ where
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let user = UserRow { id: row.id };
|
||||
|
||||
if row.old_id.is_some() {
|
||||
Ok(Upsert::Updated(user))
|
||||
} else {
|
||||
Ok(Upsert::Created(user))
|
||||
}
|
||||
Ok(Upsert {
|
||||
previous: row.old_id.map(|id| UserRow { id }),
|
||||
current: UserRow { id: row.id },
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!("DELETE FROM users WHERE id = $1 RETURNING id", id)
|
||||
sqlx::query_as!(UserRow, "DELETE FROM users WHERE id = $1 RETURNING id", id)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
} else {
|
||||
Delete::NotFound
|
||||
})
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -9,10 +9,7 @@ use super::{
|
||||
};
|
||||
use crate::{
|
||||
error::AppError,
|
||||
repository::{
|
||||
self,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
repository::{self},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -76,12 +73,14 @@ async fn upsert_account(
|
||||
) -> Result<(StatusCode, Json<AccountResponse>), AppError> {
|
||||
let account = account.into_domain(id)?;
|
||||
|
||||
let (status, account) = match repository::accounts::upsert(&pool, account).await? {
|
||||
Upsert::Created(account) => (StatusCode::CREATED, account),
|
||||
Upsert::Updated(account) => (StatusCode::OK, account),
|
||||
let upserted = repository::accounts::upsert(&pool, account).await?;
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
Ok((status, Json(account.into())))
|
||||
Ok((status, Json(upserted.current.into())))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -90,8 +89,9 @@ async fn delete_account(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
match repository::accounts::delete_by_id(&pool, id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
repository::accounts::delete_by_id(&pool, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
@@ -15,10 +15,7 @@ use super::{
|
||||
use crate::{
|
||||
domain,
|
||||
error::AppError,
|
||||
repository::{
|
||||
self, RepositoryError, accounts,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
repository::{self, RepositoryError, accounts},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -91,18 +88,20 @@ async fn upsert_scheduled_transaction(
|
||||
let transaction = transaction.into_domain(id)?;
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let (status, transaction) =
|
||||
match repository::recurring_transactions::upsert(&mut *tx, transaction).await? {
|
||||
Upsert::Created(transaction) => (StatusCode::CREATED, transaction),
|
||||
Upsert::Updated(transaction) => (StatusCode::OK, transaction),
|
||||
};
|
||||
let upserted = repository::recurring_transactions::upsert(&mut *tx, transaction).await?;
|
||||
let accounts =
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&transaction)).await?;
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&upserted.current)).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
Ok((
|
||||
status,
|
||||
Json(create_response(transaction, &accounts, Utc::now())?),
|
||||
Json(create_response(upserted.current, &accounts, Utc::now())?),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -112,10 +111,11 @@ async fn delete_scheduled_transaction(
|
||||
State(pool): State<PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
match repository::recurring_transactions::delete_by_id(&pool, id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
repository::recurring_transactions::delete_by_id(&pool, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn get_accounts_for_transactions(
|
||||
|
||||
@@ -12,10 +12,7 @@ use super::{
|
||||
use crate::{
|
||||
domain,
|
||||
error::AppError,
|
||||
repository::{
|
||||
self, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
repository::{self, RepositoryError},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -86,16 +83,18 @@ async fn upsert_transaction(
|
||||
let transaction = transaction.into_domain(id)?;
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let (status, transaction) =
|
||||
match repository::transactions::upsert(&mut *tx, transaction).await? {
|
||||
Upsert::Created(transaction) => (StatusCode::CREATED, transaction),
|
||||
Upsert::Updated(transaction) => (StatusCode::OK, transaction),
|
||||
};
|
||||
let upserted = repository::transactions::upsert(&mut *tx, transaction).await?;
|
||||
let accounts =
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&transaction)).await?;
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&upserted.current)).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
Ok((status, Json(create_response(transaction, &accounts)?)))
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
Ok((status, Json(create_response(upserted.current, &accounts)?)))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -104,10 +103,11 @@ async fn delete_transaction(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
match repository::transactions::delete_by_id(&pool, id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
repository::transactions::delete_by_id(&pool, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn get_accounts_for_transactions(
|
||||
|
||||
@@ -13,10 +13,7 @@ use crate::{
|
||||
crypto::{generate_secret, hash_secret},
|
||||
domain::UserAppSecret,
|
||||
error::AppError,
|
||||
repository::{
|
||||
self, ReferenceRelation, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
repository::{self, ReferenceRelation, RepositoryError},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -102,13 +99,15 @@ async fn upsert_user_app_secret(
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||
let upsert = repository::user_app_secrets::upsert(&mut *tx, secret).await?;
|
||||
let upserted = repository::user_app_secrets::upsert(&mut *tx, secret).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
let (status, secret) = match upsert {
|
||||
Upsert::Created(secret) => (StatusCode::CREATED, secret),
|
||||
Upsert::Updated(secret) => (StatusCode::OK, secret),
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
let secret = upserted.current;
|
||||
|
||||
Ok((
|
||||
status,
|
||||
@@ -127,13 +126,12 @@ async fn delete_user_app_secret(
|
||||
) -> Result<StatusCode, AppError> {
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||
let result = repository::user_app_secrets::delete_by_id(&mut *tx, app_id, secret_id).await?;
|
||||
let deleted = repository::user_app_secrets::delete_by_id(&mut *tx, app_id, secret_id).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
match result {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
deleted.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn ensure_app_ownership(
|
||||
|
||||
+12
-12
@@ -9,10 +9,7 @@ use super::{
|
||||
};
|
||||
use crate::{
|
||||
error::AppError,
|
||||
repository::{
|
||||
self,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
repository::{self},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -74,12 +71,14 @@ async fn upsert_user(
|
||||
) -> Result<(StatusCode, Json<UserResponse>), AppError> {
|
||||
let user = user.into_domain(id)?;
|
||||
|
||||
let (status, user) = match repository::users::upsert(&pool, user).await? {
|
||||
Upsert::Created(user) => (StatusCode::CREATED, user),
|
||||
Upsert::Updated(user) => (StatusCode::OK, user),
|
||||
let upserted = repository::users::upsert(&pool, user).await?;
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
Ok((status, Json(user.into())))
|
||||
Ok((status, Json(upserted.current.into())))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -88,8 +87,9 @@ async fn delete_user(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
match repository::users::delete_by_id(&pool, id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
repository::users::delete_by_id(&pool, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user