using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.CreateNewFactorDetails;
public class CreateNewFactorDetailsCommandHandler : IRequestHandler<CreateNewFactorDetailsCommand, CreateNewFactorDetailsResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewFactorDetailsResponseDto> Handle(CreateNewFactorDetailsCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<FactorDetails>();
        await _context.FactorDetails.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewFactorDetailsEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewFactorDetailsResponseDto>();
    }
}
