Esempio n. 1
0
        public async Task <ActionResult <QuestionResource> > CreateQuestion([FromBody] CreateQuestionResource model)
        {
            var token     = Request.Headers["Authorization"].ToString();
            var userToken = new JwtSecurityTokenHandler().ReadJwtToken(token);
            var userId    = userToken.Claims.ToArray()[0].Value.ToString();
            var userGuid  = new Guid(userId);

            foreach (var quest in model.Questions)
            {
                var question     = _mapper.Map <QuestionResource, QuizQuestion>(quest);
                var oldQuestions = await _quizQuestionService.GetAllQuestionsByEvaluationId(quest.EvaluationId);

                await DeleteOldQuestions(oldQuestions, model);

                if (quest.Id > 0)
                {
                    var questionToUpdate = await _quizQuestionService.GetQuestionById(quest.Id);

                    await _quizQuestionService.UpdateQuestion(questionToUpdate, question);

                    var oldAnswers = await _answerService.GetAllAnswersByQuestionId(quest.Id);
                    await DeleteOldAnswers(oldAnswers, question);

                    foreach (var newAns in question.Answers)
                    {
                        if (newAns.Id > 0)
                        {
                            var answerToUpdate = await _answerService.GetAnswerById(newAns.Id);

                            await _answerService.UpdateAnswer(answerToUpdate, newAns);
                        }
                        else
                        {
                            newAns.QuizQuestionId = question.Id;
                            newAns.Status         = 1;
                            await _answerService.CreateAnswer(newAns);
                        }
                    }
                }
                else
                {
                    var questionToAdd = _mapper.Map <QuestionResource, QuizQuestion>(quest);
                    questionToAdd.CreatedById  = userGuid;
                    questionToAdd.EvaluationId = quest.EvaluationId;
                    questionToAdd.Status       = 1;
                    foreach (var ans in questionToAdd.Answers)
                    {
                        ans.Status = 1;
                    }
                    var questionResult = await _quizQuestionService.CreateQuestion(questionToAdd);

                    if (questionResult == null)
                    {
                        return(BadRequest());
                    }
                }
            }
            return(Ok());
        }
Esempio n. 2
0
        //Save the answer entered for a specfic question
        public ActionResult CreateAnswer(string TxtAra)
        {
            int        quNo   = (int)TempData["Category"];
            AnswerView AnsObj = new AnswerView();

            AnsObj.Answerlist = TxtAra;
            AnsObj.QuestionId = quNo;
            IAnswer.CreateAnswer(AMapToDto(AnsObj));
            return(RedirectToAction("AllQuestions", "Question"));
        }
Esempio n. 3
0
        public IActionResult Create(Guid?id, [Bind("UserId,QuestionId,Text")] AnswerCreateModel answerCreateModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(answerCreateModel));
            }

            _service.CreateAnswer(id, answerCreateModel);

            return(RedirectToAction("Index", "Questions"));
        }
Esempio n. 4
0
        public ActionResult Create([Bind(Include = "Answer_ID,content,ISCorrect,Question_ID")] AnswerViewModel answer)
        {
            if (ModelState.IsValid)
            {
                var       mapper    = new MapperConfiguration(cfg => cfg.CreateMap <AnswerViewModel, AnswerDTO>()).CreateMapper();
                AnswerDTO answerDTO = mapper.Map <AnswerViewModel, AnswerDTO>(answer);
                // answerDTO.Question_ID = id.Value;
                int Answer_ID = AnswerService.CreateAnswer(answerDTO);
                return(RedirectToAction("Details", "Questions", new { id = answer.Question_ID }));
            }

            // ViewBag.Question_ID = new SelectList(QuestionService.GetQuestions(), "Question_ID", "content");
            return(View(answer));
        }
 public ActionResult NewAnswer([FromBody] AnswerViewModel model)
 {
     try
     {
         var question = _questionService.GetQuestionById(model.QuestionId);
         model.Question = question;
         _answerService.CreateAnswer(model);
         return(Ok(question.Id));
     }
     catch (Exception e)
     {
         return(BadRequest("Something went wrong. Please contact support!"));
     }
 }
Esempio n. 6
0
        public async Task <IActionResult> CreateAnswer(CreateAnswerViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index", new { id = viewModel.PostId }).PushAlert("Answer is Valid", Core.Enums.AlertType.Error));
            }

            var createdAnswer = await answerService.CreateAnswer(viewModel.Content, viewModel.PostId);

            if (createdAnswer == null)
            {
                return(RedirectToAction("Index", new { id = viewModel.PostId }).PushAlert("Error occured when creating a answer"));
            }

            return(RedirectToAction("Index", new { id = viewModel.PostId }).PushAlert("Answer added successfully"));
        }
        public async Task <ActionResult> _CreateAnswer(AnswerEditModel model, Guid questionId)
        {
            if (ModelState.IsValid)
            {
                AnswerDTO answerDto = _mapper.Map <AnswerDTO>(model);

                OperationDetails result = await _answerService.CreateAnswer(answerDto);

                if (result.Succeeded)
                {
                    TempData["PartialMessageSuccess"] = result.Message;
                    return(RedirectToAction("_AnswersForQuestion", "Panel", new { questionId = answerDto.QuestionId }));
                }
                else
                {
                    ModelState.AddModelError(result.Property, result.Message);
                }
            }

            return(PartialView(model));
        }
Esempio n. 8
0
        public async Task <IActionResult> CreateAnswer([FromBody] CreateAnswer createAnswer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var response = await _answerService.CreateAnswer(createAnswer);

                if (response.Error != null)
                {
                    return(StatusCode(response.Error.ErrorCode, response.Error.ErrorDescriprion));
                }
                return(Ok(response.Data));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new Error(ex.Message)));
            }
        }
        public async Task <StandardResponse> AddAnswerToQuestion(ClaimsPrincipal identity, AddAnswerViewModel model, ModelStateDictionary modelState)
        {
            if (!modelState.IsValid)
            {
                return(modelState.StandardError());
            }

            using (var db = new ServiceDb())
            {
                using (var user = await _userService.Become(db, identity, null))
                {
                    var answer = await _answerService.CreateAnswer(db, user, model.QuestionId, model.Body);

                    await db.SaveChangesAsync();

                    return(StandardResponse.For(new
                    {
                        answer.AnswerId
                    }));
                }
            }
        }
Esempio n. 10
0
        private async void SubmitAnswer(object sender, EventArgs e)
        {
            var answer = _answerEntry.Text.Split(',').Select(word => word.Trim()).ToList();

            var isCorrect = _riddle.Check(answer);

            if (isCorrect)
            {
                var response = await _answerService.CreateAnswer(_riddle);

                if (response.IsSuccessStatusCode)
                {
                    await DisplayAlert(null, "Correct", "Dismiss");
                }
                else
                {
                    await DisplayAlert(null, "There hast been an error", "Dismiss");
                }
            }
            else
            {
                await DisplayAlert(null, "This Ain't It Chief", "Dismiss");
            }
        }
Esempio n. 11
0
        public ActionResult AddAnswer(int?testId, int?questionId)
        {
            if (testId == null || questionId == null)
            {
                return(View("Error"));
            }
            AnswerViewModel a = new AnswerViewModel()
            {
                AnswerValue = 0,
                QuestionId  = questionId.Value
            };
            QuestionViewModel q = questionService.GetById(a.QuestionId).ToMvcQuestion();

            foreach (QuestionItem item in q.Items)
            {
                a.Items.Add(new AnswerItem()
                {
                    ItemType = item.ItemType, BoolValue = false, Value = ""
                });
            }
            answerService.CreateAnswer(a.ToBllAnswer());
            a = answerService.GetByPredicate(entity => entity.QuestionId == questionId.Value).Last().ToMvcAnswer();
            return(RedirectToAction("EditAnswer", new { answerId = a.Id }));
        }
Esempio n. 12
0
 public Question CreateQuestion(CreateQuestionDto createQuestionDto)
 {
     if (_context.Quizzes
         .Any(q => q.Id == createQuestionDto.QuizId))
     {
         var question = new Question()
         {
             Text            = createQuestionDto.Text,
             SecondsToAnswer = createQuestionDto.SecondsToAnswer,
             Points          = createQuestionDto.Points,
             QuizId          = createQuestionDto.QuizId,
             Position        = createQuestionDto.Position
         };
         _context.Questions.Add(question);
         _context.SaveChanges();
         foreach (CreateAnswerDto createAnswerDto in createQuestionDto.Answers)
         {
             createAnswerDto.QuestionId = question.Id;
             answerService.CreateAnswer(createAnswerDto);
         }
         return(GetQuestion(question.Id));
     }
     return(null);
 }
Esempio n. 13
0
 public ActionResult <Answer> CreateAnswer(CreateAnswerDto createAnswerDto)
 {
     return(answerService.CreateAnswer(createAnswerDto));
 }
        public async Task <IActionResult> CreateAnswer([FromBody] CreateAnswerRequest request)
        {
            var result = await _answerService.CreateAnswer(request);

            return(Ok(result));
        }
Esempio n. 15
0
 public void CreateAnswer(Answer answer)
 {
     _answerService.CreateAnswer(answer);
 }