Esempio n. 1
0
        public async Task InserirAsync(EstoqueSignature signature)
        {
            var estoque = new Models.Estoque(signature.ProdutoId, signature.Quantidade);

            var existe = await _estoqueRepository.ObterPorIdAsync(estoque.ProdutoId);

            if (existe == null)
            {
                throw new ArgumentNullException("Já existe este produto em estoque. Você deve atualiar o estoque.");
            }

            await _estoqueRepository.InserirAsync(estoque);
        }
Esempio n. 2
0
        public async Task <IActionResult> Post([FromBody] EstoqueSignature signature)
        {
            try
            {
                await _estoqueService.InserirAsync(signature);

                return(Created("", ""));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Erro ao adicionar estoque");
                return(BadRequest($"Erro => {ex.Message}"));
            }
        }
Esempio n. 3
0
        public async Task <EstoqueResult> AtualizarAsync(EstoqueSignature signature)
        {
            var estoque = await _estoqueRepository.ObterPorIdAsync(signature.ProdutoId);

            if (estoque == null)
            {
                return(null);
            }

            estoque.Aumentar(signature.Quantidade);

            await _estoqueRepository.AtualizarAsync(estoque);

            return(estoque.ToResult());
        }
Esempio n. 4
0
        public async Task <IActionResult> Put(Guid id, [FromBody] EstoqueSignature signature)
        {
            try
            {
                var retorno = await _estoqueService.AtualizarAsync(signature);

                if (retorno == null)
                {
                    return(NotFound());
                }

                return(Accepted("", retorno));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Erro ao alterar estoque");
                return(BadRequest($"Erro => {ex.Message}"));
            }
        }
Esempio n. 5
0
        public async Task InserirAsync(VendaSignature signature)
        {
            var produtos = new List <Produto>();
            var total    = 0.00M;

            if (!signature.ProdutosId.Any())
            {
                throw new Exception("Para a venda, é necessário ao menos informar um produto");
            }


            foreach (var produtoId in signature.ProdutosId)
            {
                var produtoResult = await _produtoGateway.GetAsync(urlProduto, produtoId).ConfigureAwait(false);

                if (!produtoResult.Sucesso)
                {
                    throw new Exception(produtoResult.MessagemErro);
                }

                var estoque = new EstoqueSignature()
                {
                    ProdutoId  = produtoId,
                    Quantidade = 1
                };

                var estoqueResult = await _estoqueGateway.PutAsync(urlEstoque, produtoId, estoque).ConfigureAwait(false);

                if (!estoqueResult.Sucesso)
                {
                    throw new Exception(estoqueResult.MessagemErro);
                }

                produtos.Add(new Produto(produtoResult.ObjectToSerialize.descricao, produtoResult.ObjectToSerialize.preco));

                total += produtoResult.ObjectToSerialize.preco;
            }

            var pagamento = new PagamentoSignature()
            {
                NumeroCartao   = signature.Pagamento.NumeroCartao,
                NumeroParcelas = signature.Pagamento.NumeroParcelas,
                Total          = total
            };

            var response = await _pagamentoGateway.PostAsync(urlPagamento, pagamento).ConfigureAwait(false);

            if (!response.Sucesso)
            {
                throw new Exception(response.MessagemErro);
            }


            var venda = new Models.Venda(new Pagamento(response.ObjectToSerialize.pagamentoId,
                                                       response.ObjectToSerialize.numeroCartao,
                                                       response.ObjectToSerialize.numeroParcelas,
                                                       response.ObjectToSerialize.total));

            venda.AdicionarProdutos(produtos);

            await _vendaRepository.InserirAsync(venda).ConfigureAwait(false);
        }