// POST api/Cities public HttpResponseMessage PostCity(City city) { if (ModelState.IsValid) { //todo: exampin why is the need to always go back up the tree.. Country country = db.Countries.Where(x => x.ID == city.Country).SingleOrDefault(); country.User = db.Users.Where(x => x.Id == country.UserId).SingleOrDefault(); city.Country1 = country; db.Cities.Add(city); try { db.SaveChanges(); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.Created); } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/Cities/5 public HttpResponseMessage PutCity(int id, City city) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != city.ID) { return Request.CreateResponse(HttpStatusCode.BadRequest); } //todo: exampin why is the need to always go back up the tree.. Country country = db.Countries.Where(x => x.ID == city.Country).SingleOrDefault(); country.User = db.Users.Where(x => x.Id == country.UserId).SingleOrDefault(); city.Country1 = country; db.Entry(city).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }