Esempio n. 1
0
 public async Task <IActionResult> BuscarPorId(int id)
 {
     try
     {
         var result = _service.GetById(id);
         return(Ok(result));
     }
     catch (BusinessException ex)
     {
         return(BadRequest(ex.Message));
     }
     catch (Exception ex)
     {
         return(BadRequest());
     }
 }
Esempio n. 2
0
 public IActionResult GetComandaById(string Id)
 {
     try
     {
         return(new JsonResult(_service.GetById(Id))
         {
             StatusCode = 200
         });
     }
     catch (Exception e)
     {
         return(new JsonResult(BadRequest(e.Message))
         {
             StatusCode = 400
         });
     }
 }
Esempio n. 3
0
        private async Task TesteDeCrudIntegracaoAsync()
        {
            IComandaService service        = serviceProvider.GetRequiredService <IComandaService>();
            IUsuarioService usuarioService = serviceProvider.GetRequiredService <IUsuarioService>();
            IProdutoService produtoService = serviceProvider.GetRequiredService <IProdutoService>();

            Comanda comandaNovo = null;
            Usuario usuario     = null;
            Produto produto     = null;
            Produto produto2    = null;

            try
            {
                usuario = await ObterUsuario(usuarioService);

                produto = await ObterProduto(produtoService, $"Produto pedido");

                produto2 = await ObterProduto(produtoService, $"Produto pedido 2");

                Comanda comanda = new Comanda();
                comanda.Garcom = usuario;
                comanda.AdicionarPedido(produto, 1);
                comandaNovo = await service.Add(comanda);

                service.Result.Any().Should().BeFalse();

                Comanda comandaPedidoEmPreparo = await service.AlterarSituacaoPedido(comandaNovo.Id, ComandaPedidoSituacao.Preparando);

                comandaPedidoEmPreparo.Pedidos.All(c => c.Situacao == ComandaPedidoSituacao.Preparando).Should().BeTrue();

                Comanda comandaPedidoPronto = await service.AlterarSituacaoPedido(comandaNovo.Id, ComandaPedidoSituacao.Pronto);

                comandaPedidoPronto.Pedidos.All(c => c.Situacao == ComandaPedidoSituacao.Pronto).Should().BeTrue();

                Comanda comandaPedidoCancelado = await service.AlterarSituacaoPedido(comandaNovo.Id, ComandaPedidoSituacao.Cancelado);

                comandaPedidoCancelado.Pedidos.All(c => c.Situacao == ComandaPedidoSituacao.Cancelado).Should().BeTrue();

                IList <Comanda> comandasAbertas = await service.TotasComandasAbertas();

                comandasAbertas.Any().Should().BeTrue();

                IQueryable <Comanda> comandasAbertasQueryable = service.ComandasAbertas();
                comandasAbertasQueryable.Any().Should().BeTrue();

                comandaNovo = await comandasAbertasQueryable.Where(c => c.Id == comandaNovo.Id)
                              .Include(c => c.Garcom)
                              .Include(c => c.Pedidos)
                              .ThenInclude(c => c.Produto)
                              .FirstOrDefaultAsync();

                comandaNovo.Should().NotBeNull();
                comandaNovo.Id.Should().NotBe(0);
                comandaNovo.Garcom.Nome.Should().Be(usuario.Nome);
                comandaNovo.AdicionarPedido(produto2, 1);
                comandaNovo.FecharConta();
                comandaNovo.Situacao.Should().Be(ComandaSituacao.Fechada);
                comandaNovo.TotalAPagar.Should().Be(20);
                comandaNovo.GorjetaGarcom.Should().Be(2.0M);

                bool resultadoEdicao = await service.Edity(comandaNovo);

                resultadoEdicao.Should().BeTrue();

                IEnumerable <Comanda> entidades = await service.GetAll();

                entidades.Any().Should().BeTrue();

                Comanda comandaById = await service.GetById(comandaNovo.Id);

                comandaById.Should().NotBeNull();

                bool exists = service.Exists(comandaNovo.Id);
                exists.Should().BeTrue();
            }
            finally
            {
                if (comandaNovo != null)
                {
                    bool resultadoExclusao = await service.Delete(comandaNovo.Id);

                    resultadoExclusao.Should().BeTrue();
                }

                if (usuario != null)
                {
                    bool resultadoExclusao = await usuarioService.Delete(usuario.Id);

                    resultadoExclusao.Should().BeTrue();
                }

                if (produto != null)
                {
                    bool resultadoExclusao = await produtoService.Delete(produto.Id);

                    resultadoExclusao.Should().BeTrue();
                }

                if (produto2 != null)
                {
                    bool resultadoExclusao = await produtoService.Delete(produto2.Id);

                    resultadoExclusao.Should().BeTrue();
                }
            }
        }