示例#1
0
        public static Poll GetPollsInfo(int pollID)
        {
            using (PollSystemEntities context = new PollSystemEntities())
            {
                Poll poll = context.Polls.Include("PollAnswers").First(p => p.PollID == pollID);

                return poll;
            }
        }
示例#2
0
        public static List<Poll> GetAllPolls()
        {
            using (PollSystemEntities context = new PollSystemEntities())
            {
                List<Poll> polls = context.Polls.Include("PollAnswers").OrderBy(p => p.Question).ToList();

                return polls;
            }
        }
示例#3
0
        public static List<Poll> GetThreeRandomPolls()
        {
            using(PollSystemEntities context = new PollSystemEntities())
            {
                List<Poll> polls = context.Polls.Include("PollAnswers").ToList();

                List<Poll> pollsToReturn = new List<Poll>();

                Random rndGen = new Random();

                for (int i = 0; i < 3 && polls.Count != 0; i++)
                {
                    int index = rndGen.Next(0, polls.Count);
                    pollsToReturn.Add(polls[index]);
                    polls.RemoveAt(index);
                }

                return pollsToReturn;
            }
        }
示例#4
0
        public static void CreateNewPoll(string question, string answer1, 
			string answer2, string answer3, string answer4, string answer5, string answer6)
        {
            Poll newPoll = new Poll();
            newPoll.Question = question;
            PollSystemEntities context = new PollSystemEntities();
            context.Polls.AddObject(newPoll);
            context.SaveChanges();

            if (!string.IsNullOrWhiteSpace(answer1))
                CreateAnswer(answer1, newPoll);
            if (!string.IsNullOrWhiteSpace(answer2))
                CreateAnswer(answer2, newPoll);
            if (!string.IsNullOrWhiteSpace(answer3))
                CreateAnswer(answer3, newPoll);
            if (!string.IsNullOrWhiteSpace(answer4))
                CreateAnswer(answer4, newPoll);
            if (!string.IsNullOrWhiteSpace(answer5))
                CreateAnswer(answer5, newPoll);
            if (!string.IsNullOrWhiteSpace(answer6))
                CreateAnswer(answer6, newPoll);
        }
示例#5
0
 public static int ShowVotesCount(int pollID)
 {
     using (PollSystemEntities context = new PollSystemEntities())
     {
         Poll poll = context.Polls.Include("PollAnswers").First(p => p.PollID == pollID);
         int votesCount = 0;
         foreach (PollAnswer answer in poll.PollAnswers)
         {
             votesCount += answer.Votes;
         }
         return votesCount;
     }
 }
示例#6
0
 public static void ResetPoll(int pollID)
 {
     PollSystemEntities context = new PollSystemEntities();
     context.ExecuteStoreCommand("UPDATE PollAnswers SET Votes=0 WHERE PollID={0}", pollID);
 }
示例#7
0
 public static void DeletePoll(int pollID)
 {
     PollSystemEntities context = new PollSystemEntities();
     context.ExecuteStoreCommand("DELETE FROM PollAnswers WHERE PollID={0}", pollID);
     context.ExecuteStoreCommand("DELETE FROM Polls WHERE PollID={0}", pollID);
 }
示例#8
0
 private static void CreateAnswer(string answerText, Poll newPoll)
 {
     using (PollSystemEntities context = new PollSystemEntities())
     {
         if (answerText.Length > 255)
         {
             // Special case: long text should be inserted directly
             string cmd = String.Format("INSERT INTO PollAnswers(Answer, Votes, PollID) VALUES ('{0}', {1}, {2})", answerText, 0, newPoll.PollID);
             context.ExecuteStoreCommand(cmd);
         }
         else
         {
             PollAnswer answer = new PollAnswer();
             answer.Answer = answerText;
             answer.PollID = newPoll.PollID;
             answer.Votes = 0;
             context.PollAnswers.AddObject(answer);
         }
         context.SaveChanges();
     }
 }
示例#9
0
        public static void VoteForPoll(int answerID)
        {
            using (PollSystemEntities context = new PollSystemEntities())
            {
                PollAnswer answer = context.PollAnswers.First(a => a.AnswerID == answerID);

                int votes = answer.Votes;
                answer.Votes = votes + 1;

                context.SaveChanges();
            }
        }