backbone_bucket/application/service/
file_comment_service.rs1use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateFileCommentDto, UpdateFileCommentDto};
11use crate::domain::entity::FileComment;
12use crate::infrastructure::persistence::FileCommentRepository;
13
14pub type FileCommentService = GenericCrudService<
29 FileComment,
30 CreateFileCommentDto,
31 UpdateFileCommentDto,
32 FileCommentRepository,
33>;
34
35pub struct FileCommentRowRules;
39
40#[async_trait::async_trait]
41impl backbone_core::write_guard::WriteGuard<FileComment> for FileCommentRowRules {
42 async fn check(
43 &self,
44 ctx: &backbone_core::write_guard::WriteCtx<'_, FileComment>,
45 ) -> backbone_core::write_guard::GuardOutcome {
46 use backbone_core::row_rule as r;
47 use backbone_core::violation::Violation;
48 use backbone_core::write_guard::{GuardOutcome, WriteKind};
49 let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
50 let Some(after) = ctx.after.filter(|_| create || update) else {
51 return GuardOutcome::allow();
52 };
53 let Ok(row) = serde_json::to_value(after) else {
54 return GuardOutcome::allow();
55 };
56 let row = &row;
57 let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
58 let mut shadow = Vec::new();
59 if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["status"]) {
61 shadow.push(Violation::new("status", "state_field_verbs_only", "the state field moves only through its verbs"));
62 }
63 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)))) {
65 shadow.push(Violation::new("content", "empty_comment", "Comment content is required"));
66 }
67 if (create || update) && !r::holds(&r::le(r::length(r::field(row, "content")), r::Val::Num(10000.0_f64))) {
69 shadow.push(Violation::new("content", "comment_too_long", "Comment content max 10000 characters"));
70 }
71 if (update) && !r::holds(&r::eq(r::actor(ctx.actor.as_deref()), r::field(row, "user_id"))) {
73 shadow.push(Violation::new("user_id", "not_comment_owner", "Only comment owner can edit"));
74 }
75 if (update) && !r::holds(&r::ne(r::field(row, "status"), r::text("deleted"))) {
77 shadow.push(Violation::new("status", "comment_deleted", "Cannot edit deleted comment"));
78 }
79 GuardOutcome { refuse: Vec::new(), shadow }
80 }
81}
82