Handler.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Microsoft.EntityFrameworkCore;
  4. using SharedKernel.Results;
  5. namespace Application.Features.Api.Forum.CommentReport.Create;
  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.Comment.FirstOrDefaultAsync(x => x.ID == request.CommentID, ct);
  11. if (comment is null)
  12. {
  13. return Result.Failure(Error.NotFound("CommentReport.CommentNotFound", "댓글을 찾을 수 없습니다."));
  14. }
  15. // 중복 신고 확인
  16. var exists = await db.CommentReport.AnyAsync(x => x.CommentID == request.CommentID && x.MemberID == request.MemberID, ct);
  17. if (exists)
  18. {
  19. return Result.Failure(Error.Conflict("CommentReport.Duplicate", "이미 신고한 댓글입니다."));
  20. }
  21. var report = new Domain.Entities.Forum.Comments.CommentReport
  22. {
  23. BoardID = comment.BoardID,
  24. PostID = comment.PostID,
  25. CommentID = request.CommentID,
  26. MemberID = request.MemberID,
  27. Type = request.Type,
  28. Reason = request.Reason
  29. };
  30. await db.CommentReport.AddAsync(report, ct);
  31. comment.Reports++;
  32. // 댓글 작성자의 신고 당한 횟수 증가
  33. var reportedStats = await db.MemberStats.FirstOrDefaultAsync(x => x.MemberID == comment.MemberID, ct);
  34. if (reportedStats is not null)
  35. {
  36. reportedStats.ReportedCount++;
  37. }
  38. await db.SaveChangesAsync(ct);
  39. return Result.Success();
  40. }
  41. }