public async void ComboBox_Changed(object sender, EventArgs e) { // Try to make API call for selected Suppliers Object and display in Textbox. Else return try { statusTextBlock.Text = ""; Suppliers selectedSupplier = (Suppliers)suppliersComboBox.SelectedItem; int supplierID = supplierList.Find(p => p.SupplierId == selectedSupplier.SupplierId).SupplierId; var supplier = await GetSupplier("https://travelexperts.azurewebsites.net/api/SuppliersAPI/" + supplierID.ToString()); var products = await GetProductsFromSupplier(selectedSupplier); myListView.ItemsSource = products; nameTextbox.Text = supplier.SupName; } catch (Exception) { return; } }
public async void addSubmit_ClickAsync(object sender, EventArgs e) { // Clear Status text when submit button is clicked statusTextBlock.Text = ""; string supplierName = nameTextbox.Text; // Validation: Name is required if (nameTextbox.Text == "") { statusTextBlock.Foreground = Brushes.DarkOrange; statusTextBlock.Text = "Missing Fields!!"; submitButton.Background = Brushes.DarkOrange; return; } // Create new Supplier object from input field to post into database var supplier = new Suppliers { SupName = nameTextbox.Text }; // Get all selected products from ListView List <Products> productsSelectedList = new List <Products>(); var selectedProducts = allListView.SelectedItems; foreach (var item in selectedProducts) { productsSelectedList.Add((Products)item); } // Make Post API call to database to insert Supplier var task = await PostSupplierAsync("https://travelexperts.azurewebsites.net/api/SuppliersAPI", supplier); var items = task; if (items != null) { // Add Products into ProductsSuppliers Table with newly added Supplier int newSupplierID = items.SupplierId; var productTask = await PostProductsForSupplierAsync(productsSelectedList, newSupplierID); if (!productTask) { statusTextBlock.Foreground = Brushes.Red; statusTextBlock.Text = "Something Went Wrong!"; submitButton.Background = Brushes.Red; return; } // Successful insert of Supplier object into database statusTextBlock.Foreground = Brushes.Green; statusTextBlock.Text = "Supplier Created!"; submitButton.Background = Brushes.Green; nameTextbox.Text = ""; } else { // Post Request failed statusTextBlock.Foreground = Brushes.Red; statusTextBlock.Text = "Unable to Add Supplier!"; submitButton.Background = Brushes.Red; } }
private async Task <bool> ReplaceProductsSupplierAsync(List <Products> productsSelectedList, Suppliers supplier) { // Delete from ProductsSuppliers table the Add again var deleteReturn = await DeleteProductsSuppliersAsync(supplier.SupplierId); var postReturn = await PostProductsForSupplierAsync(productsSelectedList, supplier.SupplierId); if (postReturn && deleteReturn) { return(true); } else { return(false); } }