コード例 #1
0
        public async Task <RequestResult> Handle(EditarPretendenteCommand request, CancellationToken cancellationToken)
        {
            var pretendenteDb = await _pretendenteRepositorio.ObterAsync(request.Id);

            if (pretendenteDb == null)
            {
                _logger.LogError($"[Editar pretendente] - pretendente {request.Id} não encontrato.");
                return(new RequestResult().NotFound());
            }

            pretendenteDb.AtualizarNome(request.Nome);

            var validate = new PretendenteValidator(_pretendenteRepositorio).Validate(pretendenteDb);

            if (!validate.IsValid)
            {
                _logger.LogError("[Editar pretendente] - " + validate.Errors.FirstOrDefault().ErrorMessage);
                return(new RequestResult().BadRequest(validate.Errors.Select(p => p.ErrorMessage).ToArray()));
            }

            await _pretendenteRepositorio.AtualizarAsync(pretendenteDb);

            await _bus.Publish(new PretendenteEditadoEvent(request.Id, request.Nome), cancellationToken);

            return(new RequestResult().Ok());
        }
コード例 #2
0
        public async Task <RequestResult> Handle(CriarPretendenteCommand request, CancellationToken cancellationToken)
        {
            var pretendente = new Domain.Entities.Pretendente(request.Nome, request.Email);
            var validate    = new PretendenteValidator(_pretendenteRepositorio).Validate(pretendente);

            if (!validate.IsValid)
            {
                _logger.LogError("[Adicionar pretendente] - " + validate.Errors.FirstOrDefault().ErrorMessage);
                return(new RequestResult().BadRequest(validate.Errors.Select(p => p.ErrorMessage).ToArray()));
            }

            await _pretendenteRepositorio.AdicionarAsync(pretendente);

            await _bus.Publish(new PretendenteCriadoEvent(request.Nome, request.Email), cancellationToken);

            return(new RequestResult().Ok());
        }