Skip to main content

backbone_catalog/application/service/
attribute_value_service.rs

1//! Application service for AttributeValue 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 `attribute_value_service_custom.rs` (preserved on regen).
7
8use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateAttributeValueDto, UpdateAttributeValueDto};
11use crate::domain::entity::AttributeValue;
12use crate::infrastructure::persistence::AttributeValueRepository;
13
14/// Application service for AttributeValue 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 `attribute_value_service_custom.rs`:
21///
22/// ```rust,ignore
23/// pub struct AttributeValueServiceCustom {
24///     inner: Arc<AttributeValueService>,
25///     // add domain-specific dependencies here
26/// }
27/// ```
28pub type AttributeValueService = GenericCrudService<
29    AttributeValue,
30    CreateAttributeValueDto,
31    UpdateAttributeValueDto,
32    AttributeValueRepository,
33>;
34
35/// The rules the schema declares for AttributeValue, checked against the row a create or update
36/// would store. The generated build attaches it to [`AttributeValueService`]. 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 AttributeValueRowRules;
40
41#[async_trait::async_trait]
42impl backbone_core::write_guard::WriteGuard<AttributeValue> for AttributeValueRowRules {
43    async fn check(
44        &self,
45        ctx: &backbone_core::write_guard::WriteCtx<'_, AttributeValue>,
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 mut shadow = Vec::new();
60        let mut refuse = Vec::new();
61        // `attribute_id_required` (@required on AttributeValue.attribute_id)
62        if (create || (update && r::changed(old.as_ref(), row, &["attribute_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "attribute_id")), r::not(r::is_empty(r::trim(r::field(row, "attribute_id")))))) {
63            refuse.push(Violation::new("attribute_id", "required", "attribute_id is required"));
64        }
65        // `code_required` (@required on AttributeValue.code)
66        if (create || (update && r::changed(old.as_ref(), row, &["code"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "code")), r::not(r::is_empty(r::trim(r::field(row, "code")))))) {
67            refuse.push(Violation::new("code", "required", "code is required"));
68        }
69        // `code_max_length` (@max(40) on AttributeValue.code)
70        if (create || (update && r::changed(old.as_ref(), row, &["code"]))) && !r::holds(&r::le(r::length(r::field(row, "code")), r::Val::Num(40.0_f64))) {
71            shadow.push(Violation::new("code", "max_length", "code must be at most 40 characters").with_param("max", 40_i64));
72        }
73        // `label_required` (@required on AttributeValue.label)
74        if (create || (update && r::changed(old.as_ref(), row, &["label"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "label")), r::not(r::is_empty(r::trim(r::field(row, "label")))))) {
75            refuse.push(Violation::new("label", "required", "label is required"));
76        }
77        // `label_max_length` (@max(100) on AttributeValue.label)
78        if (create || (update && r::changed(old.as_ref(), row, &["label"]))) && !r::holds(&r::le(r::length(r::field(row, "label")), r::Val::Num(100.0_f64))) {
79            shadow.push(Violation::new("label", "max_length", "label must be at most 100 characters").with_param("max", 100_i64));
80        }
81        // `label_en_max_length` (@max(100) on AttributeValue.label_en)
82        if (create || (update && r::changed(old.as_ref(), row, &["label_en"]))) && !r::holds(&r::le(r::length(r::field(row, "label_en")), r::Val::Num(100.0_f64))) {
83            shadow.push(Violation::new("label_en", "max_length", "label_en must be at most 100 characters").with_param("max", 100_i64));
84        }
85        // `swatch_hex_max_length` (@max(10) on AttributeValue.swatch_hex)
86        if (create || (update && r::changed(old.as_ref(), row, &["swatch_hex"]))) && !r::holds(&r::le(r::length(r::field(row, "swatch_hex")), r::Val::Num(10.0_f64))) {
87            shadow.push(Violation::new("swatch_hex", "max_length", "swatch_hex must be at most 10 characters").with_param("max", 10_i64));
88        }
89        // `icon_max_length` (@max(255) on AttributeValue.icon)
90        if (create || (update && r::changed(old.as_ref(), row, &["icon"]))) && !r::holds(&r::le(r::length(r::field(row, "icon")), r::Val::Num(255.0_f64))) {
91            shadow.push(Violation::new("icon", "max_length", "icon must be at most 255 characters").with_param("max", 255_i64));
92        }
93        GuardOutcome { refuse, shadow }
94    }
95}
96