use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreateFileCommentDto, UpdateFileCommentDto};
use crate::domain::entity::FileComment;
use crate::infrastructure::persistence::FileCommentRepository;
pub type FileCommentService = GenericCrudService<
FileComment,
CreateFileCommentDto,
UpdateFileCommentDto,
FileCommentRepository,
>;
pub struct FileCommentRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<FileComment> for FileCommentRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, FileComment>,
) -> 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"));
}
if (create) && !r::holds(&r::and(r::is_not_null(r::field(row, "content")), r::gt(r::length(r::trim(r::field(row, "content"))), r::Val::Num(0.0_f64)))) {
shadow.push(Violation::new("content", "empty_comment", "Comment content is required"));
}
if (create || update) && !r::holds(&r::le(r::length(r::field(row, "content")), r::Val::Num(10000.0_f64))) {
shadow.push(Violation::new("content", "comment_too_long", "Comment content max 10000 characters"));
}
if (update) && !r::holds(&r::eq(r::actor(ctx.actor.as_deref()), r::field(row, "user_id"))) {
shadow.push(Violation::new("user_id", "not_comment_owner", "Only comment owner can edit"));
}
if (update) && !r::holds(&r::ne(r::field(row, "status"), r::text("deleted"))) {
shadow.push(Violation::new("status", "comment_deleted", "Cannot edit deleted comment"));
}
GuardOutcome { refuse: Vec::new(), shadow }
}
}