Handler.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.EntityFrameworkCore;
  4. using SharedKernel.Results;
  5. namespace Application.Features.Api.Feed.DeleteComment;
  6. public sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result>
  7. {
  8. public async Task<Result> Handle(Command request, CancellationToken ct)
  9. {
  10. var comment = await db.FeedComment.FirstOrDefaultAsync(c => c.ID == request.CommentID && !c.IsDeleted, ct);
  11. if (comment is null)
  12. {
  13. return Result.Failure(Error.NotFound("Feed.CommentNotFound", "댓글을 찾을 수 없습니다."));
  14. }
  15. if (comment.MemberID != request.MemberID)
  16. {
  17. return Result.Failure(Error.Forbidden("Feed.NotOwner", "삭제 권한이 없습니다."));
  18. }
  19. comment.IsDeleted = true;
  20. comment.DeletedAt = DateTime.UtcNow;
  21. // 포스트 댓글 수 차감
  22. var post = await db.FeedPost.FirstOrDefaultAsync(p => p.ID == comment.FeedPostID, ct);
  23. if (post is not null)
  24. {
  25. post.Comments = post.Comments > 0 ? post.Comments - 1 : 0;
  26. }
  27. // 부모 댓글 Replies 차감
  28. if (comment.ParentID is int pid)
  29. {
  30. var parent = await db.FeedComment.FirstOrDefaultAsync(p => p.ID == pid, ct);
  31. if (parent is not null)
  32. {
  33. parent.Replies = parent.Replies > 0 ? parent.Replies - 1 : 0;
  34. }
  35. }
  36. await db.SaveChangesAsync(ct);
  37. return Result.Success();
  38. }
  39. }