Skip to main content

backbone_bucket/application/service/
file_lock_service.rs

1//! Application service for FileLock 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_lock_service_custom.rs` (preserved on regen).
7
8use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateFileLockDto, UpdateFileLockDto};
11use crate::domain::entity::FileLock;
12use crate::infrastructure::persistence::FileLockRepository;
13
14/// Application service for FileLock 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_lock_service_custom.rs`:
21///
22/// ```rust,ignore
23/// pub struct FileLockServiceCustom {
24///     inner: Arc<FileLockService>,
25///     // add domain-specific dependencies here
26/// }
27/// ```
28pub type FileLockService = GenericCrudService<
29    FileLock,
30    CreateFileLockDto,
31    UpdateFileLockDto,
32    FileLockRepository,
33>;
34
35/// The row rules the schema declares for FileLock, checked against the row a create or
36/// update would store. The generated build attaches it to [`FileLockService`]. Every rule
37/// runs in shadow: a break is logged under `backbone::write_guard` and the write goes ahead.
38pub struct FileLockRowRules;
39
40#[async_trait::async_trait]
41impl backbone_core::write_guard::WriteGuard<FileLock> for FileLockRowRules {
42    async fn check(
43        &self,
44        ctx: &backbone_core::write_guard::WriteCtx<'_, FileLock>,
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        // `only_owner_can_modify` (file_lock.hook.yaml:88)
64        if (update) && !r::holds(&r::eq(r::actor(ctx.actor.as_deref()), r::field(row, "user_id"))) {
65            shadow.push(Violation::new("user_id", "not_lock_owner", "Only lock owner can modify"));
66        }
67        // `cannot_modify_expired` (file_lock.hook.yaml:94)
68        if (update) && !r::holds(&r::ne(r::field(row, "status"), r::text("expired"))) {
69            shadow.push(Violation::new("status", "lock_expired", "Cannot modify expired lock"));
70        }
71        GuardOutcome { refuse: Vec::new(), shadow }
72    }
73}
74