public ActionResult Create(ReviewVM review) { //add the new value to the view VM ReviewVM reviewVM = (ReviewVM)Session[Global.ReviewVMSession]; Review_Order_ItemOM rom = new Review_Order_ItemOM(db); if (reviewVM == null) { ViewBag.error(Global.Review_Create_Error); return RedirectToAction("Index", "User"); } //what we have from the user submitted information is the anonymous flag, a rating for each item as well as the review text Rating.GetRatingReviewOrderItems(reviewVM.review_order_items, review.review_order_items); TextParser.ParseReviewText(reviewVM.review_order_items, review.reviewText); TextParser.SetDate(reviewVM.review_order_items); reviewVM.review.is_anonymous = review.is_anonymous; reviewVM.review.rating = Rating.CalculateAverageRating(reviewVM.review_order_items); //have to set this to null and only associate the ID. This is a weird behaviour. //If I don't it creates a new order and new order items. This is done in the foreach below //No clue why this is happening? reviewVM.review.order = null; bool result = false; bool exceptionThrown = false; if (ModelState.IsValid) { try { result = om.Create(reviewVM.review); List<review_order_item> roiList = reviewVM.review_order_items; foreach (review_order_item roi in roiList) { if (!string.IsNullOrEmpty(roi.review_text)) { roi.review_id = reviewVM.review.id; roi.order_item = null; rom.Create(roi); } } } catch (Exception) { exceptionThrown = true; } } if (result) { ViewBag.order_id = new SelectList(db.orders, "id", "id", reviewVM.review.order_id); ViewBag.restaurant_id = new SelectList(db.restaurants, "id", "name", reviewVM.review.restaurant_id); ViewBag.user_id = new SelectList(db.users, "id", "username", reviewVM.review.user_id); ViewBag.ratings = new SelectList(Rating.GetRatings(), "Value", "Text"); //succesfully created a review, can remove the session one Session.Remove(Global.ReviewVMSession); } else { ViewBag.error(exceptionThrown ? Global.ServerError : Global.Review_Create_Error); } return RedirectToAction("Details", "User", new { id = reviewVM.review.user_id }); }
// // GET: /Review/Create public ActionResult Create(int orderID) { ReviewVM reviewVM; review review = new review(); OrderIM oim = new OrderIM(db); user authUser = Util.User.UserUtil.getAuthenticatedUser(Request); if (authUser == null) { return RedirectToAction("Index", "User"); } review.user_id = authUser.id; // Look for a valid order before we continue order order = oim.find(orderID); if (order == null) { return RedirectToAction("Index", "User"); } review.order_id = order.id; if (order.table != null) { review.restaurant_id = order.table.restaurant_id.Value; } else { //TODO: this must be fixed after CAP-480 review.restaurant_id = db.restaurants.First().id; } reviewVM = new ReviewVM(review, order); Session.Add(Global.ReviewVMSession, reviewVM); ViewBag.ratings = new SelectList(Rating.GetRatings(), "Value", "Text"); return View(reviewVM); }
public void CreateTest() { ReviewController target = new ReviewController(); // TODO: Initialize to an appropriate value ReviewVM review = new ReviewVM(review1,order1); // TODO: Initialize to an appropriate value ActionResult expected = null; // TODO: Initialize to an appropriate value ActionResult actual; actual = target.Create(review); // Assert.AreEqual(expected, actual); Assert.IsNotNull(actual); }