public ActionResult AddToCart(FormCollection values) { int customBentoBoxId = Convert.ToInt32( values["customBentoBoxId"] ); string encodedRecipe = values["recipe"]; string customBentoBoxName = values["customBentoBoxName"]; CustomBentoBox customBentoBox; if (customBentoBoxId == 0) { customBentoBox = new CustomBentoBox(); var recipe = CustomBentoBox.DecodeRecipe(encodedRecipe); customBentoBox.BentoBoxId = recipe.BentoBoxId; customBentoBox.CustomBentoBoxName = customBentoBoxName; customBentoBox.AddToCustomBentoBox(recipe.DishIds); db.CustomBentoBoxes.AddObject(customBentoBox); db.SaveChanges(); } else { customBentoBox = db.CustomBentoBoxes.Single(i => i.CustomBentoBoxId == customBentoBoxId); if (customBentoBox != null) { var recipe = CustomBentoBox.DecodeRecipe(encodedRecipe); customBentoBox.ReplaceRecipe(recipe.DishIds); } else { return HttpNotFound(); } } if (ModelState.IsValid) { var shoppingCart = ShoppingCartHelper.GetCart(HttpContext); shoppingCart.AddToCart( customBentoBox); return RedirectToAction("Edit", "ShoppingCart"); } return RedirectToAction("Create"); }
public ActionResult AddToCurrentBox(string encodedRecipe, int dishId) { // Retrieve the album from the database var addedDish = db.Dishes.Single(i => i.DishId == dishId); // Add it to the shopping cart var customBentoBox = new CustomBentoBox(encodedRecipe); var isAdded = customBentoBox.AddToCustomBentoBox(addedDish); var processor = new Processor(); processor.ProcessRule(customBentoBox); if (!processor.CanAddDish) { // need to remove the current dish! customBentoBox.RemoveFromCustomBentoBox(addedDish); } if (isAdded == false) { processor.AddDuplicationMessage(); } var mainCourses = from item in customBentoBox.CustomBentoBoxItems where item.Dish.DishTypeId == (int)DishType.MainCourse select new { dishId = item.DishId, dishImageUrl = item.Dish.DishImageUrl, dishName = item.Dish.DishName, customBentoBoxId = customBentoBox.CustomBentoBoxId }; var sideDishes = from item in customBentoBox.CustomBentoBoxItems where item.Dish.DishTypeId == (int)DishType.SideDish select new { dishId = item.DishId, dishImageUrl = item.Dish.DishImageUrl, dishName = item.Dish.DishName, customBentoBoxId = customBentoBox.CustomBentoBoxId }; var others = from item in customBentoBox.CustomBentoBoxItems where item.Dish.DishTypeId == (int)DishType.Drink select new { dishId = item.DishId, dishImageUrl = item.Dish.DishImageUrl, dishName = item.Dish.DishName, customBentoBoxId = customBentoBox.CustomBentoBoxId }; var warningMsg = processor.Errors; encodedRecipe = customBentoBox.EncodedRecipe; var unitPrice = customBentoBox.BentoBox.UnitPrice +customBentoBox.CustomBentoBoxItems.Sum(i => i.Dish.DishIncrementalPrice * i.Quantity); return Json(new { encodedRecipe = encodedRecipe, currentItems = new { mainCourses = new { dishes = mainCourses, count = customBentoBox.BentoBox.NumOfEntree } , sideDishes = new { dishes = sideDishes, count = 1 } , others = new { dishes = others, count = 1 } }, warningMsg = warningMsg , isValid = isAdded && processor.CanAddDish, unitPrice = unitPrice }); }