public async Task InsertAsync(SaleInput input) { var itens = new List <Item>(); var total = 0.00M; if (input.Itens.Count <= 0) { throw new Exception("Para a venda, é necessário pelo menos informar um item"); } foreach (var itemId in input.Itens) { var item = await _itemRepository .GetByIdAsync(itemId) .ConfigureAwait(false); var stock = await _stockGateway .PutAsync(urlStock, itemId) .ConfigureAwait(false); if (!stock.Success) { throw new Exception(stock.ErrorMessage); } itens.Add(item); total += item.Price; } var payment = new PaymentInput() { CardNumber = input.Payment.CardNumber, SubDivision = input.Payment.SubDivision, Total = total }; var response = await _paymentGateway .PostAsync(urlPayment, payment) .ConfigureAwait(false); if (!response.Success) { throw new Exception(response.ErrorMessage); } var sale = new Sale(new Payment(response.ReadToObject.Id, response.ReadToObject.CardNumber, response.ReadToObject.SubDivision, response.ReadToObject.Total, response.ReadToObject.TotalSubDivision)); sale.AddItems(itens); await _saleRepository .InsertAsync(sale) .ConfigureAwait(false); }