Skip to main content

backbone_bucket/application/service/
file_comment_service.rs

1//! Application service for FileComment entities
2//!
3//! Generated by metaphor-schema. Do not edit manually.
4//!
5//! Type alias over `GenericCrudService` — all CRUD logic lives in backbone-core.
6//! For custom behaviour, create `file_comment_service_custom.rs` (preserved on regen).
7
8use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateFileCommentDto, UpdateFileCommentDto};
11use crate::domain::entity::FileComment;
12use crate::infrastructure::persistence::FileCommentRepository;
13
14/// Application service for FileComment entities.
15///
16/// All 12 standard CRUD operations (list, create, get, update, patch,
17/// soft_delete, restore, empty_trash, bulk_create, upsert, find_by_id,
18/// list_deleted) are provided by `GenericCrudService`.
19///
20/// Custom extensions go in `file_comment_service_custom.rs`:
21///
22/// ```rust,ignore
23/// pub struct FileCommentServiceCustom {
24///     inner: Arc<FileCommentService>,
25///     // add domain-specific dependencies here
26/// }
27/// ```
28pub type FileCommentService = GenericCrudService<
29    FileComment,
30    CreateFileCommentDto,
31    UpdateFileCommentDto,
32    FileCommentRepository,
33>;
34
35/// The row rules the schema declares for FileComment, checked against the row a create or
36/// update would store. The generated build attaches it to [`FileCommentService`]. Every rule
37/// runs in shadow: a break is logged under `backbone::write_guard` and the write goes ahead.
38pub struct FileCommentRowRules;
39
40#[async_trait::async_trait]
41impl backbone_core::write_guard::WriteGuard<FileComment> for FileCommentRowRules {
42    async fn check(
43        &self,
44        ctx: &backbone_core::write_guard::WriteCtx<'_, FileComment>,
45    ) -> backbone_core::write_guard::GuardOutcome {
46        use backbone_core::row_rule as r;
47        use backbone_core::violation::Violation;
48        use backbone_core::write_guard::{GuardOutcome, WriteKind};
49        let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
50        let Some(after) = ctx.after.filter(|_| create || update) else {
51            return GuardOutcome::allow();
52        };
53        let Ok(row) = serde_json::to_value(after) else {
54            return GuardOutcome::allow();
55        };
56        let row = &row;
57        let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
58        let mut shadow = Vec::new();
59        // `status` belongs to a state machine: only its verbs move it
60        if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["status"]) {
61            shadow.push(Violation::new("status", "state_field_verbs_only", "the state field moves only through its verbs"));
62        }
63        // `content_required` (file_comment.hook.yaml:88)
64        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)))) {
65            shadow.push(Violation::new("content", "empty_comment", "Comment content is required"));
66        }
67        // `content_max_length` (file_comment.hook.yaml:94)
68        if (create || update) && !r::holds(&r::le(r::length(r::field(row, "content")), r::Val::Num(10000.0_f64))) {
69            shadow.push(Violation::new("content", "comment_too_long", "Comment content max 10000 characters"));
70        }
71        // `only_owner_can_edit` (file_comment.hook.yaml:124)
72        if (update) && !r::holds(&r::eq(r::actor(ctx.actor.as_deref()), r::field(row, "user_id"))) {
73            shadow.push(Violation::new("user_id", "not_comment_owner", "Only comment owner can edit"));
74        }
75        // `cannot_edit_deleted` (file_comment.hook.yaml:130)
76        if (update) && !r::holds(&r::ne(r::field(row, "status"), r::text("deleted"))) {
77            shadow.push(Violation::new("status", "comment_deleted", "Cannot edit deleted comment"));
78        }
79        GuardOutcome { refuse: Vec::new(), shadow }
80    }
81}
82