Example #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");
        }
Example #2
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");
        }
Example #3
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");
        }
Example #4
0
        public async Task <IActionResult> UpdateUser(string id, UserToUpdateDto userToUpdate)
        {
            var user = await _repo.GetUserAsync(id);

            if (id.Equals(userToUpdate.Id))
            {
                BadRequest("You are not permitted");
            }

            _mapper.Map(userToUpdate, user);

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

            throw new Exception("Article failed to update");
        }
Example #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");
        }
Example #6
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");
        }