public async Task <PagamentoResult> InserirAsync(PagamentoSignature signature) { var pagamento = new Models.Pagamento(signature.NumeroCartao, signature.NumeroParcelas, signature.Total); if (!pagamento.ValidarCartao()) { throw new System.ArgumentException("Cartão de crédito inválido"); } await _pagamentoRepository.InserirAsync(pagamento); return(pagamento.ToResult()); }
public async Task <IActionResult> Post([FromBody] PagamentoSignature signature) { try { var pagamento = await _pagamentoService.InserirAsync(signature); return(Created("", pagamento)); } catch (ArgumentException ex) { _logger.LogError(ex.ToString()); return(BadRequest($"Pagamento => {ex.Message}")); } }
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); }