Ejemplo n.º 1
0
        public ActionResult MyListChangeRating(int?id, int?selectedRating)
        {
            string userId = User.Identity.GetUserId();

            if (userId == null)
            {
                return(View("Error"));
            }

            if (selectedRating == null)
            {
                using (var userGameRatingService = new UserGameRatingService())
                {
                    var existingUserGameRating = userGameRatingService.GetExistingUserGameRating(userId, (int)id);
                    if (existingUserGameRating != null)
                    {
                        var dbUserGameRating = userGameRatingService.GetByID(existingUserGameRating.Id);
                        userGameRatingService.Delete(dbUserGameRating);
                        userGameRatingService.Save();
                    }
                }
            }

            if (selectedRating != null)
            {
                int gameRatingIdToStore;
                using (var gameRatingService = new GameRatingService())
                {
                    gameRatingIdToStore = gameRatingService.GetGameRatingByRating((int)selectedRating);
                }
                using (var userGameRatingService = new UserGameRatingService())
                {
                    var dbUserGameRating = userGameRatingService.GetExistingUserGameRating(userId, (int)id);
                    if (dbUserGameRating == null)
                    {
                        var userGameRating = new UserGameRating
                        {
                            UserId       = userId,
                            GameId       = (int)id,
                            GameRatingId = gameRatingIdToStore
                        };
                        userGameRatingService.Add(userGameRating);
                        userGameRatingService.Save();
                    }
                    else
                    {
                        var dbUGRToStore = userGameRatingService.GetByID(dbUserGameRating.Id);
                        dbUGRToStore.GameRatingId = gameRatingIdToStore;
                        userGameRatingService.Save();
                    }
                }
            }
            return(RedirectToAction("MyList", "Account"));
        }
Ejemplo n.º 2
0
        public ActionResult MyList()
        {
            List <GameViewModel> myGames;

            using (var userStatusService = new UserGameStatusService())
            {
                myGames = userStatusService.GetAllGamesForUserById(User.Identity.GetUserId()).Select(g => new GameViewModel(g)).OrderBy(g => g.Title).ToList();
            }

            using (var ratingService = new GameRatingService())
            {
                UserGameRating userGameRating;
                using (var userRatingService = new UserGameRatingService())
                {
                    foreach (var game in myGames)
                    {
                        userGameRating = userRatingService.GetExistingUserGameRating(User.Identity.GetUserId(), game.Id);

                        var ratings = ratingService.GetAll().Select(x => new SelectListItem {
                            Text = x.Rating.ToString(), Value = x.Id.ToString(), Selected = userGameRating == null ? false : userGameRating.GameRatingId == x.Id
                        }).ToList();
                        game.Ratings        = ratings;
                        game.SelectedRating = ratings.Where(r => r.Selected != false).Select(r => Convert.ToInt32(r.Value)).FirstOrDefault();
                    }
                }
            }

            using (var statusService = new GameStatusService())
            {
                UserGameStatus userGameStatus;
                using (var userStatusService = new UserGameStatusService())
                {
                    foreach (var game in myGames)
                    {
                        userGameStatus = userStatusService.GetExistingUserGameStatus(User.Identity.GetUserId(), game.Id);

                        var statuses = statusService.GetAll().Select(s => new SelectListItem {
                            Text = s.Status, Value = s.Id.ToString(), Selected = userGameStatus.GameStatusId == s.Id
                        }).ToList();
                        game.Statuses       = statuses;
                        game.SelectedStatus = statuses.Where(s => s.Selected != false).Select(s => s.Value.ToString()).FirstOrDefault();
                    }
                }
            }
            return(View(myGames));
        }
        public ActionResult Details(int?id)
        {
            if (id == null || id <= 0)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var game = gameService.GetByID((int)id);

            if (game == null)
            {
                return(HttpNotFound());
            }

            var model = new GameViewModel(game);

            using (var ratingService = new GameRatingService())
            {
                UserGameRating userGameRating;
                using (var userRatingService = new UserGameRatingService())
                {
                    userGameRating = userRatingService.GetExistingUserGameRating(User.Identity.GetUserId(), (int)id);
                    var allUserGameRatings = userRatingService.GetAllUserGameRatingsByGameId((int)id); //Get all user game ratings for a single game
                    if (allUserGameRatings.Count > 0)                                                  //calculate game rating
                    {
                        var rating = 0;
                        foreach (var el in allUserGameRatings)
                        {
                            rating += el.GameRating.Rating;
                        }
                        double result = rating / (double)allUserGameRatings.Count;
                        ViewBag.Score = result.ToString("n2");
                    }
                    else
                    {
                        ViewBag.Score = "N/A";
                    }
                }

                var ratings = ratingService.GetAll().Select(x => new SelectListItem {
                    Text = x.Rating.ToString(), Value = x.Id.ToString(), Selected = userGameRating == null ? false : userGameRating.GameRatingId == x.Id
                }).ToList();
                model.Ratings        = ratings;
                model.SelectedRating = ratings.Where(r => r.Selected != false).Select(r => Convert.ToInt32(r.Value)).FirstOrDefault();
                //model.SelectedRating = ratings.Where(r => r.Selected != false).Select(r => r.Value = "Rate game").FirstOrDefault();
            }
            using (var statusService = new GameStatusService())
            {
                UserGameStatus userGameStatus;
                using (var userStatusService = new UserGameStatusService())
                {//load existing user game status for a game for it to be selected
                    userGameStatus = userStatusService.GetExistingUserGameStatus(User.Identity.GetUserId(), (int)id);
                }
                var statuses = statusService.GetAll().Select(s => new SelectListItem {
                    Text = s.Status, Value = s.Id.ToString(), Selected = userGameStatus == null ? false : userGameStatus.GameStatusId == s.Id
                }).ToList();
                model.Statuses       = statuses;
                model.SelectedStatus = statuses.Where(s => s.Selected != false).Select(s => s.Value.ToString()).FirstOrDefault();
            }
            return(View(model));
        }
        public ActionResult Details(int?id, GameViewModel gameViewModel)
        {
            string userId = User.Identity.GetUserId();

            if (userId == null)
            {
                return(View("Error"));
            }
            if (id == null || id <= 0)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (gameViewModel.SelectedStatus != null)
            {
                int gameStatusIdToStore = int.Parse(gameViewModel.SelectedStatus);
                //using (var gameStatusService = new GameStatusService())
                //{
                //    gameStatusIdToStore = gameStatusService.GetStatusIdBySelectedStatus(gameViewModel.SelectedStatus);
                //}
                using (var userGameStatusService = new UserGameStatusService())
                {
                    var dbUserGameStatus = userGameStatusService.GetExistingUserGameStatus(userId, (int)id);
                    if (dbUserGameStatus == null)
                    {
                        var userGameStatus = new UserGameStatus
                        {
                            UserId       = userId,
                            GameId       = (int)id,
                            GameStatusId = gameStatusIdToStore
                        };
                        userGameStatusService.Add(userGameStatus);
                        userGameStatusService.Save();
                    }
                    else
                    {
                        var dbUGSToStore = userGameStatusService.GetByID(dbUserGameStatus.Id);
                        dbUGSToStore.GameStatusId = gameStatusIdToStore;
                        userGameStatusService.Save();
                    }
                }
            }

            if (gameViewModel.SelectedRating == 0)
            {
                using (var userGameRatingService = new UserGameRatingService())
                {
                    var existingUserGameRating = userGameRatingService.GetExistingUserGameRating(userId, (int)id);
                    if (existingUserGameRating != null)
                    {
                        var dbUserGameRating = userGameRatingService.GetByID(existingUserGameRating.Id);
                        userGameRatingService.Delete(dbUserGameRating);
                        userGameRatingService.Save();
                    }
                }
            }

            if (gameViewModel.SelectedRating != 0)
            {
                int gameRatingIdToStore;
                using (var gameRatingService = new GameRatingService())
                {
                    gameRatingIdToStore = gameRatingService.GetGameRatingByRating((int)gameViewModel.SelectedRating);
                }
                using (var userGameRatingService = new UserGameRatingService())
                {
                    var dbUserGameRating = userGameRatingService.GetExistingUserGameRating(userId, (int)id);
                    if (dbUserGameRating == null)
                    {
                        var userGameRating = new UserGameRating
                        {
                            UserId       = userId,
                            GameId       = (int)id,
                            GameRatingId = gameRatingIdToStore
                        };
                        userGameRatingService.Add(userGameRating);
                        userGameRatingService.Save();
                    }
                    else
                    {
                        var dbUGRToStore = userGameRatingService.GetByID(dbUserGameRating.Id);
                        dbUGRToStore.GameRatingId = gameRatingIdToStore;
                        userGameRatingService.Save();
                    }
                }
            }
            return(RedirectToAction("Details", "Games"));
        }