public async Task <Models.Subdivision> DeleteAsync(int idCountry, int idSubdivision) { Models.Subdivision data = null; try { var uri = new Uri($"{this.URL}/countries/{idCountry}/subdivisions/{idSubdivision}"); var response = await cliente.DeleteAsync(uri); if (response.IsSuccessStatusCode) { var contentResponse = await response.Content.ReadAsStringAsync(); data = JsonConvert.DeserializeObject <Models.Subdivision>(contentResponse); } if ((int)response.StatusCode > 400) { data = new Models.Subdivision(); var contentResponse = await response.Content.ReadAsStringAsync(); data.Error = JsonConvert.DeserializeObject <Models.Error>(contentResponse); } } catch (Exception ex) { Console.Write(ex.Message); } return(data); }
public async Task <Models.Subdivision> SaveAsync(int idCountry, Models.Subdivision subdivision) { Models.Subdivision data = null; try { if (subdivision.id.HasValue) { return(await EditAsync(idCountry, subdivision)); } var uri = new Uri($"{this.URL}/countries/{idCountry}/subdivisions"); var json = JsonConvert.SerializeObject(subdivision); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await cliente.PostAsync(uri, content); if (response.IsSuccessStatusCode) { var contentResponse = await response.Content.ReadAsStringAsync(); var root = JsonConvert.DeserializeObject <Models.RootSingleSubdivision>(contentResponse); if (root != null && root.data != null) { data = root.data; } } if ((int)response.StatusCode > 400) { data = new Models.Subdivision(); var contentResponse = await response.Content.ReadAsStringAsync(); data.Error = JsonConvert.DeserializeObject <Models.Error>(contentResponse); } } catch (Exception ex) { Console.Write(ex.Message); } return(data); }
public async Task <Models.Subdivision> EditAsync(int idCountry, Models.Subdivision subdivision) { Models.Subdivision data = null; try { var uri = new Uri($"{this.URL}/countries/{idCountry}/subdivisions/{subdivision.id}"); var json = JsonConvert.SerializeObject(subdivision); var content = new StringContent(json, Encoding.UTF8, "application/json"); var request = new HttpRequestMessage(new HttpMethod("PATCH"), uri); request.Content = content; var response = await cliente.SendAsync(request); if (response.IsSuccessStatusCode) { var contentResponse = await response.Content.ReadAsStringAsync(); var root = JsonConvert.DeserializeObject <Models.RootSingleSubdivision>(contentResponse); if (root != null && root.data != null) { data = root.data; } } if ((int)response.StatusCode > 400) { data = new Models.Subdivision(); var contentResponse = await response.Content.ReadAsStringAsync(); data.Error = JsonConvert.DeserializeObject <Models.Error>(contentResponse); } } catch (Exception ex) { Console.Write(ex.Message); } return(data); }