Beispiel #1
0
        public IActionResult Create(QuestionModelView model)
        {
            model.Category          = _categoryService.Get(model.CategoryId);
            model.Question.Category = model.Category;
            model.Question.Answers  = model.Answers;

            int    correctAnswerId = model.Answers[model.CorrectAnswerId].Id;
            Answer correctAnswer   = new Answer
            {
                Text     = model.Answers[correctAnswerId].Text,
                Question = model.Question
            };
            bool correctAnswerCreated = _answerService.Create(correctAnswer);

            Question question = new Question
            {
                Text            = model.Question.Text,
                Answers         = model.Answers,
                CorrectAnswerId = correctAnswer.Id,
                Category        = model.Category,
                CategoryId      = model.CategoryId
            };

            bool questionCreated = _questionService.Create(question);

            if (correctAnswerCreated && questionCreated)
            {
                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }
Beispiel #2
0
        public IActionResult Create(AnswerVM model)
        {
            if (ModelState.IsValid)
            {
                var answer = _answerService.GetAll().Where(x => x.QuestionId == model.QuestionId && x.Text == model.Text).FirstOrDefault();

                if (answer == null)
                {
                    int count = _answerService.GetAll().Where(x => x.QuestionId == model.QuestionId && x.IsRight == true && x.IsRight == model.IsRight).Count();

                    if (count == 0)
                    {
                        _answerService.Create(model);

                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "The right answer to this question already has!");
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "The answer to this question already has!");
                }
            }

            ViewBag.QuestionList = _questionService.GetAll();

            return(View());
        }
Beispiel #3
0
        public async Task <IActionResult> Create([Bind("Id,PublishDateTime,Content,OcurrenceId,ProfileId")] AnswerViewModel answerViewModel)
        {
            ApplicationUser usr = await GetCurrentUserAsync();

            var currentUserGuid = Guid.Parse(usr.Id);

            if (currentUserGuid == null)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                return(View(answerViewModel));
            }

            var answer = _mapper.Map <Answer>(answerViewModel);

            answer.ProfileId       = currentUserGuid;
            answer.OcurrenceId     = Guid.Parse(HttpContext.Session.GetString("OcurrenceId"));
            answer.PublishDateTime = DateTime.Now;

            await _answerService.Create(answer);

            var url = Url.Action("GetOcurrenceAnswers", "Ocurrences", new { id = answer.OcurrenceId });

            return(Json(new { success = true, url }));
        }
Beispiel #4
0
        public ActionResult EditAnswer(ViewAnswerModel answer)
        {
            ViewBag.User = userService.GetUserByEmail(User.Identity.Name);
            int flag = 0;

            foreach (var item in answerService.GetAllByQuestionId(answer.QuestionId))
            {
                if (item.Text_of_answer.Equals(answer.Text_of_answer))
                {
                    flag++;
                    break;
                }
            }
            if (flag > 0 && answer.Id == 0)
            {
                ModelState.AddModelError("", "Такой ответ уже существует");
            }
            if (ModelState.IsValid)
            {
                if (answer.Id == 0)
                {
                    var       mapper    = new MapperConfiguration(cfg => cfg.CreateMap <ViewAnswerModel, AnswerDTO>()).CreateMapper();
                    AnswerDTO answerDTO = mapper.Map <ViewAnswerModel, AnswerDTO>(answer);
                    answerService.Create(answerDTO);
                    TempData["message"] = string.Format("Добавление ответа выполнено успешно");
                }
                else
                {
                    AnswerDTO newAnswer = answerService.GetById(answer.Id);
                    if (newAnswer != null)
                    {
                        var mapper = new MapperConfiguration(cfg => cfg.CreateMap <ViewAnswerModel, AnswerDTO>()).CreateMapper();
                        newAnswer = mapper.Map <ViewAnswerModel, AnswerDTO>(answer);
                        answerService.Update(newAnswer);
                        TempData["message"] = string.Format("Добавление ответа выполнено успешно");
                    }
                }

                if (userService.GetUserByEmail(User.Identity.Name).RoleId == 2)
                {
                    return(RedirectToAction("TeacherPanel", "Teacher", new
                    {
                        result = "answers",
                        questType = questService.GetById(questionService.GetById(answer.QuestionId).QuestId).Type,
                        id = answer.QuestionId
                    }));
                }
                return(RedirectToAction("AdminPanel", new
                {
                    result = "answers",
                    questType = questService.GetById(questionService.GetById(answer.QuestionId).QuestId).Type,
                    id = answer.QuestionId
                }));
            }
            ViewBag.QuestType = questService.GetById(questionService.GetById(answer.QuestionId).QuestId).Type;
            return(View());
        }
Beispiel #5
0
        public IActionResult Create([FromBody] Answer answer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            _service.Create(answer);
            return(Ok(answer));
        }
        public ActionResult Create([Bind(Include = "ID,Desctiption,Correct,QuestionID")] Answer answer)
        {
            if (ModelState.IsValid)
            {
                answerService.Create(answer);
                return(RedirectToAction("Index"));
            }

            ViewBag.QuestionID = new SelectList(questionService.GetAll(), "ID", "Description", answer.QuestionID);
            return(View(answer));
        }
Beispiel #7
0
        public ActionResult CreateQuestionAnswer(AnswerViewModel model)
        {
            if (model == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var answer = model.ToBllAnswer();

            answerService.Create(answer);

            return(RedirectToAction("DetailsQuestionAnswers", "Answer", new { questionId = model.QuestionId }));
        }
Beispiel #8
0
        public ActionResult Detail(Guid id, AnswerViewModels model)
        {
            Answer ans = new Answer()
            {
                Id         = Guid.NewGuid(),
                Content    = model.Content,
                CreateDate = DateTime.Now,
                CreateBy   = User.Identity.GetUserName(),
                QuestionID = id
            };

            var answerquestion = _answerService.Create(ans);

            return(RedirectToAction("Detail"));
        }
Beispiel #9
0
        public IActionResult Create(AnswerModelView model)
        {
            if (ModelState.IsValid)
            {
                Answer answer = new Answer
                {
                    Text       = model.Text,
                    Question   = _questionService.Get(model.QuestionId),
                    QuestionId = model.QuestionId
                };

                _answerService.Create(answer);

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }
Beispiel #10
0
        public async Task <IActionResult> CreateAnswer([FromBody] AnswerForCreationDto model)
        {
            // maps resource to a domain model
            var answer = _mapper.Map <Answer>(model);

            try
            {
                // create answer
                var answerCreated = await _answerService.Create(answer, model.QuestionId);

                return(Ok(_mapper.Map <AnswerDto>(answerCreated)));
            }
            catch (Exception ex)
            {
                // return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }
Beispiel #11
0
        public IActionResult CreateAnswer([FromBody] CreateAnswerModel model)
        {
            // map model to entity
            var answer = _mapper.Map <Answer>(model);

            try
            {
                // create user answer
                _answerService.Create(answer);
                return(Ok(new {
                    data = answer,
                    message = "Successfully saved user answer."
                }));
            }
            catch (AppException ex)
            {
                // return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }
Beispiel #12
0
 public JsonResult Create([FromBody] Answer obj)
 {
     return(Json(_AnswerService.Create(obj)));
 }
Beispiel #13
0
        public IActionResult Create(AnswerDto answer)
        {
            _answerService.Create(answer);

            return(View());
        }
Beispiel #14
0
 public IActionResult Create(CreateAnswerRequestModel model)
 {
     return(Ok(_answer.Create(model)));
 }