Added account member API.

This commit is contained in:
2026-10-11 10:00:04 +02:00
parent 6ee0317186
commit 21f0c17af9
27 changed files with 994 additions and 8 deletions
+125
View File
@@ -0,0 +1,125 @@
use axum::{extract::State, http::StatusCode};
use serde::Deserialize;
use sqlx::PgPool;
use uuid::Uuid;
use super::{
dtos::account_member::{AccountMemberRequest, AccountMemberResponse},
extractors::{Auth, Json, Path, Query},
};
use crate::{
error::AppError,
repository::{self, RepositoryError},
};
pub fn create_router() -> axum::Router<PgPool> {
axum::Router::new()
.route("/", axum::routing::get(list_account_members))
.route(
"/{member_id}",
axum::routing::get(get_account_member)
.put(upsert_account_member)
.delete(delete_account_member),
)
}
#[derive(Debug, Deserialize)]
pub struct QueryParams {
pub limit: Option<u64>,
pub offset: Option<u64>,
}
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn list_account_members(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path(account_id): Path<Uuid>,
Query(query): Query<QueryParams>,
) -> Result<Json<Vec<AccountMemberResponse>>, AppError> {
let query = repository::account_members::Query {
account_id,
limit: Some(query.limit.unwrap_or(20)),
offset: query.offset,
};
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
ensure_account_exists(&mut tx, account_id).await?;
let members = repository::account_members::list(&mut *tx, query)
.await?
.into_iter()
.map(AccountMemberResponse::from)
.collect();
tx.commit().await.map_err(RepositoryError::from)?;
Ok(Json(members))
}
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn get_account_member(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
) -> Result<Json<AccountMemberResponse>, AppError> {
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
ensure_account_exists(&mut tx, account_id).await?;
let member = repository::account_members::get_by_id(&mut *tx, account_id, member_id)
.await?
.ok_or(AppError::NotFound)?;
tx.commit().await.map_err(RepositoryError::from)?;
Ok(Json(member.into()))
}
#[axum::debug_handler]
#[tracing::instrument(skip(pool, member))]
async fn upsert_account_member(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
Json(member): Json<AccountMemberRequest>,
) -> Result<(StatusCode, Json<AccountMemberResponse>), AppError> {
let member = member.into_domain(member_id, account_id)?;
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
ensure_account_exists(&mut tx, account_id).await?;
let upserted = repository::account_members::upsert(&mut *tx, member).await?;
tx.commit().await.map_err(RepositoryError::from)?;
let status = if upserted.previous.is_some() {
StatusCode::OK
} else {
StatusCode::CREATED
};
Ok((status, Json(upserted.current.into())))
}
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn delete_account_member(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
) -> Result<StatusCode, AppError> {
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
ensure_account_exists(&mut tx, account_id).await?;
let deleted =
repository::account_members::delete_by_id(&mut *tx, account_id, member_id).await?;
tx.commit().await.map_err(RepositoryError::from)?;
deleted.ok_or(AppError::NotFound)?;
Ok(StatusCode::NO_CONTENT)
}
async fn ensure_account_exists(
connection: &mut sqlx::PgConnection,
account_id: Uuid,
) -> Result<(), AppError> {
repository::accounts::get_by_id(&mut *connection, account_id)
.await?
.map(|_| ())
.ok_or(AppError::NotFound)
}