use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreateContentHashDto, UpdateContentHashDto};
use crate::domain::entity::ContentHash;
use crate::infrastructure::persistence::ContentHashRepository;
pub type ContentHashService = GenericCrudService<
ContentHash,
CreateContentHashDto,
UpdateContentHashDto,
ContentHashRepository,
>;
pub struct ContentHashRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<ContentHash> for ContentHashRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, ContentHash>,
) -> backbone_core::write_guard::GuardOutcome {
use backbone_core::row_rule as r;
use backbone_core::violation::Violation;
use backbone_core::write_guard::{GuardOutcome, WriteKind};
let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
let Some(after) = ctx.after.filter(|_| create || update) else {
return GuardOutcome::allow();
};
let Ok(row) = serde_json::to_value(after) else {
return GuardOutcome::allow();
};
let row = &row;
let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
let mut shadow = Vec::new();
let mut refuse = Vec::new();
if (create || (update && r::changed(old.as_ref(), row, &["hash"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "hash")), r::not(r::is_empty(r::trim(r::field(row, "hash")))))) {
refuse.push(Violation::new("hash", "required", "hash is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["hash"]))) && !r::holds(&r::eq(r::length(r::field(row, "hash")), r::Val::Num(64.0_f64))) {
shadow.push(Violation::new("hash", "length", "hash must be exactly 64 characters").with_param("length", 64_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["size_bytes"]))) && !r::holds(&r::gt(r::field(row, "size_bytes"), r::Val::Num(0.0_f64))) {
shadow.push(Violation::new("size_bytes", "positive", "size_bytes must be positive"));
}
if (create || (update && r::changed(old.as_ref(), row, &["storage_key"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "storage_key")), r::not(r::is_empty(r::trim(r::field(row, "storage_key")))))) {
refuse.push(Violation::new("storage_key", "required", "storage_key is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["storage_key"]))) && !r::holds(&r::le(r::length(r::field(row, "storage_key")), r::Val::Num(1024.0_f64))) {
shadow.push(Violation::new("storage_key", "max_length", "storage_key must be at most 1024 characters").with_param("max", 1024_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["reference_count"]))) && !r::holds(&r::ge(r::field(row, "reference_count"), r::Val::Num(1.0_f64))) {
shadow.push(Violation::new("reference_count", "min", "reference_count must be at least 1").with_param("min", 1_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["fingerprint"]))) && !r::holds(&r::le(r::length(r::field(row, "fingerprint")), r::Val::Num(128.0_f64))) {
shadow.push(Violation::new("fingerprint", "max_length", "fingerprint must be at most 128 characters").with_param("max", 128_i64));
}
GuardOutcome { refuse, shadow }
}
}