feat(api): POST /api/admin/field-definitions (create field definition)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -9,11 +9,15 @@ use axum::{
|
||||
response::IntoResponse,
|
||||
routing::{get, put},
|
||||
};
|
||||
use domain::{AuditActor, CatalogueObject, ObjectId, ObjectInput, Visibility};
|
||||
use domain::{
|
||||
AuditActor, AuthorityKind, CatalogueObject, FieldType, LocalizedLabel, NewFieldDefinition,
|
||||
ObjectId, ObjectInput, Visibility, VocabularyId,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
use crate::{AppState, pagination::Pagination, reindex};
|
||||
use crate::{AppState, admin_vocab::LabelInput, pagination::Pagination, reindex};
|
||||
|
||||
/// A localized label `{ lang, label }` (shared across admin views).
|
||||
#[derive(Serialize, ToSchema)]
|
||||
@@ -364,6 +368,23 @@ pub(crate) struct FieldDefinitionView {
|
||||
pub labels: Vec<LabelView>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, utoipa::ToSchema)]
|
||||
pub(crate) struct NewFieldDefinitionRequest {
|
||||
pub key: String,
|
||||
/// text | localized_text | integer | date | boolean | term | authority
|
||||
pub data_type: String,
|
||||
pub vocabulary_id: Option<String>,
|
||||
pub authority_kind: Option<String>,
|
||||
pub required: bool,
|
||||
pub group: Option<String>,
|
||||
pub labels: Vec<LabelInput>,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, utoipa::ToSchema)]
|
||||
pub(crate) struct CreatedField {
|
||||
pub key: String,
|
||||
}
|
||||
|
||||
/// List all field definitions. Requires `ViewInternal`.
|
||||
#[utoipa::path(
|
||||
get, path = "/api/admin/field-definitions",
|
||||
@@ -407,6 +428,79 @@ pub(crate) async fn list_field_definitions(
|
||||
))
|
||||
}
|
||||
|
||||
/// Create a field definition. Requires `EditCatalogue`. All type/binding consistency
|
||||
/// (term needs a vocabulary, authority takes no vocabulary, scalars take no binding) is
|
||||
/// validated by `FieldType::from_parts`, which returns `None` for any bad combination.
|
||||
#[utoipa::path(
|
||||
post, path = "/api/admin/field-definitions",
|
||||
request_body = NewFieldDefinitionRequest,
|
||||
responses(
|
||||
(status = 201, body = CreatedField),
|
||||
(status = 400, description = "Malformed vocabulary_id or authority_kind"),
|
||||
(status = 401),
|
||||
(status = 403),
|
||||
(status = 409, description = "Duplicate key"),
|
||||
(status = 422, description = "Inconsistent type/binding")
|
||||
)
|
||||
)]
|
||||
pub(crate) async fn create_field_definition(
|
||||
_auth: Authorized<EditCatalogue>,
|
||||
State(state): State<AppState>,
|
||||
Json(req): Json<NewFieldDefinitionRequest>,
|
||||
) -> Result<(StatusCode, Json<CreatedField>), StatusCode> {
|
||||
let vocabulary_id = match req.vocabulary_id.as_deref() {
|
||||
None | Some("") => None,
|
||||
Some(s) => Some(
|
||||
s.parse::<VocabularyId>()
|
||||
.map_err(|_| StatusCode::BAD_REQUEST)?,
|
||||
),
|
||||
};
|
||||
|
||||
let authority_kind = match req.authority_kind.as_deref() {
|
||||
None | Some("") => None,
|
||||
Some(s) => Some(AuthorityKind::from_db(s).ok_or(StatusCode::BAD_REQUEST)?),
|
||||
};
|
||||
|
||||
let field_type = FieldType::from_parts(&req.data_type, vocabulary_id, authority_kind)
|
||||
.ok_or(StatusCode::UNPROCESSABLE_ENTITY)?;
|
||||
|
||||
let new = NewFieldDefinition {
|
||||
key: req.key,
|
||||
field_type,
|
||||
required: req.required,
|
||||
group_key: req.group,
|
||||
labels: req
|
||||
.labels
|
||||
.into_iter()
|
||||
.map(|l| LocalizedLabel {
|
||||
lang: l.lang,
|
||||
label: l.label,
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let mut tx = state
|
||||
.db
|
||||
.pool()
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
match db::fields::create_field_definition(&mut tx, &new).await {
|
||||
Ok(_) => {
|
||||
tx.commit()
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
Ok((StatusCode::CREATED, Json(CreatedField { key: new.key })))
|
||||
}
|
||||
Err(err) if err.as_database_error().and_then(|e| e.code()).as_deref() == Some("23505") => {
|
||||
Err(StatusCode::CONFLICT)
|
||||
}
|
||||
Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
|
||||
}
|
||||
}
|
||||
|
||||
/// Replace an object's flexible-field values (validated against the registry).
|
||||
///
|
||||
/// **Replace semantics:** the body is the *complete* desired field set. Omitting a key
|
||||
@@ -470,5 +564,8 @@ pub(crate) fn routes() -> Router<AppState> {
|
||||
get(get_object).put(update_object).delete(delete_object),
|
||||
)
|
||||
.route("/api/admin/objects/{id}/fields", put(set_fields))
|
||||
.route("/api/admin/field-definitions", get(list_field_definitions))
|
||||
.route(
|
||||
"/api/admin/field-definitions",
|
||||
get(list_field_definitions).post(create_field_definition),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user