Пример #1
0
        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 });
        }
Пример #2
0
 public Review_Order_ItemController()
 {
     im = new Review_Order_ItemIM(db);
     om = new Review_Order_ItemOM(db);
 }