示例#1
0
 public VoteResultDTO Vote(VoteListDTO voteDTO, string token)
 {
     try
     {
        return bl.FormLogic.Vote(voteDTO,token);
     }
     catch (Exception ex)
     {
         return null;
     }
 }
示例#2
0
        public VoteResultDTO Vote(VoteListDTO voteListDTO, string token)
        {
            int userID = _dataAccess.UserRepository.FindFirstBy(user => user.Username == voteListDTO.Username).UserID;
            int questionID;

            //testeaza daca tokenul si userul care a votat coincid
            if (userID == _dataAccess.TokenRepository.FindFirstBy(user => user.TokenString == token).UserID)
            {
                questionID= voteListDTO.Answers[0].Question;

                int formID = _dataAccess.QuestionRepository.FindFirstBy(question => question.QuestionID == questionID).FormID;

                //incrementez numarul de voturi pentru fiecare intrebare si raspuns

                foreach (VoteDTO voteDTO in voteListDTO.Answers)
                {
                    _dataAccess.AnswerRepository.AddVote(voteDTO.Answer);
                    _dataAccess.QuestionRepository.AddVote(voteDTO.Question);
                }

                //preiau rezultatele din baza de date pentru fiecare intrebare si raspuns
                VoteResultDTO voteResult = new VoteResultDTO();
                voteResult.Questions = new List<VoteQuestionResultDTO>();

                VoteQuestionResultDTO voteQuestionResult;
                VoteAnswerResultDTO voteAnswerResult;
                List<Answer> listAnswer;

                foreach (VoteDTO voteDTO in voteListDTO.Answers)
                {
                    listAnswer = _dataAccess.AnswerRepository.FindAllBy(answer => answer.QuestionID == voteDTO.Question).ToList();
                    voteQuestionResult = new VoteQuestionResultDTO();

                    //id-ul si nr voturi intrebare
                    voteQuestionResult.QuestionID = voteDTO.Question;
                    voteQuestionResult.QuestionNrVotes = Decimal.ToInt32(_dataAccess.QuestionRepository.FindFirstBy(question => question.QuestionID == voteDTO.Question).NrVotes);
                    voteQuestionResult.Answers = new List<VoteAnswerResultDTO>();

                    foreach (Answer a in listAnswer)
                    {
                        voteAnswerResult = new VoteAnswerResultDTO();
                        voteAnswerResult.AnswerID = a.AnswerID;
                        voteAnswerResult.AnswerNrVotes = Decimal.ToInt32(_dataAccess.AnswerRepository.FindFirstBy(answer => answer.AnswerID == a.AnswerID).NrVotes);

                        voteQuestionResult.Answers.Add(voteAnswerResult);
                    }

                    voteResult.Questions.Add(voteQuestionResult);
                }

                VotedForms voted = new VotedForms();
                voted.UserID = userID;
                voted.FormID = formID;

                //adauga sondajul in lista sondajelor votate de userul respectiv
                _dataAccess.VotedFormsRepository.Add(voted);
                return voteResult;
            }
            else throw new Exception("Poll already voted!");
        }