//! Authority records (person / organisation / place). use domain::{Authority, AuthorityId, AuthorityKind, AuthorityRef, LocalizedLabel, NewAuthority}; use sqlx::Row; /// Labels aggregated per row as JSON, to read an authority and its labels in one query. const LABELS_JSON: &str = "COALESCE(json_agg(json_build_object('lang', al.lang, 'label', al.label) \ ORDER BY al.lang) FILTER (WHERE al.authority_id IS NOT NULL), '[]'::json)"; /// Insert an authority and its labels. Multiple statements — pass a transaction /// connection (`&mut *tx`) for atomicity. pub async fn create_authority( conn: &mut sqlx::PgConnection, new: &NewAuthority, ) -> Result { let id = AuthorityId::new(); sqlx::query("INSERT INTO authority (id, kind, external_uri) VALUES ($1, $2, $3)") .bind(id.to_uuid()) .bind(new.kind.as_str()) .bind(new.external_uri.as_deref()) .execute(&mut *conn) .await?; for label in &new.labels { sqlx::query("INSERT INTO authority_label (authority_id, lang, label) VALUES ($1, $2, $3)") .bind(id.to_uuid()) .bind(&label.lang) .bind(&label.label) .execute(&mut *conn) .await?; } Ok(id) } /// Fetch one authority (with its labels). pub async fn authority_by_id<'e, E>( executor: E, id: AuthorityId, ) -> Result, sqlx::Error> where E: sqlx::PgExecutor<'e>, { let sql = format!( "SELECT a.id, a.kind, a.external_uri, {LABELS_JSON} AS labels \ FROM authority a LEFT JOIN authority_label al ON al.authority_id = a.id \ WHERE a.id = $1 GROUP BY a.id" ); let row = sqlx::query(&sql) .bind(id.to_uuid()) .fetch_optional(executor) .await?; row.map(map_authority).transpose() } /// List authorities of a given kind (with labels), ordered by id. pub async fn list_by_kind<'e, E>( executor: E, kind: AuthorityKind, ) -> Result, sqlx::Error> where E: sqlx::PgExecutor<'e>, { let sql = format!( "SELECT a.id, a.kind, a.external_uri, {LABELS_JSON} AS labels \ FROM authority a LEFT JOIN authority_label al ON al.authority_id = a.id \ WHERE a.kind = $1 GROUP BY a.id ORDER BY a.id" ); let rows = sqlx::query(&sql) .bind(kind.as_str()) .fetch_all(executor) .await?; rows.into_iter().map(map_authority).collect() } /// Resolve an authority to an [`AuthorityRef`] (carrying its kind). pub async fn resolve_authority<'e, E>( executor: E, id: AuthorityId, ) -> Result, sqlx::Error> where E: sqlx::PgExecutor<'e>, { let kind: Option = sqlx::query_scalar("SELECT kind FROM authority WHERE id = $1") .bind(id.to_uuid()) .fetch_optional(executor) .await?; match kind { Some(k) => { let kind = AuthorityKind::from_db(&k).ok_or_else(|| { sqlx::Error::Decode(format!("unknown authority kind: {k}").into()) })?; Ok(Some(AuthorityRef::new(id, kind))) } None => Ok(None), } } fn map_authority(row: sqlx::postgres::PgRow) -> Result { let kind_str: String = row.try_get("kind")?; let kind = AuthorityKind::from_db(&kind_str) .ok_or_else(|| sqlx::Error::Decode(format!("unknown authority kind: {kind_str}").into()))?; let labels: sqlx::types::Json> = row.try_get("labels")?; Ok(Authority { id: AuthorityId::from_uuid(row.try_get("id")?), kind, external_uri: row.try_get("external_uri")?, labels: labels.0, }) }