// GET api/<controller>/5 public Models.Customer Get(int id) { try { using (var db = new Models.MapStratDBEntities()) { var customer = db.Customers.FirstOrDefault(f => f.Id == id); if (customer == null) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(string.Format("No Customer found with ID = {0}", id)) }; throw new HttpResponseException(resp); } return customer; } } catch (HttpResponseException ex) { throw ex; } catch (Exception ex) { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(ex.Message) }; throw new HttpResponseException(resp); } }
// GET api/<controller> public IEnumerable<Models.Customer> Get() { try { using (var db = new Models.MapStratDBEntities()) { return db.Customers.ToArray(); } } catch (Exception ex) { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(ex.Message) }; throw new HttpResponseException(resp); } }
// PUT api/<controller>/5 public Models.Customer Put(int id, [FromBody]Models.Customer value) { try { using (var db = new Models.MapStratDBEntities()) { var customer = db.Customers.FirstOrDefault(f => f.Id == id); if (customer == null) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(string.Format("No Customer found with ID = {0}", id)) }; throw new HttpResponseException(resp); } customer.Name = value.Name; customer.Address = value.Address; customer.PhoneNumber = value.PhoneNumber; db.SaveChanges(); return customer; } } catch (HttpResponseException ex) { throw ex; } catch (Exception ex) { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(ex.Message) }; throw new HttpResponseException(resp); } }
// PUT api/<controller> public Models.Customer Post([FromBody]Models.Customer value) { { try { using (var db = new Models.MapStratDBEntities()) { db.Customers.Add(value); db.SaveChanges(); return value; } } catch (Exception ex) { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(ex.Message) }; throw new HttpResponseException(resp); } } }