backbone-bucket 0.4.6

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Application service for FileComment entities
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! Type alias over `GenericCrudService` — all CRUD logic lives in backbone-core.
//! For custom behaviour, create `file_comment_service_custom.rs` (preserved on regen).

use backbone_core::GenericCrudService;

use crate::presentation::dto::{CreateFileCommentDto, UpdateFileCommentDto};
use crate::domain::entity::FileComment;
use crate::infrastructure::persistence::FileCommentRepository;

/// Application service for FileComment entities.
///
/// All 12 standard CRUD operations (list, create, get, update, patch,
/// soft_delete, restore, empty_trash, bulk_create, upsert, find_by_id,
/// list_deleted) are provided by `GenericCrudService`.
///
/// Custom extensions go in `file_comment_service_custom.rs`:
///
/// ```rust,ignore
/// pub struct FileCommentServiceCustom {
///     inner: Arc<FileCommentService>,
///     // add domain-specific dependencies here
/// }
/// ```
pub type FileCommentService = GenericCrudService<
    FileComment,
    CreateFileCommentDto,
    UpdateFileCommentDto,
    FileCommentRepository,
>;

/// The row rules the schema declares for FileComment, checked against the row a create or
/// update would store. The generated build attaches it to [`FileCommentService`]. Every rule
/// runs in shadow: a break is logged under `backbone::write_guard` and the write goes ahead.
pub struct FileCommentRowRules;

#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<FileComment> for FileCommentRowRules {
    async fn check(
        &self,
        ctx: &backbone_core::write_guard::WriteCtx<'_, FileComment>,
    ) -> 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();
        // `status` belongs to a state machine: only its verbs move it
        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"));
        }
        // `content_required` (file_comment.hook.yaml:88)
        if (create) && !r::holds(&r::and(r::is_not_null(r::field(row, "content")), r::gt(r::length(r::trim(r::field(row, "content"))), r::Val::Num(0.0_f64)))) {
            shadow.push(Violation::new("content", "empty_comment", "Comment content is required"));
        }
        // `content_max_length` (file_comment.hook.yaml:94)
        if (create || update) && !r::holds(&r::le(r::length(r::field(row, "content")), r::Val::Num(10000.0_f64))) {
            shadow.push(Violation::new("content", "comment_too_long", "Comment content max 10000 characters"));
        }
        // `only_owner_can_edit` (file_comment.hook.yaml:124)
        if (update) && !r::holds(&r::eq(r::actor(ctx.actor.as_deref()), r::field(row, "user_id"))) {
            shadow.push(Violation::new("user_id", "not_comment_owner", "Only comment owner can edit"));
        }
        // `cannot_edit_deleted` (file_comment.hook.yaml:130)
        if (update) && !r::holds(&r::ne(r::field(row, "status"), r::text("deleted"))) {
            shadow.push(Violation::new("status", "comment_deleted", "Cannot edit deleted comment"));
        }
        GuardOutcome { refuse: Vec::new(), shadow }
    }
}