Handler.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Microsoft.EntityFrameworkCore;
  4. using SharedKernel.Results;
  5. namespace Application.Features.Api.Forum.Comment.Update;
  6. public sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result>
  7. {
  8. public async Task<Result> Handle(Command request, CancellationToken ct)
  9. {
  10. if (string.IsNullOrWhiteSpace(request.Content))
  11. {
  12. return Result.Failure(Error.Problem("Comment.ContentRequired", "내용을 입력해주세요."));
  13. }
  14. var comment = await db.Comment.FirstOrDefaultAsync(x => x.ID == request.ID, ct);
  15. if (comment is null)
  16. {
  17. return Result.Failure(Error.NotFound("Comment.NotFound", "댓글을 찾을 수 없습니다."));
  18. }
  19. if (comment.MemberID != request.MemberID)
  20. {
  21. return Result.Failure(Error.Forbidden("Comment.Forbidden", "수정 권한이 없습니다."));
  22. }
  23. comment.Content = request.Content.Trim();
  24. comment.IsSecret = request.IsSecret;
  25. comment.UpdatedAt = DateTime.UtcNow;
  26. await db.SaveChangesAsync(ct);
  27. return Result.Success();
  28. }
  29. }