public ActionResult AddToOrder(OrderInfoDTO info) { if (!((bool?)this.Session["LoggedIn"] ?? false)) return RedirectToAction("Login", "Account"); if (Request.IsAjaxRequest()) { try { using (var proxy = factory.GetWorkChannel()) { List<OrderIngredientDTO> ings = proxy.QueryIngredients(new QueryRequest<IngredientsQuery> { Query = new IngredientsQuery { Login = ((UserDTO)this.Session["User"]).Email, Password = ((UserDTO)this.Session["User"]).Password, IngredientIds = info.Ingredients } }).Data; for (int i = 0; i < ings.Count; i++) { ings[i].Quantity = (info.Quantities[i] == "normal") ? ings[i].NormalWeight : ings[i].ExtraWeight; } double sizeValue = GetSize(info); SizeDTO size = new SizeDTO { SizeValue = sizeValue }; List<OrderDetailDTO> od = new List<OrderDetailDTO> { new OrderDetailDTO { Ingredients=ings, Size=size } }; OrderDTO data = new OrderDTO { OrderDetailsDTO = od }; return PartialView("_OrderList", data); } } catch (Exception e) { return HandleFaults(e); } } return View(); }
private static double GetSize(OrderInfoDTO info) { double sizeValue = SizeDTO.SMALL; switch (info.Size) { case "normal": sizeValue = SizeDTO.MEDIUM; break; case "big": sizeValue = SizeDTO.GREAT; break; default: break; } return sizeValue; }
public void HomeControllerAddToOrder() { const bool ISAJAX = true; OrderInfoDTO info = new OrderInfoDTO() { Ingredients = new int[] { 1, 2 }, Quantities = new string[] { "normal", "extra" }, Size = "big" }; HomeController ctrl = new HomeController(new WorkChannelFactoryMock()); FakeControllerSession.SetFakeControllerContext(ctrl, ISAJAX); //Redirect if not logged ctrl.Session["LoggedIn"] = false; var res1 = ctrl.AddToOrder(info) as RedirectToRouteResult; Assert.IsNotNull(res1); Assert.AreEqual(res1.RouteValues["action"], "Login"); Assert.AreEqual(res1.RouteValues["controller"], "Account"); //Pass if everything ok ctrl.Session["LoggedIn"] = true; var res2 = ctrl.AddToOrder(info) as PartialViewResult; Assert.IsNotNull(res2); var order = res2.Model as OrderDTO; Assert.IsNotNull(order); Assert.IsTrue(order.OrderDetailsDTO.Count == 1); Assert.IsTrue(order.OrderDetailsDTO[0].Ingredients.Count == 2); Assert.IsTrue(order.OrderDetailsDTO[0].Size.SizeValue == SizeDTO.GREAT); //Fault when wrong user ((UserDTO)ctrl.Session["User"]).Email = "Unknown"; var res3 = ctrl.AddToOrder(info) as ViewResult; Assert.IsNotNull(res3); Assert.AreEqual(res3.Model, "wrong user"); }