Nit picking a little.
This commit is contained in:
@@ -38,16 +38,17 @@ async fn list_account_members(
|
||||
Query(query): Query<QueryParams>,
|
||||
) -> Result<Json<Vec<AccountMemberResponse>>, 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<AccountMemberRequest>,
|
||||
) -> Result<(StatusCode, Json<AccountMemberResponse>), 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?;
|
||||
|
||||
|
||||
@@ -72,14 +72,9 @@ async fn upsert_account(
|
||||
Json(account): Json<AccountRequest>,
|
||||
) -> Result<(StatusCode, Json<AccountResponse>), 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 {
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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<PgPool> {
|
||||
axum::Router::new()
|
||||
@@ -39,15 +35,15 @@ async fn list_transactions(
|
||||
State(pool): State<PgPool>,
|
||||
Query(query): Query<QueryParams>,
|
||||
) -> Result<Json<Vec<TransactionResponse>>, 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<PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<TransactionResponse>, 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<Uuid>,
|
||||
Json(transaction): Json<TransactionRequest>,
|
||||
) -> Result<(StatusCode, Json<TransactionResponse>), 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<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)
|
||||
@@ -132,7 +129,7 @@ async fn get_accounts_for_transactions(
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
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(
|
||||
|
||||
@@ -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)?;
|
||||
|
||||
@@ -70,8 +70,8 @@ async fn upsert_user(
|
||||
Json(user): Json<UserRequest>,
|
||||
) -> Result<(StatusCode, Json<UserResponse>), 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 {
|
||||
|
||||
Reference in New Issue
Block a user