From c89b76043bc0428f7f3c1a1e54dc68a1a59d5d67 Mon Sep 17 00:00:00 2001 From: Rogier Reerink Date: Mon, 12 Oct 2026 12:01:26 +0200 Subject: [PATCH] Nit picking a little. --- server/src/routes/account_members.rs | 27 +++++----- server/src/routes/accounts.rs | 15 ++---- server/src/routes/scheduled_transactions.rs | 55 ++++++++++----------- server/src/routes/transactions.rs | 43 ++++++++-------- server/src/routes/user_app_secrets.rs | 4 +- server/src/routes/users.rs | 2 +- 6 files changed, 63 insertions(+), 83 deletions(-) diff --git a/server/src/routes/account_members.rs b/server/src/routes/account_members.rs index 0e58809..ab83b38 100644 --- a/server/src/routes/account_members.rs +++ b/server/src/routes/account_members.rs @@ -38,16 +38,17 @@ async fn list_account_members( Query(query): Query, ) -> Result>, AppError> { let user_id = auth.user_id(); - let query = repository::account_members::Query { - limit: Some(query.limit.unwrap_or(20)), - offset: query.offset, - }; repository::accounts::get_by_id(&pool, user_id, account_id) .await? .map(|_| ()) .ok_or(AppError::NotFound)?; + let query = repository::account_members::Query { + limit: Some(query.limit.unwrap_or(20)), + offset: query.offset, + }; + let members = repository::account_members::list(&pool, user_id, account_id, query) .await? .into_iter() @@ -81,7 +82,6 @@ async fn upsert_account_member( Json(member): Json, ) -> Result<(StatusCode, Json), AppError> { let member = member.into_domain(member_id, account_id)?; - let upserted = repository::account_members::upsert(&pool, auth.user_id(), member) .await? .ok_or(AppError::NotFound)?; @@ -110,21 +110,18 @@ async fn delete_account_member( .await? .ok_or(AppError::NotFound)?; - let members = repository::account_members::list( - &mut *tx, - user_id, - account_id, - repository::account_members::Query { - limit: Some(2), - offset: None, - }, - ) - .await?; + let members_query = repository::account_members::Query { + limit: Some(2), + offset: None, + }; + let members = + repository::account_members::list(&mut *tx, user_id, account_id, members_query).await?; if let [only] = members.as_slice() && only.id == member_id { return Err(AppError::LastAccountMemberRemoval); } + let deleted = repository::account_members::delete_by_id(&mut *tx, user_id, account_id, member_id).await?; diff --git a/server/src/routes/accounts.rs b/server/src/routes/accounts.rs index 4cb1706..f35a36d 100644 --- a/server/src/routes/accounts.rs +++ b/server/src/routes/accounts.rs @@ -72,14 +72,9 @@ async fn upsert_account( Json(account): Json, ) -> Result<(StatusCode, Json), AppError> { let user_id = auth.user_id(); + let account = account.into_domain(id)?; - - let mut tx = pool - .begin() - .await - .map_err(repository::RepositoryError::from)?; - - let upserted = repository::accounts::upsert(&mut *tx, user_id, account) + let upserted = repository::accounts::upsert(&pool, user_id, account) .await? .ok_or(AppError::AccountIdAlreadyExists)?; @@ -89,13 +84,9 @@ async fn upsert_account( account_id: id, user_id, }; - repository::account_members::insert_initial(&mut *tx, member).await?; + repository::account_members::insert_initial(&pool, member).await?; } - tx.commit() - .await - .map_err(repository::RepositoryError::from)?; - let status = if upserted.previous.is_some() { StatusCode::OK } else { diff --git a/server/src/routes/scheduled_transactions.rs b/server/src/routes/scheduled_transactions.rs index d6ac1e8..bcfcc8d 100644 --- a/server/src/routes/scheduled_transactions.rs +++ b/server/src/routes/scheduled_transactions.rs @@ -12,11 +12,7 @@ use super::{ }, extractors::{Auth, Json, Path, Query}, }; -use crate::{ - domain, - error::AppError, - repository::{self, RepositoryError, accounts}, -}; +use crate::{domain, error::AppError, repository}; pub fn create_router() -> axum::Router { axum::Router::new() @@ -42,21 +38,19 @@ async fn list_scheduled_transactions( State(pool): State, Query(query): Query, ) -> Result>, AppError> { - let now = Utc::now(); + let user_id = auth.user_id(); + let query = repository::recurring_transactions::Query { limit: Some(query.limit.unwrap_or(20)), offset: query.offset, }; - let mut tx = pool.begin().await.map_err(RepositoryError::from)?; - let transactions = - repository::recurring_transactions::list(&mut *tx, auth.user_id(), query).await?; - let accounts = get_accounts_for_transactions(&mut tx, auth.user_id(), &transactions).await?; - tx.commit().await.map_err(RepositoryError::from)?; + let transactions = repository::recurring_transactions::list(&pool, user_id, query).await?; + let accounts = get_accounts_for_transactions(&pool, user_id, &transactions).await?; let responses = transactions .into_iter() - .map(|transaction| create_response(transaction, &accounts, now)) + .map(|transaction| create_response(transaction, &accounts, Utc::now())) .collect::, _>>()?; Ok(Json(responses)) @@ -69,14 +63,13 @@ async fn get_scheduled_transaction( State(pool): State, Path(id): Path, ) -> Result, AppError> { - let mut tx = pool.begin().await.map_err(RepositoryError::from)?; - let transaction = repository::recurring_transactions::get_by_id(&mut *tx, auth.user_id(), id) + let user_id = auth.user_id(); + + let transaction = repository::recurring_transactions::get_by_id(&pool, user_id, id) .await? .ok_or(AppError::NotFound)?; let accounts = - get_accounts_for_transactions(&mut tx, auth.user_id(), std::slice::from_ref(&transaction)) - .await?; - tx.commit().await.map_err(RepositoryError::from)?; + get_accounts_for_transactions(&pool, user_id, std::slice::from_ref(&transaction)).await?; Ok(Json(create_response(transaction, &accounts, Utc::now())?)) } @@ -89,18 +82,17 @@ async fn upsert_scheduled_transaction( Path(id): Path, Json(transaction): Json, ) -> Result<(StatusCode, Json), AppError> { - let ScheduledTransactionRequest::Recurring(transaction) = transaction; - let transaction = transaction.into_domain(id)?; + let user_id = auth.user_id(); - let mut tx = pool.begin().await.map_err(RepositoryError::from)?; - let account = accounts::get_by_id(&mut *tx, auth.user_id(), transaction.account_id) + let ScheduledTransactionRequest::Recurring(transaction) = transaction; + + let transaction = transaction.into_domain(id)?; + let account = repository::accounts::get_by_id(&pool, user_id, transaction.account_id) .await? .ok_or(AppError::AccountIdNotFound)?; - let upserted = - repository::recurring_transactions::upsert(&mut *tx, auth.user_id(), transaction) - .await? - .ok_or(AppError::ScheduledTransactionIdAlreadyExists)?; - tx.commit().await.map_err(RepositoryError::from)?; + let upserted = repository::recurring_transactions::upsert(&pool, user_id, transaction) + .await? + .ok_or(AppError::ScheduledTransactionIdAlreadyExists)?; let status = if upserted.previous.is_some() { StatusCode::OK @@ -130,11 +122,14 @@ async fn delete_scheduled_transaction( Ok(StatusCode::NO_CONTENT) } -async fn get_accounts_for_transactions( - connection: &mut sqlx::PgConnection, +async fn get_accounts_for_transactions<'e, E>( + executor: E, user_id: Uuid, transactions: &[domain::RecurringTransaction], -) -> Result, AppError> { +) -> Result, AppError> +where + E: sqlx::Executor<'e, Database = sqlx::Postgres>, +{ let account_ids = transactions .iter() .map(|transaction| transaction.account_id) @@ -142,7 +137,7 @@ async fn get_accounts_for_transactions( .into_iter() .collect::>(); - Ok(accounts::get_by_ids(connection, user_id, &account_ids).await?) + Ok(repository::accounts::get_by_ids(executor, user_id, &account_ids).await?) } fn create_response( diff --git a/server/src/routes/transactions.rs b/server/src/routes/transactions.rs index 7334b20..289dcd8 100644 --- a/server/src/routes/transactions.rs +++ b/server/src/routes/transactions.rs @@ -9,11 +9,7 @@ use super::{ dtos::transaction::{TransactionRequest, TransactionResponse}, extractors::{Auth, Json, Path, Query}, }; -use crate::{ - domain, - error::AppError, - repository::{self, RepositoryError}, -}; +use crate::{domain, error::AppError, repository}; pub fn create_router() -> axum::Router { axum::Router::new() @@ -39,15 +35,15 @@ async fn list_transactions( State(pool): State, Query(query): Query, ) -> Result>, AppError> { + let user_id = auth.user_id(); + let query = repository::transactions::Query { limit: Some(query.limit.unwrap_or(20)), offset: query.offset, }; - let mut tx = pool.begin().await.map_err(RepositoryError::from)?; - let transactions = repository::transactions::list(&mut *tx, auth.user_id(), query).await?; - let accounts = get_accounts_for_transactions(&mut tx, auth.user_id(), &transactions).await?; - tx.commit().await.map_err(RepositoryError::from)?; + let transactions = repository::transactions::list(&pool, user_id, query).await?; + let accounts = get_accounts_for_transactions(&pool, user_id, &transactions).await?; let responses = transactions .into_iter() @@ -64,14 +60,13 @@ async fn get_transaction( State(pool): State, Path(id): Path, ) -> Result, AppError> { - let mut tx = pool.begin().await.map_err(RepositoryError::from)?; - let transaction = repository::transactions::get_by_id(&mut *tx, auth.user_id(), id) + let user_id = auth.user_id(); + + let transaction = repository::transactions::get_by_id(&pool, user_id, id) .await? .ok_or(AppError::NotFound)?; let accounts = - get_accounts_for_transactions(&mut tx, auth.user_id(), std::slice::from_ref(&transaction)) - .await?; - tx.commit().await.map_err(RepositoryError::from)?; + get_accounts_for_transactions(&pool, user_id, std::slice::from_ref(&transaction)).await?; Ok(Json(create_response(transaction, &accounts)?)) } @@ -84,16 +79,15 @@ async fn upsert_transaction( Path(id): Path, Json(transaction): Json, ) -> Result<(StatusCode, Json), AppError> { - let transaction = transaction.into_domain(id)?; + let user_id = auth.user_id(); - let mut tx = pool.begin().await.map_err(RepositoryError::from)?; - let account = repository::accounts::get_by_id(&mut *tx, auth.user_id(), transaction.account_id) + let transaction = transaction.into_domain(id)?; + let account = repository::accounts::get_by_id(&pool, user_id, transaction.account_id) .await? .ok_or(AppError::AccountIdNotFound)?; - let upserted = repository::transactions::upsert(&mut *tx, auth.user_id(), transaction) + let upserted = repository::transactions::upsert(&pool, user_id, transaction) .await? .ok_or(AppError::TransactionIdAlreadyExists)?; - tx.commit().await.map_err(RepositoryError::from)?; let status = if upserted.previous.is_some() { StatusCode::OK @@ -120,11 +114,14 @@ async fn delete_transaction( Ok(StatusCode::NO_CONTENT) } -async fn get_accounts_for_transactions( - connection: &mut sqlx::PgConnection, +async fn get_accounts_for_transactions<'e, E>( + executor: E, user_id: Uuid, transactions: &[domain::Transaction], -) -> Result, AppError> { +) -> Result, AppError> +where + E: sqlx::Executor<'e, Database = sqlx::Postgres>, +{ let account_ids = transactions .iter() .map(|transaction| transaction.account_id) @@ -132,7 +129,7 @@ async fn get_accounts_for_transactions( .into_iter() .collect::>(); - Ok(repository::accounts::get_by_ids(connection, user_id, &account_ids).await?) + Ok(repository::accounts::get_by_ids(executor, user_id, &account_ids).await?) } fn create_response( diff --git a/server/src/routes/user_app_secrets.rs b/server/src/routes/user_app_secrets.rs index e93e6e8..5ed4b81 100644 --- a/server/src/routes/user_app_secrets.rs +++ b/server/src/routes/user_app_secrets.rs @@ -1,4 +1,5 @@ use axum::{extract::State, http::StatusCode}; +use chrono::Utc; use serde::Deserialize; use sqlx::PgPool; use uuid::Uuid; @@ -92,11 +93,10 @@ async fn upsert_user_app_secret( return Err(AppError::NotFound); } - let now = chrono::Utc::now(); let generated_secret = generate_secret().map_err(|_| AppError::Unknown)?; let hashed_secret = hash_secret(&generated_secret); - let secret = request.into_domain(secret_id, app_id, hashed_secret, now)?; + let secret = request.into_domain(secret_id, app_id, hashed_secret, Utc::now())?; let upserted = repository::user_app_secrets::upsert(&pool, user_id, secret) .await? .ok_or(AppError::NotFound)?; diff --git a/server/src/routes/users.rs b/server/src/routes/users.rs index 9a3a024..1fe0a89 100644 --- a/server/src/routes/users.rs +++ b/server/src/routes/users.rs @@ -70,8 +70,8 @@ async fn upsert_user( Json(user): Json, ) -> Result<(StatusCode, Json), AppError> { let user = user.into_domain(id)?; - let upserted = repository::users::upsert(&pool, user).await?; + let status = if upserted.previous.is_some() { StatusCode::OK } else {