public void CheckPermission(IPhotoContestData data, User user, Contest contest)
 {
     if (!contest.Committee.Contains(user))
     {
         throw new BadRequestException("User is not in the voting committee.");
     }
 }
 public void CheckPermission(IPhotoContestData data, User user, Contest contest)
 {
     if (!contest.IsOpenForSubmissions)
     {
         throw new BadRequestException("The contest registration is closed.");
     }
 }
        public void ApplyReward(IPhotoContestData data, Contest contest)
        {
            var winner = contest
                        .Participants
                        .OrderByDescending(u => u.Pictures.Where(p => p.ContestId == contest.Id)
                        .Select(p => p.Votes.Count))
                        .FirstOrDefault();

            if (winner != null)
            {
                data.ContestWinners.Add(new ContestWinners()
                {
                    ContestId = contest.Id,
                    Place = 1,
                    WinnerId = winner.Id
                });

                foreach (var reward in contest.Rewards)
                {
                    reward.UserId = winner.Id;
                }

                data.SaveChanges();
            }
        }
Пример #4
0
        private static bool PictureCanBeVotedUnvoted(User user, Picture dbPicture, Contest dbContest)
        {
            // 1. User is author
            if (dbPicture.AuthorId == user.Id)
            {
                return false;
            }

            // 2. User is contest owner
            if (dbContest.OwnerId == user.Id)
            {
                return false;
            }

            //3. Contest is not Active
            if (dbContest.Status != PhotoContest.Models.Enumerations.ContestStatus.Active)
            {
                return false;
            }

            // 4. Contest is voted by Jury and user is not member of the Jury
            if (dbContest.VotingType == PhotoContest.Models.Enumerations.VotingType.Closed &&
                !dbContest.Jury.Members.Any(m => m.Id == user.Id))
            {
                return false;
            }

            return true;
        }
        public void ApplyReward(IPhotoContestData data, Contest contest)
        {
            var winners = contest
                        .Participants
                        .OrderByDescending(u => u.Pictures.Where(p => p.ContestId == contest.Id).Sum(p => p.Votes.Count))
                        .ThenBy(u => u.Pictures.Count)
                        .Take(this._topNPrizes)
                        .ToList();

            if (winners.Any())
            {
                for (int i = 0; i < winners.Count; i++)
                {
                    data.ContestWinners.Add(new ContestWinners()
                    {
                        ContestId = contest.Id,
                        Place = i + 1,
                        WinnerId = winners.ElementAt(i).Id
                    });

                    foreach (var reward in contest.Rewards.Where(r => r.Place == i + 1))
                    {
                        reward.UserId = winners[i].Id;
                    }
                }

                data.SaveChanges();
            }
        }
 public void CheckPermission(IPhotoContestData data, User user, Contest contest)
 {
     if (contest.Participants.Contains(user))
     {
         throw new BadRequestException("You cannot vote for contest that you currently participate in.");
     }
 }
Пример #7
0
        public static bool HasVotedForPicture(User user, Picture dbPicture, Contest dbContest)
        {
            if (dbPicture.Votes.Any(v => v.VoterId == user.Id && v.ContestId == dbContest.Id))
            {
                return true;
            }

            return false;
        }
        public void CheckDeadline(IPhotoContestData data, Contest contest, User user)
        {
            if (contest.SubmissionDeadline < DateTime.Now)
            {
                if (contest.IsOpenForSubmissions)
                {
                    contest.IsOpenForSubmissions = false;
                    data.Contests.Update(contest);
                    data.SaveChanges();
                }

                throw new BadRequestException("The contest is closed for submissions/registrations");
            }
        }
        public void CheckPermission(IPhotoContestData data, User user, Contest contest)
        {
            if (!contest.IsOpenForSubmissions)
            {
                throw new BadRequestException("The registration for this contest is closed.");
            }

            if (!contest.InvitedUsers.Contains(user))
            {
                throw new BadRequestException("The user is not selected to participate.");
            }

            var invitation = user.PendingInvitations.FirstOrDefault(i => i.ContestId == contest.Id);

            invitation.Status = InvitationStatus.Accepted;

            data.SaveChanges();
        }
        public ActionResult Post(ContestModel contest)
        {
            if (!ModelState.IsValid)
            {
                return View("add", contest);
            }

            if (contest.ParticipationEndTime == null && contest.MaxParticipationsCount == null)
            {
                ModelState.AddModelError("", "You have to choose end date or max pariticpations count");
                return View("add", contest);
            }

            var userId = User.Identity.GetUserId();
            var newContest = new Contest()
            {
                Title = contest.Title,
                Description = contest.Description,
                CreatedOn = DateTime.Now,
                DeadlineStrategy = contest.DeadlineStrategy,
                MaxParticipationsCount = contest.MaxParticipationsCount,
                OwnerId = userId,
                ParticipationEndTime = contest.ParticipationEndTime,
                ParticipationStrategy = contest.ParticipationStrategy,
                PossibleWinnersCount = contest.PossibleWinnersCount,
                State = TypeOfEnding.Ongoing,
                VotingStrategy = contest.VotingStrategy,
                LastUpdated = DateTime.Now
            };

            this.Data.Contests.Add(newContest);
            this.Data.SaveChanges();

            return RedirectToAction("Index", "Prize", new { ContestId = newContest.Id, countOfPrizes = newContest.PossibleWinnersCount });
        }
Пример #11
0
 public static bool CanVoteForPicture(User user, Picture dbPicture, Contest dbContest)
 {
     return PictureCanBeVotedUnvoted(user, dbPicture, dbContest) && !HasVotedForPicture(user, dbPicture, dbContest);
 }
Пример #12
0
 private static void CreateContest()
 {
     var owner = dbContext.Users.First();
     var contest = new Contest()
     {
         Title = "Some contest",
         Description = "Aide na konkursite!",
         Status = ContestStatus.Active,
         Owner = owner,
         StartDate = DateTime.Now,
         EndDate = DateTime.Now,
         DeadlineType = DeadlineType.ParticipationLimit,
         VotingType = VotingType.Open,
         ParticipationType = ParticipationType.Open
     };
     dbContext.Contests.Add(contest);
     dbContext.SaveChanges();
 }
        private ContestStatus UpdateContestStatus(Contest contest)
        {
            if (contest.Status != ContestStatus.Dismissed && contest.Status != ContestStatus.Finished)
            {
                if (contest.Status != ContestStatus.Dismissed && contest.Status != ContestStatus.Finished)
                {
                    if (contest.DeadLineStrategy == DeadLineStrategy.ByTime && contest.DateEnd <= DateTime.Now)
                    {
                        return ContestStatus.UploadClosed;
                    }
                    if (contest.DeadLineStrategy == DeadLineStrategy.ByNumberOfParticipants && contest.Participants.Count >= contest.MaximumParticipants)
                    {
                        return ContestStatus.ParticipationClosed;
                    }
                }
            }

            return contest.Status;
        }
        public int CreateContest(CreateContestBindingModel model, string userId)
        {
            if (this.Data.RewardStrategies.Find(model.RewardStrategyId) == null)
            {
                throw new NotFoundException("Not existing reward strategy");
            }

            if (this.Data.ParticipationStrategies.Find(model.ParticipationStrategyId) == null)
            {
                throw new NotFoundException("Not existing participation strategy");
            }

            if (this.Data.VotingStrategies.Find(model.VotingStrategyId) == null)
            {
                throw new NotFoundException("Not existing voting strategy");
            }

            if (this.Data.DeadlineStrategies.Find(model.DeadlineStrategyId) == null)
            {
                throw new NotFoundException("Not existing deadline strategy");
            }

            var loggedUserId = userId;

            var contest = new Contest
            {
                Title = model.Title,
                Description = model.Description,
                Status = ContestStatus.Active,
                RewardStrategyId = model.RewardStrategyId,
                VotingStrategyId = model.VotingStrategyId,
                ParticipationStrategyId = model.ParticipationStrategyId,
                DeadlineStrategyId = model.DeadlineStrategyId,
                ParticipantsLimit = model.ParticipantsLimit,
                TopNPlaces = model.TopNPlaces,
                SubmissionDeadline = model.SubmissionDeadline,
                IsOpenForSubmissions = true,
                StartDate = DateTime.Now,
                OrganizatorId = loggedUserId,
            };

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

            return contest.Id;
        }
Пример #15
0
        protected void DeleteContestData(Contest contest)
        {
            var pictures = contest.Pictures.ToList();

            for (int i = 0; i < pictures.Count; i++)
            {
                var picture = pictures[i];
                var votes = picture.Votes.ToList();
                for (int j = 0; j < votes.Count; j++)
                {
                    var vote = votes[j];
                    this.Data.Votes.Delete(vote);
                }

                this.Data.Pictures.Delete(picture);
            }

            this.Data.Contests.Delete(contest);
        }
Пример #16
0
        private bool RightToParticipate(Contest contest, string userId)
        {
            bool validContest = contest != null;

            bool isOpen = contest.State.Equals(TypeOfEnding.Ongoing);

            bool openAccTime = contest.ParticipationEndTime == null ? true : contest.ParticipationEndTime.Value > DateTime.Now;
            bool deadlineByTime = contest.DeadlineStrategy.Equals(DeadlineStrategy.ByTime) && openAccTime;
            bool deadlineByNumParticipants = contest.DeadlineStrategy.Equals(DeadlineStrategy.ByNumberOfParticipants) &&
                contest.Participants.Count() < contest.MaxParticipationsCount.Value;

            bool rightToParticipateOpen = contest.VotingStrategy.Equals(Strategy.Open);
            bool rightToParticipateClose = contest.VotingStrategy.Equals(Strategy.Closed) && contest.Participants.Any(p => p.Id == userId);

            return validContest && isOpen && (rightToParticipateClose || rightToParticipateOpen) && (deadlineByTime || deadlineByNumParticipants);
        }
        private bool IsContestFinished(Contest contest)
        {
            if (contest.DeadlineStrategy == DeadlineStrategy.ByTime)
            {
                var isOver = DateTime.Now.Date >= contest.EndDate;
                if (isOver)
                {
                    return true;
                }
            }
            else
            {
                var isFull = contest.ParticipantsLimit == contest.Participants.Count;
                if (isFull)
                {
                    return true;
                }
            }

            return false;
        }
Пример #18
0
        public ActionResult CreateContest(CreateContestBindingModel newContest)
        {
            if (newContest.DeadlineStrategy == DeadlineStrategy.ByTime)
            {
                if (newContest.Deadline > DateTime.Now)
                {
                    if (ModelState.IsValid)
                    {

                        var currentUserId = User.Identity.GetUserId();
                        var currentUser = this.Data.Users.Find(currentUserId);
                        var contest = new Contest()
                        {
                            Title = newContest.Title,
                            Description = newContest.Description,
                            CreatedOn = newContest.CreatedOn,
                            OwnerId = currentUserId,
                            ParticipationStrategy = newContest.ParticipationStrategy,
                            NumberOfParticipants = newContest.NumberOfParticipants,
                            DeadlineStrategy = newContest.DeadlineStrategy,
                            Deadline = newContest.Deadline,
                            PrizeCount = newContest.PrizeCount,
                            PrizeValues = newContest.PrizeValues*2,
                            RewardStrategy = newContest.RewardStrategy,
                            VotingStrategy = newContest.VotingStrategy
                        };
                        currentUser.Coints = currentUser.Coints - contest.PrizeValues/2;
                        if (currentUser.Coints < 0)
                        {
                            this.TempData["message-create-contest-coints"] = "You do NOT have enough Coints!";
                        }
                        else
                        {
                            this.Data.Contests.Add(contest);
                            //currentUser.Contests.Add(contest);
                            this.Data.Contests.SaveChanges();
                            this.TempData["message-create-contest-success"] = "You successfully created new contest!";
                            return RedirectToAction("ContestDetails", "Contests", new {id = contest.Id});
                        }
                    }
                }
            }
            else if(newContest.NumberOfParticipants>0)
            {
                 var currentUserId = User.Identity.GetUserId();
                        var currentUser = this.Data.Users.Find(currentUserId);
                        var contest = new Contest()
                        {
                            Title = newContest.Title,
                            Description = newContest.Description,
                            CreatedOn = newContest.CreatedOn,
                            OwnerId = currentUserId,
                            ParticipationStrategy = newContest.ParticipationStrategy,
                            NumberOfParticipants = newContest.NumberOfParticipants,
                            DeadlineStrategy = newContest.DeadlineStrategy,
                            Deadline = newContest.Deadline,
                            PrizeCount = newContest.PrizeCount,
                            PrizeValues = newContest.PrizeValues*2,
                            RewardStrategy = newContest.RewardStrategy,
                            VotingStrategy = newContest.VotingStrategy
                        };
                        currentUser.Coints = currentUser.Coints - contest.PrizeValues/2;
                if (currentUser.Coints < 0)
                {
                    this.TempData["message-create-contest-coints"] = "You do NOT have enough Coints!";
                }
                else
                {
                    this.Data.Contests.Add(contest);
                    currentUser.Contests.Add(contest);
                    this.Data.Contests.SaveChanges();
                    this.TempData["message-create-contest-success"] = "You successfully created new contest!";
                    return RedirectToAction("ContestDetails", "Contests", new {id = contest.Id});
                }
            }

            return this.View(newContest);
        }