using ChapterX.Domain.Repositories; using MediatR; using Microsoft.Extensions.Logging; namespace ChapterX.Application.Comment.Commands { public class DeleteHandler : IRequestHandler { private readonly ICommentRepository _commentRepository; private readonly ILogger _logger; public DeleteHandler(ICommentRepository commentRepository, ILogger logger) { _commentRepository = commentRepository; _logger = logger; } public async Task Handle(DeleteRequest request, CancellationToken cancellationToken) { var comment = await _commentRepository.GetByIdAsync(request.Id, cancellationToken); if (comment is null) return new DeleteResponse(false); if (comment.UserId != request.CallerId) throw new UnauthorizedAccessException("You do not own this comment."); await _commentRepository.DeleteAsync(comment, cancellationToken); return new DeleteResponse(true); } } }