Ejemplo n.º 1
0
        public async Task Post_Succeed_QuestionAndPoll()
        {
            List <PollsDTO> polls       = new List <PollsDTO>();
            var             NewQuestion = new QuestionDTO();

            polls.Add(new PollsDTO()
            {
                Id = 11, QuestionId = 11, Poll = "Poll 1", Votes = 11
            });
            polls.Add(new PollsDTO()
            {
                Id = 10, QuestionId = 11, Poll = "Poll 2", Votes = 12
            });
            var vm = new QuestionPollViewModel();

            NewQuestion.Title           = "title";
            NewQuestion.Description     = "description";
            NewQuestion.CommentsEnabled = true;
            NewQuestion.Image           = "image.png";
            NewQuestion.Tag             = "Relationship";
            NewQuestion.DeletionTime    = new DateTime(2020, 12, 25);
            vm.Poll       = polls;
            vm.Question   = NewQuestion;
            vm.Expiretime = 3;

            var response = await _client.PostAsync("api/Question/QuestionAndPoll", new StringContent(JsonConvert.SerializeObject(vm), Encoding.UTF8, "application/json"));

            response.EnsureSuccessStatusCode();

            response.StatusCode.Should().Be(HttpStatusCode.Created);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <QuestionDTO> > CreateQuestionAndPolls(QuestionPollViewModel totalQuestion)
        {
            // TimeSpan addedHours = new TimeSpan(0, 0, 1, 0); (TimeSpan to test with)
            TimeSpan addedHours = new TimeSpan(0, totalQuestion.Expiretime, 0, 0);
            var      expireTime = DateTime.UtcNow.Add(addedHours);

            var question = new Question
            {
                Id              = totalQuestion.Question.Id,
                Title           = totalQuestion.Question.Title,
                Description     = totalQuestion.Question.Description,
                Image           = totalQuestion.Question.Image,
                Tag             = totalQuestion.Question.Tag,
                CommentsEnabled = totalQuestion.Question.CommentsEnabled,
                DeletionTime    = expireTime
            };


            _context.Questions.Add(question);
            await _context.SaveChangesAsync();

            var          allPollsDTO = totalQuestion.Poll;
            List <Polls> allPolls    = new List <Polls>();

            for (int i = 0; i < allPollsDTO.Count(); ++i)
            {
                var poll = new Polls
                {
                    Poll       = totalQuestion.Poll[i].Poll,
                    QuestionId = question.Id
                };
                allPolls.Add(poll);
            }

            foreach (var pollitem in allPolls)
            {
                _context.Polls.Add(pollitem);
                await _context.SaveChangesAsync();
            }

            return(CreatedAtAction(
                       nameof(GetQuestion),
                       new { id = question.Id },
                       QuestionToDTO(question)));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <QuestionPollViewModel> > GetQuestionAndPolls(int id)
        {
            List <PollsDTO>       pollsDTO        = new List <PollsDTO>();
            QuestionPollViewModel questionAndPoll = new QuestionPollViewModel();

            var question = await _context.Questions.FindAsync(id);

            if (question == null)
            {
                return(NotFound());
            }
            var          questionDTO = QuestionToDTO(question);
            List <Polls> polls       = await _context.Polls.Where(s => s.QuestionId == id).ToListAsync();

            foreach (var poll in polls)
            {
                pollsDTO.Add(PollsController.PollsToDTO(poll));
            }

            questionAndPoll.Question = questionDTO;
            questionAndPoll.Poll     = pollsDTO;

            return(questionAndPoll);
        }