public ActionResult AdminEditContest(ContestBindingModel model)
        {
            if (model == null || !this.ModelState.IsValid)
            {
                this.AddNotification("Invalid input data", NotificationType.ERROR);
                return this.RedirectToAction("AdminEditContest");
            }

            var contest = this.Data
                              .Contests
                              .GetAll()
                              .FirstOrDefault(c => c.Id == model.Id);

            if (contest == null)
            {
                this.AddNotification("Cannot find contest", NotificationType.ERROR);
                return this.RedirectToAction("AdminEditContest");
            }

            contest.Title = model.Title;
            contest.Description = model.Description;

            if (!(this.EditAvailableVoters(model, contest) &&
                  this.EditAvailableParticipants(model, contest) &&
                  this.EditRewardStrategy(model, contest) &&
                  this.EditDeadlineStartegy(model, contest)))
            {
                this.RedirectToAction("AdminEditContest");
            }

            this.Data.SaveChanges();

            this.AddNotification("Contest edited", NotificationType.SUCCESS);
            return this.RedirectToAction("Index", "AdminOverview");
        }
        public void AddContestShouldInsertOneRecordToTheRepository()
        {
            var contests = new List<Contest>();

            var fakeUser = this.mocks.FakeUserRepository.Object.GetAll().FirstOrDefault();
            if (fakeUser == null)
            {
                Assert.Fail();
            }

            this.mocks.FakeContestRepository
                .Setup(r => r.Add(It.IsAny<Contest>()))
                .Callback(
                    (Contest contest) =>
                        {
                            contest.Organizer = fakeUser;
                            contests.Add(contest);
                        });

            var mockContext = new Mock<IPhotoBattlesData>();
            mockContext.Setup(c => c.Users)
                       .Returns(this.mocks.FakeUserRepository.Object);
            mockContext.Setup(c => c.Contests)
                       .Returns(this.mocks.FakeContestRepository.Object);

            var mockIdProvider = new Mock<IUserIdProvider>();
            mockIdProvider.Setup(ip => ip.GetUserId())
                          .Returns(fakeUser.Id);

            var contestController = new ContestsController(mockContext.Object, mockIdProvider.Object);

            var newContest = new ContestBindingModel()
            {
                Title = "Contest three",
                Description = "Contests three description",
                VotingStrategyEnum = VotingStrategyEnum.Open,
                ParticipationStrategyEnum = ParticipationStrategyEnum.Open,
                RewardStrategyEnum = RewardStrategyEnum.SingleWinner,
                DeadlineStrategyEnum = DeadlineStrategyEnum.ParticipantsLimit,
                ParticipantsLimit = 3
            };

            var response = contestController.AddContest(newContest);

            var expected = contests.FirstOrDefault(c => c.Title == newContest.Title);
            if (expected == null)
            {
                Assert.Fail();
            }

            mockContext.Verify(c => c.SaveChanges(), Times.Once);
            Assert.IsInstanceOfType(response, typeof(RedirectToRouteResult));
            Assert.AreEqual(1, contests.Count);
            Assert.AreEqual(newContest.Description, expected.Description);
        }
        public void EditContestShouldUpdateTheRecordCorrectly()
        {
            var fakeContest = this.mocks.FakeContestRepository.Object.GetAll().FirstOrDefault();
            if (fakeContest == null)
            {
                Assert.Fail();
            }

            var fakeUser = this.mocks.FakeUserRepository.Object.GetAll().FirstOrDefault();
            if (fakeUser == null)
            {
                Assert.Fail();
            }

            var mockContext = new Mock<IPhotoBattlesData>();
            mockContext.Setup(c => c.Users)
                       .Returns(this.mocks.FakeUserRepository.Object);
            mockContext.Setup(c => c.Contests)
                       .Returns(this.mocks.FakeContestRepository.Object);

            var mockIdProvider = new Mock<IUserIdProvider>();
            mockIdProvider.Setup(ip => ip.GetUserId())
                          .Returns(fakeUser.Id);

            var contestController = new AdminContestsController(mockContext.Object, mockIdProvider.Object);

            var editContest = new ContestBindingModel()
            {
                Id = 1,
                Title = "Edited contest title",
                Description = "Edited contest description",
                VotingStrategyEnum = VotingStrategyEnum.Open,
                ParticipationStrategyEnum = ParticipationStrategyEnum.Open,
                RewardStrategyEnum = RewardStrategyEnum.SingleWinner,
                DeadlineStrategyEnum = DeadlineStrategyEnum.ParticipantsLimit,
                ParticipantsLimit = 3
            };

            var response = contestController.AdminEditContest(editContest);

            var result = this.mocks.FakeContestRepository.Object.GetAll().FirstOrDefault();
            if (result == null)
            {
                Assert.Fail();
            }

            mockContext.Verify(c => c.SaveChanges(), Times.Once);
            Assert.IsInstanceOfType(response, typeof(RedirectToRouteResult));
            Assert.AreEqual(editContest.Title, result.Title);
            Assert.AreEqual(editContest.Description, result.Description);
        }
        private bool EditAvailableParticipants(ContestBindingModel model, Contest contest)
        {
            contest.RegisteredParticipants.Clear();

            if (model.ParticipationStrategyEnum == ParticipationStrategyEnum.Closed)
            {
                if (model.Participants == null || !model.Participants.Any())
                {
                    this.AddNotification("Please select participants.", NotificationType.ERROR);
                    return false;
                }

                var participants =
                    this.Data.Users.GetAll().Where(u => model.Participants.Contains(u.UserName)).ToList();

                contest.RegisteredParticipants = new List<User>(participants);
                contest.IsOpen = false;
            }
            else
            {
                contest.IsOpen = true;
            }

            contest.ParticipationStrategyEnum = model.ParticipationStrategyEnum;
            return true;
        }
        private bool EditRewardStrategy(ContestBindingModel model, Contest contest)
        {
            if (model.RewardStrategyEnum == RewardStrategyEnum.MultipleWinners)
            {
                if (model.NumberOfWinners != null)
                {
                    contest.NumberOfWinners = (int)model.NumberOfWinners;
                }
            }
            else
            {
                contest.NumberOfWinners = 1;
            }

            contest.RewardStrategyEnum = model.RewardStrategyEnum;

            return true;
        }
        private bool EditDeadlineStartegy(ContestBindingModel model, Contest contest)
        {
            if (model.DeadlineStrategyEnum == DeadlineStrategyEnum.EndDate)
            {
                contest.EndDate = model.EndDate;
                contest.ParticipantsLimit = null;
            }
            else
            {
                contest.ParticipantsLimit = model.ParticipantsLimit;
                contest.EndDate = null;
            }

            contest.DeadlineStrategyEnum = model.DeadlineStrategyEnum;

            return true;
        }
        private bool EditAvailableVoters(ContestBindingModel model, Contest contest)
        {
            contest.RegisteredVoters.Clear();

            if (model.VotingStrategyEnum == VotingStrategyEnum.Closed)
            {
                if (model.Voters == null || !model.Voters.Any())
                {
                    this.AddNotification("Please select voters.", NotificationType.ERROR);
                    return false;
                }

                var voters = this.Data.Users.GetAll().Where(u => model.Voters.Contains(u.UserName)).ToList();

                contest.RegisteredVoters = new List<User>(voters);
            }

            contest.VotingStrategyEnum = model.VotingStrategyEnum;
            return true;
        }