using FluentValidation; using MediatR; using Microsoft.Extensions.Logging; using Sufi.Demo.PeopleDirectory.Application.Contracts.Repositories; using Sufi.Demo.PeopleDirectory.Application.Contracts.Services; using Sufi.Demo.PeopleDirectory.Domain.Entities.Misc; using Sufi.Demo.PeopleDirectory.Shared.Wrapper; namespace Sufi.Demo.PeopleDirectory.Application.Features.Contacts.Commands { public class DeleteContactCommand : IRequest { public int Id { get; set; } } public sealed class DeleteContactCommandValidator : AbstractValidator { public DeleteContactCommandValidator() { RuleFor(v => v.Id) .GreaterThan(0) .WithMessage("A valid Id is required."); } } public class DeleteContactCommandHandler( IUnitOfWork unitOfWork, ILogger logger, IAppCache appCache ) : IRequestHandler { public async Task Handle(DeleteContactCommand request, CancellationToken cancellationToken) { var itemToDelete = await unitOfWork.Repository().GetByIdAsync(request.Id); if (itemToDelete != null) { await unitOfWork.Repository().DeleteByIdAsync(request.Id); // Clear cache entries related to contacts. await appCache.RemoveAsync($"contact_{request.Id}"); await appCache.RemoveAsync("contact_all"); logger.LogInformation("Contact with ID: {Id} deleted.", request.Id); return Result.Success(); } logger.LogWarning("No contact found with ID: {Id}", request.Id); return Result.Fail("No data to delete."); } } }