public async ValueTask <ICommandResult> Handle(CadastrarReservaCommand command)
        {
            if (!command.Validate())
            {
                AddNotifications(command);
                return(new CommandResult(false, base.Notifications));
            }
            var entityReservaSimulada = await _reservaRepository.GetByCodigoReserva(command.CodigoReserva).ConfigureAwait(true);

            var entityVeiculo = await _veiculoRepository.GetById(command.VeiculosId).ConfigureAwait(true);

            var entityCliente = await _clienteRepository.GetById(command.ClienteId).ConfigureAwait(true);

            if (entityCliente == null)
            {
                AddNotification("Cliente", "Cliente nao Encontrado.");
                return(new CommandResult(false, base.Notifications));
            }
            if (entityVeiculo == null)
            {
                AddNotification("Veiculo", "Veiculo Nao Encontrado");
                return(new CommandResult(false, base.Notifications));
            }
            if (entityVeiculo.Reservado)
            {
                AddNotification("Veiculo", "Este Veiculo Esta Reservado.");
                return(new CommandResult(false, base.Notifications));
            }
            if (entityReservaSimulada != null)
            {
                var reservaSimulada = new Reserva(command.Agencia, entityCliente.Id, entityVeiculo.Id, command.Grupo, command.DataInicioReserva,
                                                  command.DataFimReserva, command.Diarias, false);

                reservaSimulada.Simulado = false;
                await _reservaRepository.Add(reservaSimulada);
            }
            else
            {
                var entityReserva = new Reserva(command.Agencia, entityCliente.Id, entityVeiculo.Id, command.Grupo, command.DataInicioReserva,
                                                command.DataFimReserva, command.Diarias, false);
                await _reservaRepository.Add(entityReserva);
            }

            var result = await _reservaRepository.SaveChanges().ConfigureAwait(true);

            if (!result)
            {
                AddNotification("Reserva", "Erro ao Inserir Reserva");
                return(new CommandResult(false, base.Notifications));
            }
            return(new CommandResult(true));
        }
Example #2
0
        public async Task <IActionResult> CriarReservaAsync([FromBody] CadastrarReservaCommand command)
        {
            var result = await _commandReserva.Handle(command).ConfigureAwait(true) as CommandResult;

            if (result.Success)
            {
                return(Ok());
            }
            else
            {
                return(UnprocessableEntity(result.Messages));
            }
        }