using CMSMicroservice.Application.Common.Interfaces; using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace CMSMicroservice.Application.BlogPostCQ.Commands.DeleteBlogPost; public class DeleteBlogPostCommandHandler : IRequestHandler { private readonly IApplicationDbContext _context; private readonly ILogger _logger; public DeleteBlogPostCommandHandler(IApplicationDbContext context, ILogger logger) { _context = context; _logger = logger; } public async Task Handle(DeleteBlogPostCommand request, CancellationToken cancellationToken) { var post = await _context.BlogPosts.FirstOrDefaultAsync(x => x.Id == request.Id && !x.IsDeleted, cancellationToken) ?? throw new KeyNotFoundException($"مقاله با شناسه {request.Id} یافت نشد"); post.IsDeleted = true; await _context.SaveChangesAsync(cancellationToken); _logger.LogInformation("Blog post soft-deleted. Id: {Id}", post.Id); return Unit.Value; } }