backbone_inventory/application/service/
scrap_service.rs1use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateScrapDto, UpdateScrapDto};
11use crate::domain::entity::Scrap;
12use crate::infrastructure::persistence::ScrapRepository;
13
14pub type ScrapService = GenericCrudService<
29 Scrap,
30 CreateScrapDto,
31 UpdateScrapDto,
32 ScrapRepository,
33>;
34
35pub struct ScrapRowRules;
40
41#[async_trait::async_trait]
42impl backbone_core::write_guard::WriteGuard<Scrap> for ScrapRowRules {
43 async fn check(
44 &self,
45 ctx: &backbone_core::write_guard::WriteCtx<'_, Scrap>,
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 if (create || (update && r::changed(old.as_ref(), row, &["name"]))) && !r::holds(&r::le(r::length(r::field(row, "name")), r::Val::Num(40.0_f64))) {
63 refuse.push(Violation::new("name", "max_length", "name must be at most 40 characters").with_param("max", 40_i64));
64 }
65 if (create || (update && r::changed(old.as_ref(), row, &["origin"]))) && !r::holds(&r::le(r::length(r::field(row, "origin")), r::Val::Num(200.0_f64))) {
67 refuse.push(Violation::new("origin", "max_length", "origin must be at most 200 characters").with_param("max", 200_i64));
68 }
69 if (create || (update && r::changed(old.as_ref(), row, &["item_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "item_id")), r::not(r::is_empty(r::trim(r::field(row, "item_id")))))) {
71 refuse.push(Violation::new("item_id", "required", "item_id is required"));
72 }
73 if (create || (update && r::changed(old.as_ref(), row, &["location_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "location_id")), r::not(r::is_empty(r::trim(r::field(row, "location_id")))))) {
75 refuse.push(Violation::new("location_id", "required", "location_id is required"));
76 }
77 if (create || (update && r::changed(old.as_ref(), row, &["scrap_location_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "scrap_location_id")), r::not(r::is_empty(r::trim(r::field(row, "scrap_location_id")))))) {
79 refuse.push(Violation::new("scrap_location_id", "required", "scrap_location_id is required"));
80 }
81 GuardOutcome { refuse, shadow }
82 }
83}
84