public IHttpActionResult PutIngredient(int id, Ingredient ingredient) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != ingredient.IngredientID) { return BadRequest(); } db.Entry(ingredient).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!IngredientExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostIngredient(Ingredient ingredient) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var exists = db.Ingredients.FirstOrDefault(a => a.Name == ingredient.Name); if ( exists != null) { return Ok(exists); } db.Ingredients.Add(ingredient); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = ingredient.IngredientID }, ingredient); }