Split up files and did some consistency work.

This commit is contained in:
2026-10-18 14:53:20 +02:00
parent ed5ac7c57d
commit 68cb3d5e31
22 changed files with 506 additions and 485 deletions
+110
View File
@@ -0,0 +1,110 @@
use super::{ReferenceRelation, ReferenceViolationKind, RepositoryError};
use crate::{
database::{self, Constraint, DatabaseError, dtos::AccessTokenRow},
domain::{HashedAccessToken, HashedSecret},
repository::dtos::Upsert,
};
pub async fn get_by_hash<'e, E>(
executor: E,
secret_hash: &HashedSecret,
) -> Result<Option<HashedAccessToken>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::access_tokens::get_by_hash(executor, secret_hash.hash())
.await?
.map(decode_access_token)
.transpose()?;
Ok(token)
}
pub async fn get_by_refresh_hash<'e, E>(
executor: E,
secret_hash: &HashedSecret,
) -> Result<Option<HashedAccessToken>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::access_tokens::get_by_refresh_secret_hash(executor, secret_hash.hash())
.await?
.map(decode_access_token)
.transpose()?;
Ok(token)
}
pub async fn upsert<'e, E>(
executor: E,
access_token: HashedAccessToken,
) -> Result<Option<Upsert<HashedAccessToken>>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::access_tokens::upsert(executor, encode_access_token(access_token))
.await
.map_err(classify_upsert_error)?
.map(|upserted| upserted.try_map(decode_access_token))
.transpose()?
.map(Into::into);
Ok(token)
}
pub async fn delete_by_hash<'e, E>(
executor: E,
secret_hash: &HashedSecret,
) -> Result<Option<HashedAccessToken>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::access_tokens::delete_by_hash(executor, secret_hash.hash())
.await?
.map(decode_access_token)
.transpose()?;
Ok(token)
}
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
let relation = match &error {
DatabaseError::ForeignKeyViolation {
constraint: Constraint::AccessTokenUser,
..
} => ReferenceRelation::User,
DatabaseError::ForeignKeyViolation {
constraint: Constraint::AccessTokenUserApp,
..
} => ReferenceRelation::UserApp,
_ => return RepositoryError::Database(error),
};
RepositoryError::ReferenceViolation {
relation,
kind: ReferenceViolationKind::Missing,
source: error,
}
}
fn decode_access_token(row: AccessTokenRow) -> Result<HashedAccessToken, RepositoryError> {
Ok(HashedAccessToken {
id: row.id,
user_id: row.user_id,
user_app_id: row.user_app_id,
secret_hash: HashedSecret::from(row.secret_hash),
expires_at: row.expires_at,
issued_at: row.issued_at,
})
}
fn encode_access_token(token: HashedAccessToken) -> AccessTokenRow {
AccessTokenRow {
id: token.id,
user_id: token.user_id,
user_app_id: token.user_app_id,
secret_hash: token.secret_hash.hash().to_vec(),
expires_at: token.expires_at,
issued_at: token.issued_at,
}
}
+18 -18
View File
@@ -95,6 +95,24 @@ where
Ok(decode(row)?)
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
account_id: Uuid,
id: Uuid,
) -> Result<Option<AccountMember>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let member = database::account_members::delete_by_id(executor, user_id, account_id, id)
.await
.map_err(classify_delete_error)?
.map(decode)
.transpose()?;
Ok(member)
}
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
match &error {
DatabaseError::ForeignKeyViolation {
@@ -124,24 +142,6 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
}
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
account_id: Uuid,
id: Uuid,
) -> Result<Option<AccountMember>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let member = database::account_members::delete_by_id(executor, user_id, account_id, id)
.await
.map_err(classify_delete_error)?
.map(decode)
.transpose()?;
Ok(member)
}
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
match &error {
DatabaseError::CheckViolation {
+16 -16
View File
@@ -80,6 +80,22 @@ where
Ok(upserted)
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<RecurringTransaction>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let transaction = database::recurring_transactions::delete_by_id(executor, user_id, id)
.await?
.map(decode)
.transpose()?;
Ok(transaction)
}
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
let relation = match &error {
DatabaseError::ForeignKeyViolation {
@@ -99,22 +115,6 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
}
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<RecurringTransaction>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let transaction = database::recurring_transactions::delete_by_id(executor, user_id, id)
.await?
.map(decode)
.transpose()?;
Ok(transaction)
}
fn decode(row: RecurringTransactionRow) -> Result<RecurringTransaction, DecodeError> {
let recurrence = Recurrence {
every: RecurrenceInterval::try_from(row.recurrence_every)?,
+89
View File
@@ -0,0 +1,89 @@
use super::{ReferenceRelation, ReferenceViolationKind, RepositoryError};
use crate::{
database::{self, Constraint, DatabaseError, dtos::RefreshTokenRow},
domain::{HashedRefreshToken, HashedSecret},
repository::dtos::Upsert,
};
pub async fn get_by_hash<'e, E>(
executor: E,
secret_hash: &HashedSecret,
) -> Result<Option<HashedRefreshToken>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::refresh_tokens::get_by_hash(executor, secret_hash.hash())
.await?
.map(decode_refresh_token)
.transpose()?;
Ok(token)
}
pub async fn upsert<'e, E>(
executor: E,
refresh_token: HashedRefreshToken,
) -> Result<Option<Upsert<HashedRefreshToken>>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::refresh_tokens::upsert(executor, encode_refresh_token(refresh_token))
.await
.map_err(classify_upsert_error)?
.map(|upserted| upserted.try_map(decode_refresh_token))
.transpose()?
.map(Into::into);
Ok(token)
}
pub async fn delete_by_hash<'e, E>(
executor: E,
secret_hash: &HashedSecret,
) -> Result<Option<HashedRefreshToken>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::refresh_tokens::delete_by_hash(executor, secret_hash.hash())
.await?
.map(decode_refresh_token)
.transpose()?;
Ok(token)
}
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
let relation = match &error {
DatabaseError::ForeignKeyViolation {
constraint: Constraint::RefreshTokenAccessToken,
..
} => ReferenceRelation::AccessToken,
_ => return RepositoryError::Database(error),
};
RepositoryError::ReferenceViolation {
relation,
kind: ReferenceViolationKind::Missing,
source: error,
}
}
fn decode_refresh_token(row: RefreshTokenRow) -> Result<HashedRefreshToken, RepositoryError> {
Ok(HashedRefreshToken {
id: row.id,
token_id: row.access_token_id,
secret_hash: HashedSecret::from(row.secret_hash),
expires_at: row.expires_at,
issued_at: row.issued_at,
})
}
fn encode_refresh_token(token: HashedRefreshToken) -> RefreshTokenRow {
RefreshTokenRow {
id: token.id,
access_token_id: token.token_id,
secret_hash: token.secret_hash.hash().to_vec(),
expires_at: token.expires_at,
issued_at: token.issued_at,
}
}
-193
View File
@@ -1,193 +0,0 @@
use super::{ReferenceRelation, ReferenceViolationKind, RepositoryError};
use crate::{
database::{
self, Constraint, DatabaseError,
dtos::{AccessTokenRow, RefreshTokenRow},
},
domain::{HashedAccessToken, HashedRefreshToken, HashedSecret},
repository::dtos::Upsert,
};
#[tracing::instrument(skip(executor, secret_hash))]
pub async fn get_access_token_by_hash<'e, E>(
executor: E,
secret_hash: &HashedSecret,
) -> Result<Option<HashedAccessToken>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::tokens::get_access_token_by_hash(executor, secret_hash.hash())
.await?
.map(decode_access_token)
.transpose()?;
Ok(token)
}
#[tracing::instrument(skip(executor, secret_hash))]
pub async fn get_access_token_by_refresh_hash<'e, E>(
executor: E,
secret_hash: &HashedSecret,
) -> Result<Option<HashedAccessToken>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token =
database::tokens::get_access_token_by_refresh_secret_hash(executor, secret_hash.hash())
.await?
.map(decode_access_token)
.transpose()?;
Ok(token)
}
#[tracing::instrument(skip(executor, access_token))]
pub async fn upsert_access_token<'e, E>(
executor: E,
access_token: HashedAccessToken,
) -> Result<Option<Upsert<HashedAccessToken>>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::tokens::upsert_access_token(executor, encode_access_token(access_token))
.await
.map_err(classify_upsert_error)?
.map(|upserted| upserted.try_map(decode_access_token))
.transpose()?
.map(Into::into);
Ok(token)
}
#[tracing::instrument(skip(executor, secret_hash))]
pub async fn delete_access_token_by_hash<'e, E>(
executor: E,
secret_hash: &HashedSecret,
) -> Result<Option<HashedAccessToken>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::tokens::delete_access_token_by_hash(executor, secret_hash.hash())
.await?
.map(decode_access_token)
.transpose()?;
Ok(token)
}
#[tracing::instrument(skip(executor, secret_hash))]
pub async fn get_refresh_token_by_hash<'e, E>(
executor: E,
secret_hash: &HashedSecret,
) -> Result<Option<HashedRefreshToken>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::tokens::get_refresh_token_by_hash(executor, secret_hash.hash())
.await?
.map(decode_refresh_token)
.transpose()?;
Ok(token)
}
#[tracing::instrument(skip(executor, refresh_token))]
pub async fn upsert_refresh_token<'e, E>(
executor: E,
refresh_token: HashedRefreshToken,
) -> Result<Option<Upsert<HashedRefreshToken>>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token =
database::tokens::upsert_refresh_token(executor, encode_refresh_token(refresh_token))
.await
.map_err(classify_upsert_error)?
.map(|upserted| upserted.try_map(decode_refresh_token))
.transpose()?
.map(Into::into);
Ok(token)
}
#[tracing::instrument(skip(executor, secret_hash))]
pub async fn delete_refresh_token_by_hash<'e, E>(
executor: E,
secret_hash: &HashedSecret,
) -> Result<Option<HashedRefreshToken>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let token = database::tokens::delete_refresh_token_by_hash(executor, secret_hash.hash())
.await?
.map(decode_refresh_token)
.transpose()?;
Ok(token)
}
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
let relation = match &error {
DatabaseError::ForeignKeyViolation {
constraint: Constraint::AccessTokenUser,
..
} => ReferenceRelation::User,
DatabaseError::ForeignKeyViolation {
constraint: Constraint::AccessTokenUserApp,
..
} => ReferenceRelation::UserApp,
DatabaseError::ForeignKeyViolation {
constraint: Constraint::RefreshTokenAccessToken,
..
} => ReferenceRelation::AccessToken,
_ => return RepositoryError::Database(error),
};
RepositoryError::ReferenceViolation {
relation,
kind: ReferenceViolationKind::Missing,
source: error,
}
}
fn decode_access_token(row: AccessTokenRow) -> Result<HashedAccessToken, RepositoryError> {
Ok(HashedAccessToken {
id: row.id,
user_id: row.user_id,
user_app_id: row.user_app_id,
secret_hash: HashedSecret::from(row.secret_hash),
expires_at: row.expires_at,
issued_at: row.issued_at,
})
}
fn encode_access_token(token: HashedAccessToken) -> AccessTokenRow {
AccessTokenRow {
id: token.id,
user_id: token.user_id,
user_app_id: token.user_app_id,
secret_hash: token.secret_hash.hash().to_vec(),
expires_at: token.expires_at,
issued_at: token.issued_at,
}
}
fn decode_refresh_token(row: RefreshTokenRow) -> Result<HashedRefreshToken, RepositoryError> {
Ok(HashedRefreshToken {
id: row.id,
token_id: row.access_token_id,
secret_hash: HashedSecret::from(row.secret_hash),
expires_at: row.expires_at,
issued_at: row.issued_at,
})
}
fn encode_refresh_token(token: HashedRefreshToken) -> RefreshTokenRow {
RefreshTokenRow {
id: token.id,
access_token_id: token.token_id,
secret_hash: token.secret_hash.hash().to_vec(),
expires_at: token.expires_at,
issued_at: token.issued_at,
}
}
+14 -14
View File
@@ -77,20 +77,6 @@ where
Ok(upserted)
}
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
match &error {
DatabaseError::ForeignKeyViolation {
constraint: Constraint::TransactionAccount,
..
} => RepositoryError::ReferenceViolation {
relation: ReferenceRelation::Account,
kind: ReferenceViolationKind::Missing,
source: error,
},
_ => RepositoryError::Database(error),
}
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
@@ -107,6 +93,20 @@ where
Ok(transaction)
}
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
match &error {
DatabaseError::ForeignKeyViolation {
constraint: Constraint::TransactionAccount,
..
} => RepositoryError::ReferenceViolation {
relation: ReferenceRelation::Account,
kind: ReferenceViolationKind::Missing,
source: error,
},
_ => RepositoryError::Database(error),
}
}
fn decode(row: TransactionRow) -> Result<Transaction, DecodeError> {
Ok(Transaction {
id: row.id,
+14 -14
View File
@@ -78,20 +78,6 @@ where
Ok(secret)
}
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
match &error {
DatabaseError::ForeignKeyViolation {
constraint: Constraint::UserAppSecretApp,
..
} => RepositoryError::ReferenceViolation {
relation: ReferenceRelation::UserApp,
kind: ReferenceViolationKind::Missing,
source: error,
},
_ => RepositoryError::Database(error),
}
}
pub async fn verify<'e, E>(
executor: E,
app_id: Uuid,
@@ -122,6 +108,20 @@ where
Ok(secret)
}
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
match &error {
DatabaseError::ForeignKeyViolation {
constraint: Constraint::UserAppSecretApp,
..
} => RepositoryError::ReferenceViolation {
relation: ReferenceRelation::UserApp,
kind: ReferenceViolationKind::Missing,
source: error,
},
_ => RepositoryError::Database(error),
}
}
fn decode(row: UserAppSecretRow) -> Result<UserAppSecret, DecodeError> {
Ok(UserAppSecret::new(
row.id,
+14 -14
View File
@@ -88,20 +88,6 @@ where
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),
}
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
@@ -118,6 +104,20 @@ where
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,