public IHttpActionResult PostRestaurant(string name) { if (db.Restaurants.Where(x => x.Name == name).FirstOrDefault() != null) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { ReasonPhrase = "A restaurant with that name already exists" }; throw new HttpResponseException(resp); } var id = db.Restaurants.Max(x => x.Id) + 1; Restaurant restaurant = new Restaurant { Id = id, Name = name }; if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Restaurants.Add(restaurant); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = restaurant.Id }, restaurant); }
public IHttpActionResult PutRestaurant(int id, Restaurant restaurant) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != restaurant.Id) { return BadRequest(); } db.Entry(restaurant).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!RestaurantExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }