Skip to main content

backbone_inventory/application/service/
location_service.rs

1//! Application service for Location 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 `location_service_custom.rs` (preserved on regen).
7
8use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateLocationDto, UpdateLocationDto};
11use crate::domain::entity::Location;
12use crate::infrastructure::persistence::LocationRepository;
13
14/// Application service for Location 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 `location_service_custom.rs`:
21///
22/// ```rust,ignore
23/// pub struct LocationServiceCustom {
24///     inner: Arc<LocationService>,
25///     // add domain-specific dependencies here
26/// }
27/// ```
28pub type LocationService = GenericCrudService<
29    Location,
30    CreateLocationDto,
31    UpdateLocationDto,
32    LocationRepository,
33>;
34
35/// The rules the schema declares for Location, checked against the row a create or update
36/// would store. The generated build attaches it to [`LocationService`]. A refusing rule's
37/// break answers 422; a rule in shadow is logged under `backbone::write_guard` and the
38/// write goes ahead.
39pub struct LocationRowRules;
40
41#[async_trait::async_trait]
42impl backbone_core::write_guard::WriteGuard<Location> for LocationRowRules {
43    async fn check(
44        &self,
45        ctx: &backbone_core::write_guard::WriteCtx<'_, Location>,
46    ) -> backbone_core::write_guard::GuardOutcome {
47        use backbone_core::row_rule as r;
48        use backbone_core::violation::Violation;
49        use backbone_core::write_guard::{GuardOutcome, WriteKind};
50        let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
51        let Some(after) = ctx.after.filter(|_| create || update) else {
52            return GuardOutcome::allow();
53        };
54        let Ok(row) = serde_json::to_value(after) else {
55            return GuardOutcome::allow();
56        };
57        let row = &row;
58        let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
59        let shadow = Vec::new();
60        let mut refuse = Vec::new();
61        // `name_required` (@required on Location.name)
62        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")))))) {
63            refuse.push(Violation::new("name", "required", "name is required"));
64        }
65        // `name_max_length` (@max(150) on Location.name)
66        if (create || (update && r::changed(old.as_ref(), row, &["name"]))) && !r::holds(&r::le(r::length(r::field(row, "name")), r::Val::Num(150.0_f64))) {
67            refuse.push(Violation::new("name", "max_length", "name must be at most 150 characters").with_param("max", 150_i64));
68        }
69        // `complete_name_max_length` (@max(500) on Location.complete_name)
70        if (create || (update && r::changed(old.as_ref(), row, &["complete_name"]))) && !r::holds(&r::le(r::length(r::field(row, "complete_name")), r::Val::Num(500.0_f64))) {
71            refuse.push(Violation::new("complete_name", "max_length", "complete_name must be at most 500 characters").with_param("max", 500_i64));
72        }
73        // `parent_path_max_length` (@max(1000) on Location.parent_path)
74        if (create || (update && r::changed(old.as_ref(), row, &["parent_path"]))) && !r::holds(&r::le(r::length(r::field(row, "parent_path")), r::Val::Num(1000.0_f64))) {
75            refuse.push(Violation::new("parent_path", "max_length", "parent_path must be at most 1000 characters").with_param("max", 1000_i64));
76        }
77        // `barcode_max_length` (@max(64) on Location.barcode)
78        if (create || (update && r::changed(old.as_ref(), row, &["barcode"]))) && !r::holds(&r::le(r::length(r::field(row, "barcode")), r::Val::Num(64.0_f64))) {
79            refuse.push(Violation::new("barcode", "max_length", "barcode must be at most 64 characters").with_param("max", 64_i64));
80        }
81        GuardOutcome { refuse, shadow }
82    }
83}
84