using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.UserContractCQ.Commands.CreateNewUserContract;
public class CreateNewUserContractCommandHandler : IRequestHandler<CreateNewUserContractCommand, CreateNewUserContractResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewUserContractResponseDto> Handle(CreateNewUserContractCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<UserContract>();
        await _context.UserContracts.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewUserContractEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewUserContractResponseDto>();
    }
}
