dialtone_sqlx 0.1.0

Dialtone SQLx Back-End
Documentation
use crate::db::ap_object::insert::insert_ap_object;
use crate::db::ap_object::{ApObjectDbType, ApObjectVisibilityDbType};
use anyhow::{anyhow, Context};
use dialtone_common::ap::ap_object::ApObject;
use sqlx::PgPool;

pub async fn create_ap_object(
    pool: &PgPool,
    ap_object: &ApObject,
    host_name: &str,
) -> anyhow::Result<String> {
    let id = ap_object
        .id
        .clone()
        .with_context(|| anyhow!("no ID in AP Object"))?;
    let visibility = ApObjectVisibilityDbType::Visible;
    let ap_type = ap_object
        .ap_type
        .as_ref()
        .with_context(|| "AP Object has no type")?;
    let ap_object_type = ApObjectDbType::from(ap_type);
    insert_ap_object(
        pool,
        host_name,
        &ap_object,
        None,
        ap_object_type,
        None,
        visibility,
    )
    .await?;
    Ok(id)
}