コード例 #1
0
        public async Task <IActionResult> AddQuestion(QuestionViewModel questionViewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var currentlySignedInUser = await _userManager.GetUserAsync(User);

                    questionViewModel.UserId = currentlySignedInUser.Id;
                    string uniqueFileName = null;
                    if (questionViewModel.Image != null && _fileOperations.ValidateImageType(questionViewModel.Image.FileName) == true)
                    {
                        uniqueFileName = await SetPathAndUpload(questionViewModel);
                    }
                    var question = _mapper.Map <QuestionViewModel, Question>(questionViewModel);
                    question.ImageNamePath = uniqueFileName;
                    var resultQuestion = await _repository.AddQuestionAsync(question);

                    return(RedirectToAction("Details", new { questionId = resultQuestion.Id }));
                }
                catch (DbUpdateException dbex)
                {
                    ViewData["ErrorMessage"] = "DB issue - " + dbex.Message;
                    return(View("Error"));
                }
                catch (Exception ex)
                {
                    ViewData["ErrorMessage"] = ex.Message;
                    return(View("Error"));
                }
            }
            return(View("AddQuestion"));
        }
コード例 #2
0
        public async Task <IActionResult> AddAnswer(AnswerViewModel answerViewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var question = await _repository.GetQuestionByIdWithoutDetailsAsync(answerViewModel.QuestionId);

                    if (question == null)
                    {
                        Response.StatusCode      = 404;
                        ViewData["ErrorMessage"] = "404 Resource not found.";
                        return(View("Error"));
                    }
                    var currentlyLoggedInUser = await _userManager.GetUserAsync(User);

                    answerViewModel.UserId = currentlyLoggedInUser.Id;
                    string uniqueFileName = null;
                    if (answerViewModel.Image != null && _fileOperations.ValidateImageType(answerViewModel.Image.FileName) == true)
                    {
                        uniqueFileName = await SetPathAndUpload(answerViewModel);
                    }
                    var answer = _mapper.Map <AnswerViewModel, Answer>(answerViewModel);
                    answer.ImageNamePath = uniqueFileName;
                    await _repository.AddAnswerAsync(answer);

                    return(RedirectToAction("Details", "Questions", new { questionId = answerViewModel.QuestionId }));
                }
                catch (DbUpdateException dbex)
                {
                    ViewData["ErrorMessage"] = "DB issue - " + dbex.Message;
                    return(View("Error"));
                }
                catch (Exception ex)
                {
                    ViewData["ErrorMessage"] = ex.Message;
                    return(View("Error"));
                }
            }
            return(View("AddAnswer", answerViewModel));
        }