示例#1
0
        public static Poll GetPoll(int pollId)
        {
            JustPollsEntities database = new JustPollsEntities();
            Poll poll = database.Polls.Include("PossibleAnswers").Where(x => x.id == pollId).FirstOrDefault();

            return(poll);
        }
示例#2
0
        public static List <PossibleAnswer> GetPossibleAnswers(int pollId)
        {
            JustPollsEntities     database = new JustPollsEntities();
            List <PossibleAnswer> polls    = (from p in database.PossibleAnswers where p.PollId == pollId orderby p.VotesCount orderby p.text select p).ToList();

            return(polls);
        }
示例#3
0
        public static List <Poll> GetPolls(int pageNumber, int pollsPerPage)
        {
            int pollsToSkip = (pageNumber - 1) * pollsPerPage;

            JustPollsEntities database = new JustPollsEntities();
            List <Poll>       polls    = database.Polls.Include("PossibleAnswers").OrderByDescending(x => x.Date).Skip(pollsToSkip).Take(pollsPerPage).ToList();

            //List<Poll> polls = (from p in database.Polls orderby p.Date descending select p).Skip(pollsToSkip).Take(pollsPerPage).ToList();

            return(polls);
        }
示例#4
0
        public static TimeSpan TimeToNextVote(string voterIp, int pollId)
        {
            JustPollsEntities database = new JustPollsEntities();

            var lastVoteByThisIp = (from p in database.Votes.Include("PossibleAnswer") where p.voterIp == voterIp && p.PossibleAnswer.PollId == pollId orderby p.Date descending select p).FirstOrDefault();

            if (lastVoteByThisIp != null)
            {
                return(lastVoteByThisIp.Date.AddDays(1) - DateTime.Now);
            }

            return(new TimeSpan(0, 0, 0));
        }
示例#5
0
        public static int GetVotesCount(int pollId)
        {
            JustPollsEntities database = new JustPollsEntities();

            var possibleAnswers = (from p in database.PossibleAnswers where p.PollId == pollId select p);
            int votesCount      = 0;

            foreach (var answer in possibleAnswers)
            {
                votesCount += answer.VotesCount;
            }

            return(votesCount);
        }
示例#6
0
        public static void AddVote(int answerId, string voterIp)
        {
            JustPollsEntities database = new JustPollsEntities();

            int oldVotesCount = (from p in database.Votes where p.answerId == answerId && p.voterIp == voterIp select p).Count();

            PossibleAnswer answer = (from p in database.PossibleAnswers where p.id == answerId select p).FirstOrDefault();

            if (oldVotesCount == 0 && answer != null)
            {
                answer.VotesCount++;

                Vote newVote = new Vote();
                newVote.answerId = answerId;
                newVote.Date     = DateTime.Now;
                newVote.voterIp  = voterIp;

                database.Votes.AddObject(newVote);
                database.SaveChanges();
            }
        }
示例#7
0
        public static int GetPollsCount()
        {
            JustPollsEntities database = new JustPollsEntities();

            return((from p in database.Polls select p).Count());
        }