public int SaveFruit(FruitMapper fruit) { var newFruit = new Fruit(); if (fruit.Id != null) { newFruit = context.Fruits.Find(fruit.Id); } newFruit.Name = fruit.Name; newFruit.Quantity = fruit.Quantity; if (fruit.Id == null) { context.Entry(newFruit).State = System.Data.Entity.EntityState.Added; } else { context.Entry(newFruit).State = System.Data.Entity.EntityState.Modified; } if (context.SaveChanges() > 0) { return(newFruit.Id); } return(0); }
public IHttpActionResult PostFruit(FruitMapper newFruit) { var result = service.SaveFruit(newFruit); return(Ok <int>(result)); }