private void BtnAddProduct_Click(object sender, RoutedEventArgs e) { ProductManagerServiceClient client = null; try { client = new ProductManagerServiceClient(); client.Open(); if (!decimal.TryParse(txtPrice.Text, out var price)) { //show error MessageBox.Show("Invalid price! Only numbers accepted"); return; } if (!int.TryParse(txtQuantity.Text, out var quantity)) { //show error MessageBox.Show("Invalid quantity! Only numbers accepted"); return; } client.AddProduct( txtName.Text, txtDescription.Text, price, quantity ); MessageBox.Show("Success"); } catch (FaultException <ArgumentException> exception) { MessageBox.Show(exception.Message ?? "Something went wrong"); } catch (Exception exception) { MessageBox.Show("TESTES"); } finally { client?.Close(); } }
static void Main(string[] args) { //Obtem produtos do WCF ProductManagerServiceClient clientProducts = null; OrderManagerServiceClient clientOrders = null; try { clientProducts = new ProductManagerServiceClient(); clientProducts.Open(); var stayInMenu = true; do { Console.WriteLine("Enter L to list all products, B to buy one of each, anything else to exit"); var enteredInput = Console.ReadLine()?.ToUpperInvariant(); switch (enteredInput) { case "L": PrintAllProducts(clientProducts.GetAllAvailableProducts()); break; case "B": clientOrders = BuyOneOfEachProduct(clientProducts.GetAllAvailableProducts()); break; default: stayInMenu = false; break; } } while (stayInMenu); Console.ReadLine(); } finally { clientProducts?.Close(); clientOrders?.Close(); } }
public void CreateOrder(Product[] products) { //Obtem produtos do WCF ProductManagerServiceClient client = null; try { client = new ProductManagerServiceClient(); client.Open(); var successProducts = new List <Product>(); var productsInStock = client.GetAllAvailableProducts().ToDictionary(x => x.Id); foreach (var product in products) { if (!productsInStock .TryGetValue(product.Id, out var productInStock)) { throw new OrderStockException($"Product {product.Name} not found!"); } if (productInStock.Quantity < product.Quantity) { throw new OrderStockException($"{product.Name} is out of Stock"); } productInStock.Quantity = productInStock.Quantity - product.Quantity; successProducts.Add(product); } foreach (var product in productsInStock.Values) { client.UpdateProduct(product); } Orders.Enqueue(successProducts); } finally { client?.Close(); } }
private void btnRefresh_Click(object sender, RoutedEventArgs e) { //Obtem produtos do WCF ProductManagerServiceClient client = null; try { client = new ProductManagerServiceClient(); client.Open(); var products = client.GetAllProducts(); listProducts.Items.Clear(); foreach (var product in products) { listProducts.Items .Add($"{product.Name} - R${product.Price} - {product.Quantity}"); } } finally { client?.Close(); } }