public void GetComposers(ComposerList clist, ExpressionList elist) { if (clist == null || elist == null) { return; } for (int i = 0, n = elist.Count; i < n; i++) { clist.Add(this.GetComposer(elist[i])); } }
private void btnSave_Click(object sender, RoutedEventArgs e) { try { if ((string)btnSave.Content == "Add Composer") { // Add the composer // Make sure that the fields are filled out that cannot be null if (string.IsNullOrEmpty(txtFirstName.Text)) { throw new Exception("Composer must have a first name"); } if (string.IsNullOrEmpty(txtLastName.Text)) { throw new Exception("Composer must have a last name"); } // Create and set values on a composer Composer composer = new Composer(); composer.FirstName = txtFirstName.Text; composer.LastName = txtLastName.Text; if (!string.IsNullOrEmpty(txtBio.Text)) { composer.Bio = txtBio.Text; } else { composer.Bio = string.Empty; } if (cboLocation.SelectedValue != null) { composer.LocationId = (Guid)cboLocation.SelectedValue; } if (cboGender.SelectedValue != null) { composer.GenderId = (Guid)cboGender.SelectedValue; } if (cboRace.SelectedValue != null) { composer.RaceId = (Guid)cboRace.SelectedValue; } // Send it to the api HttpClient client = InitializeClient(); string serializedComposer = JsonConvert.SerializeObject(composer); var content = new StringContent(serializedComposer); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); HttpResponseMessage response = client.PostAsync("Composer", content).Result; if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent) { // get the new composer id response = client.GetAsync("Composer?name=" + composer.LastName).Result; string result = response.Content.ReadAsStringAsync().Result; Composer retrievedComposer = JsonConvert.DeserializeObject <Composer>(result); // Save the id so that we can update it composer.Id = retrievedComposer.Id; composers.Add(composer); Rebind(); cboComposer.SelectedIndex = composers.FindIndex(c => c == composer); } else { throw new Exception("Composer could not be inserted"); } } else { // Update the composer // Make sure that the fields are filled out that cannot be null if (string.IsNullOrEmpty(txtFirstName.Text)) { throw new Exception("Composer must have a first name"); } if (string.IsNullOrEmpty(txtLastName.Text)) { throw new Exception("Composer must have a last name"); } // Create and set values on a composer Composer composer = new Composer(); composer = composers[cboComposer.SelectedIndex]; composer.FirstName = txtFirstName.Text; composer.LastName = txtLastName.Text; if (!string.IsNullOrEmpty(txtBio.Text)) { composer.Bio = txtBio.Text; } else { composer.Bio = string.Empty; } if (cboLocation.SelectedValue != null) { composer.LocationId = (Guid)cboLocation.SelectedValue; } if (cboGender.SelectedValue != null) { composer.GenderId = (Guid)cboGender.SelectedValue; } if (cboRace.SelectedValue != null) { composer.RaceId = (Guid)cboRace.SelectedValue; } // Send it to the api HttpClient client = InitializeClient(); string serializedComposer = JsonConvert.SerializeObject(composer); var content = new StringContent(serializedComposer); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); HttpResponseMessage response = client.PutAsync("Composer/" + composer.Id, content).Result; if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent) { //Save index, refresh screen, and reselect where we are. var index = cboComposer.SelectedIndex; Rebind(); cboComposer.SelectedIndex = index; MessageBox.Show("Composer Saved.", "Success"); } else { throw new Exception("Composer could not be updated"); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error!"); } }
public void GetComposers(ComposerList clist, ExpressionList elist) { if (clist == null || elist == null) return; for( int i = 0, n = elist.Count; i < n; i++ ) { clist.Add(this.GetComposer(elist[i])); } }