Пример #1
0
        //Submit a user's attempt to RECIPEDB
        public ActionResult SubmitRecipeAttempt(RecipeAttempt attempt)
        {
            RecipeDBEntities ORM = new RecipeDBEntities();

            try
            {
                if (ORM.Recipes.Find(attempt.RecipeID) == null)
                {
                    JObject JsonData = (JObject)Session["Recipe"];

                    Recipe RecipeForDB = new Recipe();

                    RecipeForDB.RecipeID = JsonData["RecipeID"].ToString();
                    // RecipeForDB.Description = JsonData["Description"].ToString();
                    RecipeForDB.Ingredients_Num = JsonData["Ingredients"].ToList().Count;
                    RecipeForDB.Category        = JsonData["Cuisine"].ToString();
                    RecipeForDB.Title           = JsonData["Title"].ToString();

                    ORM.Recipes.Add(RecipeForDB);
                }

                ORM.RecipeAttempts.Add(attempt);
                ORM.SaveChanges();

                Session["Recipe"] = null;

                return(RedirectToAction("DisplayUserAttempts", new { User_ID = attempt.User_ID }));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(View("../Home/ErrorPage"));
            }
        }
        //Generate a Leaderboard View by grabbing an ApplicationUser from Identity
        //and using that data we popuplate a LeaderboardUser
        public ActionResult DisplayLeaderboard()
        {
            RecipeDBEntities     ORM     = new RecipeDBEntities();
            ApplicationDbContext UserORM = new ApplicationDbContext();

            List <LeaderboardUser> LBlist = new List <LeaderboardUser>();

            List <string> tempList = ORM.RecipeAttempts.Select(x => x.User_ID).Distinct().ToList();

            foreach (string UserID in tempList)
            {
                LeaderboardUser temp     = new LeaderboardUser();
                ApplicationUser tempUser = UserORM.Users.Find(UserID);
                temp.UserName       = tempUser.Email;
                temp.TotalAttempts  = ORM.RecipeAttempts.Where(x => x.User_ID == UserID).Count();
                temp.AveDifficulty  = Math.Round(ORM.RecipeAttempts.Where(x => x.User_ID == UserID).Select(x => x.Difficulty).Select(int.Parse).Average(), 3);
                temp.AveIngredients = Math.Round((double)ORM.RecipeAttempts.Where(x => x.User_ID == UserID).Select(x => x.Recipe.Ingredients_Num).Average(), 3);

                LBlist.Add(temp);
            }
            List <int?> RatingList = ORM.Ratings.Select(x => x.AttemptID).Distinct().ToList();

            List <LeaderboardRating> LBRatingList = new List <LeaderboardRating>();


            foreach (var Attempt in RatingList)
            {
                RecipeAttempt   AttemptForLB = ORM.RecipeAttempts.Find(Attempt);
                ApplicationUser tempUser     = UserORM.Users.Find(AttemptForLB.User_ID);

                LeaderboardRating temp = new LeaderboardRating();
                temp.UserName = tempUser.Email;
                temp.Image    = AttemptForLB.image;
                temp.Title    = AttemptForLB.Recipe.Title;
                double AveRating = (double)ORM.Ratings.Where(x => x.RecipeAttempt.AttemptID == AttemptForLB.AttemptID).Select(x => x.Rating1).Average();
                temp.AveRating       = String.Format("{0:P2}.", AveRating);
                temp.NumberOfRatings = ORM.Ratings.Where(x => x.RecipeAttempt.AttemptID == AttemptForLB.AttemptID).Count();
                LBRatingList.Add(temp);
            }
            ViewBag.Ratings = LBRatingList;
            ViewBag.Users   = LBlist;
            return(View("../Home/Leaderboard"));
        }