Skip to main content

backbone_bucket/application/service/
upload_session_service.rs

1//! Application service for UploadSession 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 `upload_session_service_custom.rs` (preserved on regen).
7
8use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateUploadSessionDto, UpdateUploadSessionDto};
11use crate::domain::entity::UploadSession;
12use crate::infrastructure::persistence::UploadSessionRepository;
13
14/// Application service for UploadSession 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 `upload_session_service_custom.rs`:
21///
22/// ```rust,ignore
23/// pub struct UploadSessionServiceCustom {
24///     inner: Arc<UploadSessionService>,
25///     // add domain-specific dependencies here
26/// }
27/// ```
28pub type UploadSessionService = GenericCrudService<
29    UploadSession,
30    CreateUploadSessionDto,
31    UpdateUploadSessionDto,
32    UploadSessionRepository,
33>;
34
35/// The row rules the schema declares for UploadSession, checked against the row a create or
36/// update would store. The generated build attaches it to [`UploadSessionService`]. Every rule
37/// runs in shadow: a break is logged under `backbone::write_guard` and the write goes ahead.
38pub struct UploadSessionRowRules;
39
40#[async_trait::async_trait]
41impl backbone_core::write_guard::WriteGuard<UploadSession> for UploadSessionRowRules {
42    async fn check(
43        &self,
44        ctx: &backbone_core::write_guard::WriteCtx<'_, UploadSession>,
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        // `valid_file_size` (upload_session.hook.yaml:133)
64        if (create) && !r::holds(&r::gt(r::field(row, "file_size"), r::Val::Num(0.0_f64))) {
65            shadow.push(Violation::new("file_size", "invalid_file_size", "File size must be positive"));
66        }
67        // `minimum_chunk_size` (upload_session.hook.yaml:139)
68        if (create) && !r::holds(&r::ge(r::field(row, "chunk_size"), r::Val::Num(5242880.0_f64))) {
69            shadow.push(Violation::new("chunk_size", "chunk_too_small", "Chunk size must be at least 5MB"));
70        }
71        // `reasonable_chunk_size` (upload_session.hook.yaml:145)
72        if (create) && !r::holds(&r::le(r::field(row, "chunk_size"), r::Val::Num(5368709120.0_f64))) {
73            shadow.push(Violation::new("chunk_size", "chunk_too_large", "Chunk size must not exceed 5GB"));
74        }
75        // `valid_chunk_count` (upload_session.hook.yaml:151)
76        if (create) && !r::holds(&r::eq(r::field(row, "total_chunks"), r::ceil(r::div(r::field(row, "file_size"), r::field(row, "chunk_size"))))) {
77            shadow.push(Violation::new("total_chunks", "invalid_chunk_count", "Total chunks must match file size and chunk size"));
78        }
79        // `valid_path` (upload_session.hook.yaml:157)
80        if (create) && !r::holds(&r::and(r::and(r::is_not_null(r::field(row, "path")), r::not(r::contains(r::field(row, "path"), r::text("..")))), r::le(r::length(r::field(row, "path")), r::Val::Num(1024.0_f64)))) {
81            shadow.push(Violation::new("path", "invalid_path", "Path must be valid and not contain traversal"));
82        }
83        // `valid_expiration` (upload_session.hook.yaml:163)
84        if (create) && !r::holds(&r::gt(r::field(row, "expires_at"), r::at(ctx.at))) {
85            shadow.push(Violation::new("expires_at", "invalid_expiration", "Expiration must be in future"));
86        }
87        GuardOutcome { refuse: Vec::new(), shadow }
88    }
89}
90