private async Task <ICollection <Answer> > GetNewAnswersAsync(AddOrUpdateQuestionWithAnswersRequest question)
        {
            var answers = new List <Answer>();

            //remove previous answers
            if (question.QuestionId > -1)
            {
                var previousAnswers = await _uow.AnswerRepository.GetAll().Where(a => a.QuestionId == question.QuestionId).ToListAsync();

                foreach (var item in previousAnswers)
                {
                    _uow.AnswerRepository.Delete(item);
                }
                await _uow.CommitAsync();
            }

            foreach (var a in question.Answers)
            {
                var ans = new Answer();
                ans.CreatedAt  = DateTime.UtcNow;
                ans.ModifiedAt = DateTime.UtcNow;
                ans.Order      = a.Order;
                ans.Title      = a.Title;
                ans.Weight     = a.Weight;
                answers.Add(ans);
            }
            return(answers);
        }
Beispiel #2
0
        public async Task <IActionResult> AddQuestionWithAnswers([FromBody] AddOrUpdateQuestionWithAnswersRequest question)
        {
            var loggedUser = User.GetUserIdFromToken();
            var result     = await _questionService.AddOrUpdateQuestionWithAnswersAsync(loggedUser, question);

            var mapped = _mapper.Map <QuestionResponse>(result);

            return(Created("", new ApiOkResponse(mapped)));
        }
        public async Task <Question> AddOrUpdateQuestionWithAnswersAsync(int loggedUser, AddOrUpdateQuestionWithAnswersRequest question)
        {
            // validate admin user
            var user = await _uow.UserRepository.FindByAsync(u => u.Id == loggedUser && u.Role == RoleEnum.ADMIN);

            if (user.Count == 0)
            {
                throw new NotAllowedException(ExceptionConstants.NOT_ALLOWED);
            }

            if (question.QuestionId > -1)
            {
                // not found question?
                var pd = await _uow.QuestionRepository.GetAsync(question.QuestionId);

                if (pd == null)
                {
                    throw new NotFoundException(ExceptionConstants.NOT_FOUND, "Question");
                }
                pd.ModifiedAt = DateTime.UtcNow;
                pd.Title      = question.QuestionName;
                pd.Order      = question.QuestionOrder;

                pd.Answers = await GetNewAnswersAsync(question);

                await _uow.QuestionRepository.UpdateAsync(pd, pd.Id);

                await _uow.CommitAsync();

                return(pd);
            }
            else
            {
                var q = new Question();
                q.ModifiedAt = DateTime.UtcNow;
                q.CreatedAt  = DateTime.UtcNow;
                q.Title      = question.QuestionName;
                q.PollId     = question.PollId;

                var orderMax = await _uow.QuestionRepository.GetAll().Where(q => q.PollId == question.PollId).OrderByDescending(p => p.Order).ToListAsync();

                q.Order = orderMax.Count > 0 ? orderMax.ElementAt(0).Order + 1 : 1;

                q.Answers = await GetNewAnswersAsync(question);

                var poll = await _uow.PollRepository.GetAsync(question.PollId);

                if (poll != null && poll.IsReadOnly == true)
                {
                    // remove tips because only read only poll have them
                    var tips = await _uow.TipRepository.GetAll().Where(t => t.PollId == poll.Id).ToListAsync();

                    foreach (var tip in tips)
                    {
                        _uow.TipRepository.Delete(tip);
                    }

                    poll.IsReadOnly  = false;
                    poll.HtmlContent = "";
                    await _uow.PollRepository.UpdateAsync(poll, poll.Id);
                }

                await _uow.QuestionRepository.AddAsync(q);

                await _uow.CommitAsync();

                return(q);
            }
        }