using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ContractCQ.Commands.CreateNewContract;
public class CreateNewContractCommandHandler : IRequestHandler<CreateNewContractCommand, CreateNewContractResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewContractResponseDto> Handle(CreateNewContractCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<Contract>();
        await _context.Contracts.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewContractEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewContractResponseDto>();
    }
}
