raisfast 0.2.23

The last backend you'll ever need. Rust-powered headless CMS with built-in blog, ecommerce, wallet, payment and 4 plugin engines.
use crate::db::Pool;
use crate::errors::app_error::{AppError, AppResult};
use crate::models::currencies::{self, Currency};

pub async fn list(pool: &Pool) -> AppResult<Vec<Currency>> {
    currencies::find_all(pool).await
}

pub async fn get_by_code(pool: &Pool, code: &str) -> AppResult<Currency> {
    currencies::find_by_code(pool, code)
        .await?
        .ok_or_else(|| AppError::not_found("currency"))
}

pub async fn create(pool: &Pool, code: &str, name: &str, decimals: i64) -> AppResult<Currency> {
    if !(0..=18).contains(&decimals) {
        return Err(AppError::BadRequest(
            "decimals must be between 0 and 18".into(),
        ));
    }
    currencies::create(pool, code, name, decimals).await
}

pub async fn update(
    pool: &Pool,
    code: &str,
    name: Option<&str>,
    is_active: Option<bool>,
) -> AppResult<bool> {
    match currencies::update(pool, code, name, is_active).await? {
        Some(_) => Ok(true),
        None => Err(AppError::not_found("currency")),
    }
}