// PUT api/<controller>/5 public HttpResponseMessage Put(int id, BaseTea baseTea) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != baseTea.Id) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } db.Entry(baseTea).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
private async void BtnUpdateBaseTea_Click(object sender, RoutedEventArgs e) { if (BaseTeaIsEmpty()) { return; } try { var baseTea = new BaseTea() { Id = (int)LblBaseTeaId.Content, Name = TxtBaseTeaName.Text }; var response = await _client.PutAsJsonAsync(Constants.BaseTeaAddress + baseTea.Id, baseTea); response.EnsureSuccessStatusCode(); LoadDataBaseTea(); } catch (Exception ex) { MessageBox.Show(Constants.GeneralError); } }
private void DataBaseTea_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (DataBaseTea.SelectedItem != null && DataBaseTea.SelectedItem.GetType() == typeof(BaseTea)) { BaseTea selectedBaseTea = (BaseTea)DataBaseTea.SelectedItem; TxtBaseTeaName.Text = selectedBaseTea.Name; LblBaseTeaId.Content = selectedBaseTea.Id; } }
// GET api/<controller>/5 public BaseTea Get(int id) { BaseTea baseTea = db.BaseTeas.Find(id); if (baseTea == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } return(baseTea); }
// POST api/<controller> public HttpResponseMessage Post(BaseTea baseTea) { if (ModelState.IsValid) { db.BaseTeas.Add(baseTea); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, baseTea); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = baseTea.Id })); return(response); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }
// DELETE api/<controller>/5 public HttpResponseMessage Delete(int id) { BaseTea baseTea = db.BaseTeas.Find(id); if (baseTea == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } db.BaseTeas.Remove(baseTea); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK, baseTea)); }