using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.DeleteFactorDetails;
public class DeleteFactorDetailsCommandHandler : IRequestHandler<DeleteFactorDetailsCommand, Unit>
{
    private readonly IApplicationDbContext _context;

    public DeleteFactorDetailsCommandHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<Unit> Handle(DeleteFactorDetailsCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.FactorDetails
            .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(FactorDetails), request.Id);
        entity.IsDeleted = true;
        _context.FactorDetails.Update(entity);
        entity.AddDomainEvent(new DeleteFactorDetailsEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return Unit.Value;
    }
}
