public IHttpActionResult Update(Rancher rancher) { RancherResponse response = new RancherResponse(); try { Rancher rancherSaved = rancherBL.UpdateRancher(rancher); response.Rancher = rancherSaved; response.Success = true; } catch (RancherException ex) { response.ErrorCode = ex.Error; response.ErrorMessage = "Error. " + ex.Error.ToString(); response.Rancher = null; response.Success = false; } catch (Exception ex) { response.ErrorMessage = "Error. " + ex.Message; response.Rancher = null; response.Success = false; } return(Ok(response)); }
public void SaveRancher() { bool reLoadList = false; HttpClient client = new HttpClient(); client.BaseAddress = new Uri(baseUrl); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/bson")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token.access_token); string action = string.Empty; if (rancherView.CurrentRancher.Id == 0) { //insert action = insertAction; reLoadList = true; } else { action = updateAction; } //update MediaTypeFormatter bsonFormatter = new BsonMediaTypeFormatter(); HttpResponseMessage response = client.PostAsync(action, rancherView.CurrentRancher, bsonFormatter).Result; response.EnsureSuccessStatusCode(); MediaTypeFormatter[] formatters = new MediaTypeFormatter[] { bsonFormatter }; RancherResponse rancherResponse = response.Content.ReadAsAsync <RancherResponse>(formatters).Result; if (rancherResponse.Success) { if (rancherResponse.Rancher != null) { if (reLoadList) { LoadRanchers(); rancherView.SelectedId = rancherResponse.Rancher.Id; } } } else { throw new RancherException(rancherResponse.ErrorCode, rancherResponse.ErrorMessage); } }
public void DeleteRancher() { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(baseUrl); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token.access_token); HttpResponseMessage response = client.PostAsJsonAsync(deleteAction, new IdModel(rancherView.SelectedId)).Result; if (response.IsSuccessStatusCode) { RancherResponse rancherResponse = response.Content.ReadAsAsync <RancherResponse>().Result; if (rancherResponse.Success) { //Deleted! LoadRanchers(); PropertyCopier.CopyProperties(new Rancher(), rancherView.CurrentRancher); rancherView.CurrentRancher.RaiseUpdateProperties(); rancherView.SelectedId = -1; } } }
public IHttpActionResult Delete(IdModel id) { RancherResponse response = new RancherResponse(); try { bool success = rancherBL.DeleteRancher(id.Id); response.Success = success; } catch (RancherException ex) { response.ErrorCode = ex.Error; response.ErrorMessage = "Error. " + ex.Error.ToString(); response.Rancher = null; response.Success = false; } catch (Exception ex) { response.ErrorMessage = "Error. " + ex.Message; response.Rancher = null; response.Success = false; } return(Ok(response)); }