use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreateBoothDto, UpdateBoothDto};
use crate::domain::entity::Booth;
use crate::infrastructure::persistence::BoothRepository;
pub type BoothService = GenericCrudService<
Booth,
CreateBoothDto,
UpdateBoothDto,
BoothRepository,
>;
pub struct BoothRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<Booth> for BoothRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, Booth>,
) -> backbone_core::write_guard::GuardOutcome {
use backbone_core::row_rule as r;
use backbone_core::violation::Violation;
use backbone_core::write_guard::{GuardOutcome, WriteKind};
let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
let Some(after) = ctx.after.filter(|_| create || update) else {
return GuardOutcome::allow();
};
let Ok(row) = serde_json::to_value(after) else {
return GuardOutcome::allow();
};
let row = &row;
let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
let mut shadow = Vec::new();
let mut refuse = Vec::new();
if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["state"]) {
shadow.push(Violation::new("state", "state_field_verbs_only", "the state field moves only through its verbs"));
}
if (create || (update && r::changed(old.as_ref(), row, &["event_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "event_id")), r::not(r::is_empty(r::trim(r::field(row, "event_id")))))) {
refuse.push(Violation::new("event_id", "required", "event_id is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["booth_category_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "booth_category_id")), r::not(r::is_empty(r::trim(r::field(row, "booth_category_id")))))) {
refuse.push(Violation::new("booth_category_id", "required", "booth_category_id is required"));
}
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")))))) {
refuse.push(Violation::new("name", "required", "name is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["name"]))) && !r::holds(&r::le(r::length(r::field(row, "name")), r::Val::Num(200.0_f64))) {
refuse.push(Violation::new("name", "max_length", "name must be at most 200 characters").with_param("max", 200_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["contact_name"]))) && !r::holds(&r::le(r::length(r::field(row, "contact_name")), r::Val::Num(200.0_f64))) {
refuse.push(Violation::new("contact_name", "max_length", "contact_name must be at most 200 characters").with_param("max", 200_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["contact_email"]))) && !r::holds(&r::le(r::length(r::field(row, "contact_email")), r::Val::Num(254.0_f64))) {
refuse.push(Violation::new("contact_email", "max_length", "contact_email must be at most 254 characters").with_param("max", 254_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["contact_email"]))) && !r::holds(&r::matches(r::field(row, "contact_email"), "^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$")) {
refuse.push(Violation::new("contact_email", "email", "contact_email must be an email address"));
}
if (create || (update && r::changed(old.as_ref(), row, &["contact_phone"]))) && !r::holds(&r::le(r::length(r::field(row, "contact_phone")), r::Val::Num(40.0_f64))) {
refuse.push(Violation::new("contact_phone", "max_length", "contact_phone must be at most 40 characters").with_param("max", 40_i64));
}
GuardOutcome { refuse, shadow }
}
}