コード例 #1
0
ファイル: VoteController.cs プロジェクト: GeorgeMi/PollApp
        public VoteResultDTO Post([FromBody] VoteListDTO voteDTO)
        {
            string token = Request.Headers.SingleOrDefault(x => x.Key == "token").Value.First();

            VoteResultDTO result = formModel.Vote(voteDTO, token);

            return(result);
        }
コード例 #2
0
ファイル: FormModel.cs プロジェクト: GeorgeMi/PollApp
 public VoteResultDTO Vote(VoteListDTO voteDTO, string token)
 {
     try
     {
         return(bl.FormLogic.Vote(voteDTO, token));
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
コード例 #3
0
        public void Vote_Test()
        {
            List <VoteDTO> list = new List <VoteDTO>();
            VoteDTO        vote = new VoteDTO()
            {
                Answer = 1, Question = 1
            };

            list.Add(vote);

            VoteListDTO voteList = new VoteListDTO()
            {
                Answers = list, Username = "******"
            };

            Assert.AreEqual(100, _formLogic.GetAllForms("1", "Title", 0, 5, "all")[0].NrVotes);
            _formLogic.Vote(voteList, "1");
            Assert.AreEqual(101, _formLogic.GetAllForms("1", "Title", 0, 5, "all")[0].NrVotes);
        }
コード例 #4
0
        public HttpResponseMessage Post([FromBody] VoteListDTO voteDTO)
        {
            HttpResponseMessage responseMessage;
            JSendMessage        json;
            string        token  = Request.Headers.SingleOrDefault(x => x.Key == "token").Value.First();
            VoteResultDTO result = formModel.Vote(voteDTO, token);

            if (result != null)
            {
                responseMessage = Request.CreateResponse(HttpStatusCode.OK, result);
            }
            else
            {
                json            = new JSendMessage("fail", "Poll already voted");
                responseMessage = Request.CreateResponse(HttpStatusCode.BadRequest, json);
            }

            return(responseMessage);
        }
コード例 #5
0
ファイル: FormLogic.cs プロジェクト: GeorgeMi/AzurePollApp
        /// <summary>
        /// Votare sondaj
        /// </summary>
        /// <param name="voteListDTO"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public VoteResultDTO Vote(VoteListDTO voteListDTO, string token)
        {
            int userID = _dataAccess.UserRepository.FindFirstBy(user => user.Username.Equals(voteListDTO.Username)).UserID;
            int questionID, formID;

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

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

                if (
                    _dataAccess.VotedFormsRepository.FindAllBy(
                        votedForm => votedForm.FormID == formID && votedForm.UserID == userID).ToList().Count > 0)
                {
                    throw new Exception("Poll already voted");
                }

                if (_dataAccess.FormRepository.FindFirstBy((form => form.FormID == formID)).State == "closed")
                {
                    throw new Exception("Closed polls cannot be voted");
                }

                // Incrementez numarul de voturi pentru fiecare intrebare si raspuns
                foreach (VoteDTO voteDTO in voteListDTO.Answers)
                {
                    _dataAccess.AnswerRepository.AddVote(voteDTO.Answer);
                }
                _dataAccess.FormRepository.AddVote(formID);

                //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 intrebare
                    voteQuestionResult.QuestionID = voteDTO.Question;
                    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.NrVotes = Decimal.ToInt32(_dataAccess.FormRepository.FindFirstBy(form => form.FormID == formID).NrVotes);
                    voteResult.Questions.Add(voteQuestionResult);
                }

                VotedForm voted = new VotedForm();
                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("Something bad happened");
            }
        }
コード例 #6
0
ファイル: FormLogic.cs プロジェクト: GeorgeMi/PollApp
        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!");
            }
        }