コード例 #1
0
        public async Task <IActionResult> AddQuiz(QuizDto quiz)
        {
            var questionToAdd = new QuizQuestion();

            questionToAdd.Question = quiz.Question;

            _repo.Add(questionToAdd);

            if (await _repo.SaveAll())
            {
                foreach (var option in quiz.Options)
                {
                    var optionToAdd = new Option();
                    optionToAdd.Answer    = option.Answer;
                    optionToAdd.IsCorrect = option.isCorrect;

                    optionToAdd.Question = await _repo.GetQuizQuestionAsync(questionToAdd.Id);

                    _repo.Add(optionToAdd);
                    await _repo.SaveAll();
                }

                return(Ok(quiz));
            }

            throw new Exception("Question failed to add");
        }
コード例 #2
0
        public async Task <IActionResult> AddArticle(ArticleToCreateDto articleToCreate)
        {
            var newArticle = _mapper.Map <Article>(articleToCreate);

            newArticle.Category = await _repo.GetCategoryAsync(articleToCreate.CategoryId);

            newArticle.Writer = await _repo.GetUserAsync(articleToCreate.WriterId);

            _repo.Add(newArticle);

            if (await _repo.SaveAll())
            {
                return(Ok(newArticle));
            }

            throw new Exception("Article failed to add");
        }
コード例 #3
0
        public async Task <IActionResult> AddComment(CommentToCreateDto commentToCreate)
        {
            var comment = _mapper.Map <Comment>(commentToCreate);

            comment.article = await _repo.GetArticleAsync(commentToCreate.ArticleId);

            comment.Commenter = await _repo.GetUserAsync(commentToCreate.CommenterId);

            _repo.Add(comment);

            if (await _repo.SaveAll())
            {
                return(Ok(comment));
            }

            throw new Exception("Comment failed to post");
        }
コード例 #4
0
        public async Task <IActionResult> AddQuestion(QuestionToCreateDto questionToCreate)
        {
            var newquestion = _mapper.Map <Question>(questionToCreate);

            newquestion.Category = await _repo.GetCategoryAsync(questionToCreate.CategoryId);

            newquestion.QuestionBy = await _repo.GetUserAsync(questionToCreate.QuestionerId);

            _repo.Add(newquestion);

            if (await _repo.SaveAll())
            {
                return(Ok(newquestion));
            }

            throw new Exception("Question failed to add");
        }
コード例 #5
0
        public async Task <IActionResult> AddAnswer(AnswerToCreateDto answerToCreate)
        {
            var answer = _mapper.Map <Answer>(answerToCreate);


            answer.AnsweredBy = await _repo.GetUserAsync(answerToCreate.AnswererId);

            answer.Question = await _repo.GetQuestionAsync(answerToCreate.QuestionId);

            _repo.Add(answer);

            if (await _repo.SaveAll())
            {
                return(Ok(answer));
            }

            throw new Exception("answer failed to add");
        }