// POST api/Countries public HttpResponseMessage PostCountry(Country country) { if (ModelState.IsValid) { country.User = db.Users.Where(x => x.Id == country.UserId).SingleOrDefault(); db.Countries.Add(country); try { db.SaveChanges(); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = country.ID })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/Countries/5 public HttpResponseMessage PutCountry(int id, Country country) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != country.ID) { return Request.CreateResponse(HttpStatusCode.BadRequest); } country.User = db.Users.Where(x => x.Id == country.UserId).SingleOrDefault(); db.Entry(country).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }