public void TestWorkoutRatingAddRating()
 {
     workout_rating rating = new workout_rating()
     {
         workout_id = 100,
         average_rating = 0,
         times_rated = 0
     };
     db.Setup(w => w.workout_rating.Add(rating)).Returns(rating);
     RedirectToRouteResult result = controller.AddWorkoutRating(100, 10) as RedirectToRouteResult;
     Assert.IsNotNull(result);
     Assert.AreEqual("Index", result.RouteValues["action"], "action was not Index");
     Assert.AreEqual("Home", result.RouteValues["controller"], "controller was not Home");
 }
        public ActionResult AddWorkoutRating(int? workout_id, int? rating)
        {
            if (workout_id == null)
            {
                return View("DetailedError", new HttpStatusCodeResult(HttpStatusCode.BadRequest, "No workout id was specified."));
            }
            if (rating == null)
            {
                return View("DetailedError", new HttpStatusCodeResult(HttpStatusCode.BadRequest, "No rating value was specified."));
            }

            int userID = userAccess.getUserId(User.Identity.Name);
            if (userID == -1)
            {
                return View("DetailedError", new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "No user could be associated with the rating being added"));
            }

            bool isFirstRating = false;
            workout_rating w_rating = db.workout_rating.Find(workout_id);
            //workout_rating w_rating = db.workout_rating.Where(m => m.workout_id == (int)workout_id).FirstOrDefault();
            if (w_rating != null)
            {
                w_rating.average_rating = (w_rating.average_rating * w_rating.times_rated + (int)rating) / (w_rating.times_rated + 1);
                w_rating.times_rated++;
            }
            else
            {
                w_rating = new workout_rating();
                w_rating.workout_id = (int)workout_id;
                w_rating.average_rating = (int)rating;
                w_rating.times_rated = 1;
                isFirstRating = true;
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (isFirstRating)
                    {
                        db.workout_rating.Add(w_rating);
                    }
                    else
                    {
                        db.Entry(w_rating).State = EntityState.Modified;
                    }

                    db.SaveChanges();
                    if (this.Request.UrlReferrer != null)
                    {
                        string url = this.Request.UrlReferrer.PathAndQuery;
                        return Redirect(url);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }

                }
                catch (Exception ex)
                {
                    return View("DetailedError", new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Failed to rate the requested workout."));
                }
            }
            else
            {
                return View("DetailedError", new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Failed to rate the requested workout."));
            }
        }
Ejemplo n.º 3
0
 private List<workout_rating> getSeedWorkoutRating()
 {
     var workouts = getSeedWorkouts();
     workout_rating rating1 = new workout_rating
     {
         workout_id = 1,
         workout = workouts[0],
         average_rating = 10.0M,
         times_rated = 1
     };
     workout_rating rating2 = new workout_rating
     {
         workout_id = 2,
         workout = workouts[1],
         average_rating = 9.0M,
         times_rated = 3
     };
     workout_rating rating3 = new workout_rating
     {
         workout_id = 3,
         workout = workouts[2],
         average_rating = 10.0M,
         times_rated = 2
     };
     var workoutRating = new List<workout_rating> { rating1, rating2, rating3 };
     return workoutRating;
 }