public void CreateContest_WithValidData_ShouldSuccessfullyAddTheContest()
        {
            var contests = new List<Contest>();
            var organizator = this.mocks
                .UserRepositoryMock
                .Object
                .All()
                .FirstOrDefault();
            if (organizator == null)
            {
                Assert.Fail("Cannot perform test - no users available");
            }

            this.mocks.ContestRepositoryMock.Setup(c => c.Add(It.IsAny<Contest>()))
                .Callback((Contest contest) =>
                {
                    contest.Owner = organizator;
                    contests.Add(contest);
                });

            var mockContext = new Mock<IPhotoContestData>();
            mockContext.Setup(c => c.Contests)
                .Returns(this.mocks.ContestRepositoryMock.Object);
            mockContext.Setup(c => c.Users)
                .Returns(this.mocks.UserRepositoryMock.Object);

            var contestController = new ContestController(mockContext.Object,null);

            string contestTitle = "Title";
            contests.AddRange(mocks.ContestRepositoryMock.Object.All().ToList());

            var newContest = new ContestInputModel
            {
                Title = contestTitle,
                Description = "Test contest",
                MaxNumberOfParticipants = 10,
                DeadlineDate = DateTime.Now.AddDays(1),
                Status = ContestStatusType.Active,
                ParticipationStrategyType = ParticipationStrategyType.Open,
                VotingStrategyType = VotingStrategyType.Open,
                WinnersCount = 1
            };

            newContest.Prizes.Add(10);

            Mapper.CreateMap<ContestInputModel, Contest>();
            ActionResult response = contestController.Create(newContest);

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

            Assert.AreEqual(1, contests.Count);
            Assert.AreEqual(newContest.Description, contestFromRepo.Description);
        }
        public ActionResult Create(ContestInputModel model)
        {
            if (ModelState.IsValid && model != null)
            {
                var contest = Mapper.Map<ContestInputModel, Contest>(model);
                contest.OwnerId = this.CurrentUser.Id;

                this.AddParticipantsToContest(model, contest);
                this.AddVotersToContest(model, contest);
                this.AddRewardsToContest(model, contest);

                this.Data.Contests.Add(contest);
                this.Data.SaveChanges();
                this.cache.RemoveContestsFromCache();

                var hubContext = GlobalHost.ConnectionManager.GetHubContext<BaseHub>();
                hubContext.Clients.All.userConnected("Created new contest with title "+contest.Title);

                return this.RedirectToAction<HomeController>(c => c.Index());
            }

            return View(model);
        }
        private void AddVotersToContest(ContestInputModel model, Contest contest)
        {
            if (model == null || contest == null)
            {
                return;
            }

            if (model.VotersIds.Count != 0)
            {
                foreach (var id in model.UserIds)
                {
                    var user = this.Data.Users.Find(id);
                    contest.Voters.Add(user);
                }
            }
        }
        private void AddRewardsToContest(ContestInputModel model, Contest contest)
        {
            if (model == null || contest == null)
            {
                return;
            }

            foreach (var prize in model.Prizes)
            {
                var reward = new Reward()
                {
                    RewardPrice = prize
                };
                contest.Rewards.Add(reward);
            }
        }
        public ActionResult Create(ContestInputModel contest)
        {
            if (ModelState.IsValid &&
                contest.Title != null &&
                contest.Description != null)
            {
                var newContest = new Contest
                {
                    Title = contest.Title,
                    Description = contest.Description,
                    RewardStrategy = contest.RewardStrategy,
                    Type = contest.Type,
                    VotingStrategy = contest.VotingStrategy,
                    DeadlineStrategy = contest.DeadlineStrategy,
                    OwnerId = this.UserProfile.Id
                };

                this.Data.Contests.Add(newContest);

                this.Data.SaveChanges();
                return this.RedirectToAction("Details", new { id = newContest.Id });
            }
            return this.View(contest);
        }
 public ActionResult Create()
 {
     var model = new ContestInputModel();
     return this.View(model);
 }