Slight polishing.
This commit is contained in:
@@ -32,6 +32,9 @@ pub enum AppError {
|
||||
#[error("not found")]
|
||||
NotFound,
|
||||
|
||||
#[error("race condition occurred")]
|
||||
RaceCondition,
|
||||
|
||||
#[error("unknown")]
|
||||
Unknown,
|
||||
|
||||
|
||||
@@ -74,7 +74,10 @@ async fn get_account_member(
|
||||
repository::account_members::get_by_id(&pool, auth.user_id(), account_id, member_id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
let users = get_users_for_account_members(&pool, std::slice::from_ref(&member)).await?;
|
||||
let users = repository::users::get_by_id(&pool, member.user_id)
|
||||
.await?
|
||||
.map(|user| HashMap::from([(user.id, user)]))
|
||||
.ok_or(AppError::RaceCondition)?; // User should exist!
|
||||
|
||||
Ok(Json(create_response(member, &users)?))
|
||||
}
|
||||
@@ -103,9 +106,13 @@ async fn upsert_account_member(
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
let users = HashMap::from([(user.id, user)]);
|
||||
|
||||
Ok((status, Json(create_response(upserted.current, &users)?)))
|
||||
Ok((
|
||||
status,
|
||||
Json(create_response(
|
||||
upserted.current,
|
||||
&HashMap::from([(user.id, user)]),
|
||||
)?),
|
||||
))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
|
||||
@@ -53,6 +53,7 @@ impl ClientError for AppError {
|
||||
"Invalid client credentials".into(),
|
||||
),
|
||||
Self::InvalidRefreshToken => (StatusCode::UNAUTHORIZED, "Invalid refresh token".into()),
|
||||
Self::RaceCondition => (StatusCode::CONFLICT, "Try again".into()),
|
||||
Self::NotFound => (StatusCode::NOT_FOUND, "Not found".into()),
|
||||
Self::Domain(error) => error.client_response(),
|
||||
Self::Extractor(error) => error.client_response(),
|
||||
|
||||
@@ -68,10 +68,15 @@ async fn get_scheduled_transaction(
|
||||
let transaction = repository::recurring_transactions::get_by_id(&pool, user_id, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
let accounts =
|
||||
get_accounts_for_transactions(&pool, user_id, std::slice::from_ref(&transaction)).await?;
|
||||
let account = repository::accounts::get_by_id(&pool, user_id, transaction.account_id)
|
||||
.await?
|
||||
.ok_or(AppError::RaceCondition)?; // Account should be accessible!
|
||||
|
||||
Ok(Json(create_response(transaction, &accounts, Utc::now())?))
|
||||
Ok(Json(create_response(
|
||||
transaction,
|
||||
&HashMap::from([(account.id, account)]),
|
||||
Utc::now(),
|
||||
)?))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
|
||||
@@ -65,10 +65,14 @@ async fn get_transaction(
|
||||
let transaction = repository::transactions::get_by_id(&pool, user_id, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
let accounts =
|
||||
get_accounts_for_transactions(&pool, user_id, std::slice::from_ref(&transaction)).await?;
|
||||
let account = repository::accounts::get_by_id(&pool, user_id, transaction.account_id)
|
||||
.await?
|
||||
.ok_or(AppError::RaceCondition)?; // Account should be accessible!
|
||||
|
||||
Ok(Json(create_response(transaction, &accounts)?))
|
||||
Ok(Json(create_response(
|
||||
transaction,
|
||||
&HashMap::from([(account.id, account)]),
|
||||
)?))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -95,9 +99,13 @@ async fn upsert_transaction(
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
let accounts = HashMap::from([(account.id, account)]);
|
||||
|
||||
Ok((status, Json(create_response(upserted.current, &accounts)?)))
|
||||
Ok((
|
||||
status,
|
||||
Json(create_response(
|
||||
upserted.current,
|
||||
&HashMap::from([(account.id, account)]),
|
||||
)?),
|
||||
))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
|
||||
Reference in New Issue
Block a user