Nit picking a little.
This commit is contained in:
@@ -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<PgPool> {
|
||||
axum::Router::new()
|
||||
@@ -42,21 +38,19 @@ async fn list_scheduled_transactions(
|
||||
State(pool): State<PgPool>,
|
||||
Query(query): Query<QueryParams>,
|
||||
) -> Result<Json<Vec<ScheduledTransactionResponse>>, 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::<Result<Vec<_>, _>>()?;
|
||||
|
||||
Ok(Json(responses))
|
||||
@@ -69,14 +63,13 @@ async fn get_scheduled_transaction(
|
||||
State(pool): State<PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<ScheduledTransactionResponse>, 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<Uuid>,
|
||||
Json(transaction): Json<ScheduledTransactionRequest>,
|
||||
) -> Result<(StatusCode, Json<ScheduledTransactionResponse>), 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<HashMap<Uuid, domain::Account>, AppError> {
|
||||
) -> Result<HashMap<Uuid, domain::Account>, 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::<Vec<_>>();
|
||||
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user