private bool SetStarState(int foodItemId, bool newState) { // Create the service var userId = User.Identity.GetUserId(); var service = new FoodItemService(userId); // Get the food item var detail = service.GetFoodItemById(foodItemId); // Create the FoodItemEdit model instance with the new star state var updatedFoodItem = new FoodItemEdit { FoodItemId = detail.FoodItemId, Name = detail.Name, Description = detail.Description, Calories = detail.Calories, CarbohydrateGrams = detail.CarbohydrateGrams, FiberGrams = detail.FiberGrams, FatGrams = detail.FatGrams, ProteinGrams = detail.ProteinGrams, SodiumMilligrams = detail.SodiumMilligrams, PotassiumMilligrams = detail.PotassiumMilligrams, IsStarred = newState }; // Return a value indicating whether the update succeeded return(service.UpdateFoodItem(updatedFoodItem)); }
public IHttpActionResult Put(FoodItemEdit foodItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateFoodItemServices(); if (!service.UpdateFoodItem(foodItem)) { return(InternalServerError()); } return(Ok()); }
public bool UpdateFoodItem(FoodItemEdit model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .FoodItems .Single(e => e.FoodId == model.FoodId && e.UserId == _userId); entity.FoodName = model.FoodName; entity.FoodId = model.FoodId; entity.Carbs = model.Carbs; entity.Protein = model.Protein; entity.Fat = model.Fat; entity.ModifiedUtc = DateTimeOffset.UtcNow; return(ctx.SaveChanges() > 0); } }
public ActionResult Edit(int id) { var service = CreateFoodItemService(); var detail = service.GetFoodItemById(id); var model = new FoodItemEdit { FoodItemId = detail.FoodItemId, Name = detail.Name, Description = detail.Description, Calories = detail.Calories, CarbohydrateGrams = detail.CarbohydrateGrams, FiberGrams = detail.FiberGrams, FatGrams = detail.FatGrams, ProteinGrams = detail.ProteinGrams, SodiumMilligrams = detail.SodiumMilligrams, PotassiumMilligrams = detail.PotassiumMilligrams, }; return(View(model)); }
public bool UpdateFoodItem(FoodItemEdit model) { var entity = _db .FoodItems .Single(e => e.FoodItemId == model.FoodItemId); entity.Name = model.Name; entity.Description = model.Description; entity.Calories = model.Calories; entity.CarbohydrateGrams = model.CarbohydrateGrams; entity.FiberGrams = model.FiberGrams; entity.FatGrams = model.FatGrams; entity.ProteinGrams = model.ProteinGrams; entity.SodiumMilligrams = model.SodiumMilligrams; entity.PotassiumMilligrams = model.PotassiumMilligrams; entity.IsStarred = model.IsStarred; entity.ModifiedBy = _db.Users.Find(_userId).FirstName + " " + _db.Users.Find(_userId).LastName; entity.ModifiedUtc = DateTimeOffset.UtcNow; return(_db.SaveChanges() == 1); }
public ActionResult Edit(int id, FoodItemEdit model) { if (!ModelState.IsValid) { return(View(model)); } if (model.FoodItemId != id) { ModelState.AddModelError("", "Id Mismatch"); return(View(model)); } var service = CreateFoodItemService(); if (service.UpdateFoodItem(model)) { TempData["SaveResult"] = "Your food item was updated."; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Your food item could not be updated."); return(View(model)); }