This commit is contained in:
2025-11-06 12:26:29 +01:00
parent 4027ac66cb
commit a6f79dbc75
12 changed files with 344 additions and 199 deletions
+18 -17
View File
@@ -11,10 +11,10 @@ use uuid::Uuid;
use crate::{
auth::TenantScopedConn,
error::{AppError, AppResult},
http::responders::{no_content, ok_json, IntoAppResult, JsonResponse, RowsAffectedExt},
models::{Correspondent, NewCorrespondent},
schema::{correspondents, document_correspondents},
utils::{
db::{no_content, EnsureEntity, IntoJsonResponse},
named_entity::{ensure_name_available, normalize_name},
time::to_iso,
},
@@ -71,7 +71,7 @@ pub async fn list_correspondents(
tenant_id,
..
}: TenantScopedConn,
) -> AppResult<Json<Vec<CorrespondentSummary>>> {
) -> AppResult<JsonResponse<Vec<CorrespondentSummary>>> {
let correspondents_list: Vec<Correspondent> = correspondents::table
.filter(correspondents::tenant_id.eq(tenant_id))
.order(correspondents::name.asc())
@@ -94,7 +94,7 @@ pub async fn list_correspondents(
response.push(build_summary(correspondent, total));
}
response.into_json()
ok_json(response)
}
#[utoipa::path(
@@ -111,7 +111,7 @@ pub async fn create_correspondent(
..
}: TenantScopedConn,
Json(payload): Json<CreateCorrespondentRequest>,
) -> AppResult<Json<CorrespondentSummary>> {
) -> AppResult<JsonResponse<CorrespondentSummary>> {
let name = normalize_name(&payload.name, || {
AppError::bad_request("name must not be empty")
})?;
@@ -140,9 +140,9 @@ pub async fn create_correspondent(
.find(new_id)
.filter(correspondents::tenant_id.eq(tenant_id))
.first(&mut conn)
.one()?;
.into_app_result()?;
build_summary(correspondent, 0).into_json()
ok_json(build_summary(correspondent, 0))
}
#[utoipa::path(
@@ -161,12 +161,12 @@ pub async fn update_correspondent(
..
}: TenantScopedConn,
Json(payload): Json<UpdateCorrespondentRequest>,
) -> AppResult<Json<CorrespondentSummary>> {
) -> AppResult<JsonResponse<CorrespondentSummary>> {
let existing: Correspondent = correspondents::table
.find(correspondent_id)
.filter(correspondents::tenant_id.eq(tenant_id))
.first(&mut conn)
.one()?;
.into_app_result()?;
let mut new_name: Option<String> = None;
if let Some(ref candidate) = payload.name {
@@ -199,7 +199,7 @@ pub async fn update_correspondent(
if new_name.is_none() && new_metadata.is_none() {
let usage = load_usage_for_correspondent(&mut conn, tenant_id, correspondent_id)?;
return build_summary(existing.clone(), usage).into_json();
return ok_json(build_summary(existing.clone(), usage));
}
let mut changeset = CorrespondentChangeset::default();
@@ -217,15 +217,17 @@ pub async fn update_correspondent(
.filter(correspondents::tenant_id.eq(tenant_id)),
)
.set((&changeset, correspondents::updated_at.eq(now)))
.execute(&mut conn)?;
.execute(&mut conn)
.into_app_result()?
.or_not_found()?;
let updated: Correspondent = correspondents::table
.find(correspondent_id)
.filter(correspondents::tenant_id.eq(tenant_id))
.first(&mut conn)
.one()?;
.into_app_result()?;
let usage = load_usage_for_correspondent(&mut conn, tenant_id, correspondent_id)?;
build_summary(updated, usage).into_json()
ok_json(build_summary(updated, usage))
}
#[utoipa::path(
@@ -255,15 +257,14 @@ pub async fn delete_correspondent(
));
}
let deleted = diesel::delete(
diesel::delete(
correspondents::table
.filter(correspondents::id.eq(correspondent_id))
.filter(correspondents::tenant_id.eq(tenant_id)),
)
.execute(&mut conn)?;
if deleted == 0 {
return Err(AppError::not_found());
}
.execute(&mut conn)
.into_app_result()?
.or_not_found()?;
no_content()
}