Exemple #1
0
        public async Task Create_WithCorrectData_ShouldSuccesfullyCreate()
        {
            string errorMessagePrefix = "AnswersService Method CreateAnswer() does not work properly.";

            var context = CDGBulgariaInmemoryFactory.InitializeContext();

            await SeedData(context);

            this.answersService = new AnswersService(context);

            AnswerServiceModel answerServiceModel = new AnswerServiceModel()
            {
                Content    = "Sofia is the place, where the association is founded.",
                QuestionId = "dfghrf3456"
            };

            bool actualResult = await this.answersService.CreateAnswer(answerServiceModel);

            Assert.True(actualResult, errorMessagePrefix);
        }
Exemple #2
0
        public async Task <bool> CreateAnswer(AnswerServiceModel answerServiceModel)
        {
            Question questionFromDb = await context.Questions.SingleOrDefaultAsync(q => q.Content == answerServiceModel.Question.Content);


            if (questionFromDb == null)
            {
                throw new ArgumentNullException(nameof(questionFromDb));
            }

            Answer answer = AutoMapper.Mapper.Map <Answer>(answerServiceModel);

            answer.Question   = questionFromDb;
            answer.QuestionId = questionFromDb.Id;


            await this.context.Answers.AddAsync(answer);

            int result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
        public async Task <IActionResult> Create(AnswerCreateInputModel answerCreateInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                var allQuestions = await this.questionsService.GetAllQuestions().ToListAsync();

                this.ViewData["questions"] = allQuestions.Select(question => new AnswerCreateQuestionViewModel
                {
                    //Id = question.Id,
                    Content = question.Content,
                });
                return(this.View(answerCreateInputModel));
            }
            string authorId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            //string author = this.User.FindFirst(ClaimTypes.Name).Value;

            AnswerServiceModel answerServiceModel = answerCreateInputModel.To <AnswerServiceModel>();

            answerServiceModel.AuthorId = authorId;

            await this.answersService.CreateAnswer(answerServiceModel);

            return(this.Redirect("/"));
        }