use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreateItemDto, UpdateItemDto};
use crate::domain::entity::Item;
use crate::infrastructure::persistence::ItemRepository;
pub type ItemService = GenericCrudService<
Item,
CreateItemDto,
UpdateItemDto,
ItemRepository,
>;
pub struct ItemRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<Item> for ItemRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, Item>,
) -> 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();
if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["status"]) {
shadow.push(Violation::new("status", "state_field_verbs_only", "the state field moves only through its verbs"));
}
GuardOutcome { refuse: Vec::new(), shadow }
}
}