public PlanUpdate UpdateModelLoadSteps(PlanUpdate model) { using (var ctx = new ApplicationDbContext()) { var query = ctx .Steps .Where(e => e.IsSaved == true) .Select(e => new StepListItem { StepId = e.StepId, StepType = e.StepType, Name = e.Name, ImageLink = e.ImageLink }).ToList(); var Meats = query.Where(m => m.StepType == Data.Steps.StepTypes.Meat).ToList(); var Cuts = query.Where(m => m.StepType == Data.Steps.StepTypes.Cut).ToList(); var CharcoalSetups = query.Where(m => m.StepType == Data.Steps.StepTypes.CharcoalSetup).ToList(); if (Meats.Count == 0 || Cuts.Count == 0 || CharcoalSetups.Count == 0) { throw new Exception(); } model.Meats = Meats; model.Cuts = Cuts; model.CharcoalSetups = CharcoalSetups; } return(model); }
public ActionResult Edit(int id) { var service = CreatePlanService(); try { var detail = service.GetOwnedPlanById(id); var model = new PlanUpdate { PlanId = detail.PlanId, Title = detail.Title, StepOneId = detail.StepOne.StepId, StepTwoId = detail.StepTwo.StepId, StepThreeId = detail.StepThree.StepId }; return(View(service.UpdateModelLoadSteps(model))); } catch (InvalidOperationException) { TempData["NoResult"] = "The Plan could not be found."; return(RedirectToAction("Index")); } catch (UnauthorizedAccessException) { TempData["NotOwner"] = "You can only Edit Plans that you created."; return(RedirectToAction("Index")); } catch (Exception) { TempData["NeedSteps"] = "A Plan cannot be made if there are not at least one of each step."; return(RedirectToAction("Index")); } }
public bool UpdatePlan(PlanUpdate model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .Plans .Single(e => e.PlanId == model.PlanId && e.UserId == _userId); var stepOne = ctx .Steps .Single(o => o.StepId == model.StepOneId); var stepTwo = ctx .Steps .Single(o => o.StepId == model.StepTwoId); var stepThree = ctx .Steps .Single(o => o.StepId == model.StepThreeId); entity.Title = model.Title; entity.StepOne = stepOne; entity.StepTwo = stepTwo; entity.StepThree = stepThree; return(ctx.SaveChanges() >= 1); } }
public ActionResult Edit(int id, PlanUpdate model) { var service = CreatePlanService(); if (!ModelState.IsValid) { return(View(service.UpdateModelLoadSteps(model))); } if (model.PlanId != id) { ModelState.AddModelError("", "ID Mismatch"); return(View(service.UpdateModelLoadSteps(model))); } if (service.UpdatePlan(model)) { TempData["SaveResult"] = "Your Plan was updated."; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Your Plan could not be updated."); return(View(service.UpdateModelLoadSteps(model))); }