public ActionResult Done(string email) { MealPlan userMealPlan = new MealPlan(); Person person = new Person(); Basket basket = new Basket(); PersonMealPlan personMealPlan = new PersonMealPlan(); using (var db = new MealPlanContext()) { person = db.Persons.First(p => p.Email == email); userMealPlan.IsAdmin = false; db.MealPlans.Add(userMealPlan); foreach (var b in db.Baskets) { if (person.PersonId == b.PersonId) { foreach (var m in db.Meals) { if (m.MealId == b.MealId) { m.MealPlans.Add(userMealPlan); } } } } personMealPlan.BeginDate = DateTime.Now.Date; personMealPlan.EndDate = DateTime.Now.Date; personMealPlan.IsActive = true; personMealPlan.MealPlan = userMealPlan; personMealPlan.Person = person; db.PersonMealPlans.Add(personMealPlan); db.SaveChanges(); db.Baskets.RemoveRange(db.Baskets.Where(b => b.PersonId == person.PersonId)); db.SaveChanges(); } return RedirectToAction("UserProfile", "User", new { Email = email }); }
public ActionResult AddtoBasket(int mealId, string email) { Basket basket = new Basket(); Person person = new Person(); using (var db = new MealPlanContext()) { person = db.Persons.First(p => p.Email == email); basket.MealId = mealId; basket.Person = person; db.Baskets.Add(basket); db.SaveChanges(); } return Content("Ok"); }