Exemplo n.º 1
0
        private void BtnInsert_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ScreenMode.Location == screenmode)
                {
                    if (string.IsNullOrEmpty(txtDescription.Text))
                    {
                        throw new Exception("Please enter a location");
                    }

                    Location location = new Location();

                    if (locations.Any(l => l.Description == txtDescription.Text))
                    {
                        throw new Exception("This location has already exists.");
                    }
                    else
                    {
                        location.Description = txtDescription.Text;
                    }

                    //Send it to the API
                    HttpClient client             = InitializeClient();
                    string     serializedLocation = JsonConvert.SerializeObject(location);
                    var        content            = new StringContent(serializedLocation);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    HttpResponseMessage response = client.PostAsync("Location", content).Result;

                    if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent)
                    {
                        //Get the new location Id
                        response = client.GetAsync("Location?description=" + location.Description).Result;
                        string   result            = response.Content.ReadAsStringAsync().Result;
                        Location retrievedLocation = JsonConvert.DeserializeObject <Location>(result);

                        //Save the Id so that we can update it.
                        location.Id = retrievedLocation.Id;

                        locations.Add(location);
                        Rebind();

                        //Select the inserted location
                        cboAttribute.SelectedIndex = locations.FindIndex(p => p == location);
                    }
                    else
                    {
                        throw new Exception("Location could not be inserted");
                    }
                }
                else if (ScreenMode.Gender == screenmode)
                {
                    if (string.IsNullOrEmpty(txtDescription.Text))
                    {
                        throw new Exception("Please enter a gender");
                    }

                    Gender gender = new Gender();

                    if (genders.Any(l => l.Description == txtDescription.Text))
                    {
                        throw new Exception("This gender has already exists.");
                    }
                    else
                    {
                        gender.Description = txtDescription.Text;
                    }

                    //Send it to the API
                    HttpClient client           = InitializeClient();
                    string     serializedGender = JsonConvert.SerializeObject(gender);
                    var        content          = new StringContent(serializedGender);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    HttpResponseMessage response = client.PostAsync("Gender", content).Result;

                    if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent)
                    {
                        //Get the new location Id
                        response = client.GetAsync("Gender?description=" + gender.Description).Result;
                        string result          = response.Content.ReadAsStringAsync().Result;
                        Gender retrievedGender = JsonConvert.DeserializeObject <Gender>(result);

                        //Save the Id so that we can update it.
                        gender.Id = retrievedGender.Id;

                        genders.Add(gender);
                        Rebind();

                        //Select the inserted location
                        cboAttribute.SelectedIndex = genders.FindIndex(p => p == gender);
                    }
                    else
                    {
                        throw new Exception("Gender could not be inserted");
                    }
                }
                else if (ScreenMode.Race == screenmode)
                {
                    if (string.IsNullOrEmpty(txtDescription.Text))
                    {
                        throw new Exception("Please enter a race");
                    }

                    Race race = new Race();

                    if (races.Any(l => l.Description == txtDescription.Text))
                    {
                        throw new Exception("This race has already exists.");
                    }
                    else
                    {
                        race.Description = txtDescription.Text;
                    }

                    //Send it to the API
                    HttpClient client         = InitializeClient();
                    string     serializedRace = JsonConvert.SerializeObject(race);
                    var        content        = new StringContent(serializedRace);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    HttpResponseMessage response = client.PostAsync("Race", content).Result;

                    if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent)
                    {
                        //Get the new location Id
                        response = client.GetAsync("Race?description=" + race.Description).Result;
                        string result        = response.Content.ReadAsStringAsync().Result;
                        Race   retrievedRace = JsonConvert.DeserializeObject <Race>(result);

                        //Save the Id so that we can update it.
                        race.Id = retrievedRace.Id;


                        races.Add(race);
                        Rebind();


                        //Select the inserted location
                        cboAttribute.SelectedIndex = races.FindIndex(p => p == race);
                    }
                    else
                    {
                        throw new Exception("Race could not be inserted");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!");
            }
        }