saas-rs-sdk 0.6.4

The SaaS RS SDK
use super::{create::find_field, schema::Table};
use crate::storage::Error;
use sqlx::{Execute, Pool, Postgres, QueryBuilder};

pub(super) async fn perform(
    pool: &Pool<Postgres>,
    table: &Table,
    id: &str,
    by_account_id: Option<String>,
) -> Result<(), Error> {
    // Prepare query
    let mut qb: QueryBuilder<Postgres> = QueryBuilder::new("UPDATE ");
    qb.push(&table.name);
    qb.push(" SET deleted_at = NOW(), deleted_by_account_id = $1");
    if let Some(field) = find_field(table, "deleted_by_account_id") {
        qb.push("::");
        qb.push(field.data_type.clone());
    }
    qb.push(" WHERE id = $2");
    if let Some(field) = find_field(table, "id") {
        qb.push("::");
        qb.push(field.data_type.clone());
    }
    let query = qb.build();
    let sql = query.sql();
    log::debug!(sql; "Deleting one");

    // Perform query
    let res = sqlx::query(sql).bind(by_account_id).bind(id).execute(pool).await?;
    if res.rows_affected() < 1 {
        return Err(Error::not_found("No such record"));
    }

    Ok(())
}