use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreateBucketDto, UpdateBucketDto};
use crate::domain::entity::Bucket;
use crate::infrastructure::persistence::BucketRepository;
pub type BucketService = GenericCrudService<
Bucket,
CreateBucketDto,
UpdateBucketDto,
BucketRepository,
>;
pub struct BucketRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<Bucket> for BucketRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, Bucket>,
) -> 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 update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["status"]) {
shadow.push(Violation::new("status", "state_field_verbs_only", "the state field moves only through its verbs"));
}
if (create || (update && r::changed(old.as_ref(), row, &["name"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "name")), r::not(r::is_empty(r::trim(r::field(row, "name")))))) {
refuse.push(Violation::new("name", "required", "name is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["name"]))) && !r::holds(&r::le(r::length(r::field(row, "name")), r::Val::Num(255.0_f64))) {
refuse.push(Violation::new("name", "max_length", "name must be at most 255 characters").with_param("max", 255_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["slug"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "slug")), r::not(r::is_empty(r::trim(r::field(row, "slug")))))) {
refuse.push(Violation::new("slug", "required", "slug is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["slug"]))) && !r::holds(&r::le(r::length(r::field(row, "slug")), r::Val::Num(255.0_f64))) {
refuse.push(Violation::new("slug", "max_length", "slug must be at most 255 characters").with_param("max", 255_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["owner_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "owner_id")), r::not(r::is_empty(r::trim(r::field(row, "owner_id")))))) {
refuse.push(Violation::new("owner_id", "required", "owner_id is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["root_path"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "root_path")), r::not(r::is_empty(r::trim(r::field(row, "root_path")))))) {
refuse.push(Violation::new("root_path", "required", "root_path is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["root_path"]))) && !r::holds(&r::le(r::length(r::field(row, "root_path")), r::Val::Num(1024.0_f64))) {
refuse.push(Violation::new("root_path", "max_length", "root_path must be at most 1024 characters").with_param("max", 1024_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["file_count"]))) && !r::holds(&r::ge(r::field(row, "file_count"), r::Val::Num(0.0_f64))) {
refuse.push(Violation::new("file_count", "non_negative", "file_count must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["total_size_bytes"]))) && !r::holds(&r::ge(r::field(row, "total_size_bytes"), r::Val::Num(0.0_f64))) {
refuse.push(Violation::new("total_size_bytes", "non_negative", "total_size_bytes must not be negative"));
}
if (create || update) && !r::holds(&r::and(r::ge(r::length(r::field(row, "name")), r::Val::Num(1.0_f64)), r::le(r::length(r::field(row, "name")), r::Val::Num(255.0_f64)))) {
refuse.push(Violation::new("name", "invalid_name_length", "Bucket name must be 1-255 characters"));
}
if (create || update) && !r::holds(&r::and(r::ge(r::length(r::field(row, "slug")), r::Val::Num(1.0_f64)), r::le(r::length(r::field(row, "slug")), r::Val::Num(255.0_f64)))) {
refuse.push(Violation::new("slug", "invalid_slug_length", "Bucket slug must be 1-255 characters"));
}
if (create || update) && !r::holds(&r::matches(r::field(row, "slug"), "^[a-z0-9-]+$")) {
refuse.push(Violation::new("slug", "invalid_slug_chars", "Bucket slug must be lowercase with hyphens only"));
}
if (create) && !r::holds(&r::le(r::length(r::field(row, "root_path")), r::Val::Num(1024.0_f64))) {
refuse.push(Violation::new("root_path", "path_too_long", "Root path too long"));
}
if (create) && !r::holds(&r::not(r::contains(r::field(row, "root_path"), r::text("..")))) {
refuse.push(Violation::new("root_path", "path_traversal", "Path traversal not allowed in root path"));
}
if (update) && !r::holds(&r::ne(r::field(row, "status"), r::text("deleted"))) {
refuse.push(Violation::new("status", "bucket_deleted", "Cannot modify deleted buckets"));
}
GuardOutcome { refuse, shadow }
}
}