Пример #1
0
        public async Task <ActionResult <CommandResult> > Put(
            Guid id,
            [FromBody] UpdateOrdemServicoCommand command)
        {
            if (id != command.Id)
            {
                return(BadRequest(new CommandResult(false, "Id diferente do cabeçalho da requisição", null)));
            }

            return(Ok((CommandResult)await _handler.Handle(command)));
        }
Пример #2
0
        public async Task <ICommandResult> Handle(UpdateOrdemServicoCommand command)
        {
            try
            {
                var ordemServico = _ordemServicoRepository.GetByIdAsync(command.Id).GetAwaiter().GetResult();
                ordemServico.Update(command.PostoColetaId, command.PacienteId, command.Convenio, command.MedicoId, command.DataRetirada);

                foreach (var exame in ordemServico.Exames.ToList())
                {
                    if (!command.Exames.Any(x => x.Id == exame.Id))
                    {
                        ordemServico.Exames.Remove(exame);
                    }
                }
                ;

                foreach (var exame in command.Exames)
                {
                    var ordemServicoExame = ordemServico.Exames.FirstOrDefault(x => x.Id == exame.Id);

                    if (ordemServicoExame == null)
                    {
                        ordemServico.AddExame(new OrdemServicoExame(ordemServico.Id, exame.ExameId, exame.Preco));
                    }
                    else
                    {
                        ordemServicoExame.Update(exame.ExameId, exame.Preco);
                    }
                }

                if (ordemServico.IsValid)
                {
                    await _ordemServicoRepository.UpdateAsync(ordemServico);

                    await _uow.CommitAsync();
                }

                return(new CommandResult(true, "Ordem de servico atualizada com sucesso", ordemServico));
            }
            catch (Exception e)
            {
                await _uow.RollbackAsync();

                return(new CommandResult(false, $"Problema com a atualização, tente mais tarde. Erro ${e.Message}", null));
            }
        }
Пример #3
0
        public Task <CommandResult> Handle(UpdateOrdemServicoCommand command, CancellationToken cancellationToken)
        {
            var cliente = _clienteRepository.GetById(command.ClienteId);

            if (cliente == null)
            {
                NotifyCommandError("Cliente não encontrado", "Erro de repositório");
                return(Task.FromResult(_commandResponse));
            }

            OrdemServico ordemServico = new OrdemServico(cliente, command.Produtos);

            _ordemServicoRepository.Update(ordemServico);

            if (Commit())
            {
                _mediator.Publish(new UpdatedOrdemServicoEvent());
            }

            return(Response());
        }