Skip to content

Commit

Permalink
Merge pull request #642 from ita-social-projects/feature/639/get-ques…
Browse files Browse the repository at this point in the history
…tion

Added get question by id service
  • Loading branch information
Marichka0406 authored Nov 21, 2023
2 parents 215d9d1 + a46c3c1 commit 7fd7f1c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
9 changes: 9 additions & 0 deletions controllers/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ const getQuestions = async (req, res) => {
res.status(200).json(questions)
}

const getQuestionById = async (req, res) => {
const { id } = req.params

const question = await questionService.getQuestionById(id)

res.status(200).json(question)
}

const createQuestion = async (req, res) => {
const { id: author } = req.user
const data = req.body
Expand Down Expand Up @@ -51,6 +59,7 @@ const updateQuestion = async (req, res) => {

module.exports = {
getQuestions,
getQuestionById,
createQuestion,
deleteQuestion,
updateQuestion
Expand Down
3 changes: 1 addition & 2 deletions routes/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ const {
roles: { TUTOR }
} = require('~/consts/auth')


router.use(authMiddleware)
const params = [{ model: Question, idName: 'id' }]

router.get('/', asyncWrapper(questionController.getQuestions))
router.get('/:id', isEntityValid({ params }), asyncWrapper(questionController.getQuestionById))
router.use(restrictTo(TUTOR))
router.post('/', asyncWrapper(questionController.createQuestion))
router.delete('/:id', isEntityValid({ params }), asyncWrapper(questionController.deleteQuestion))
router.patch('/:id', isEntityValid({ params }), asyncWrapper(questionController.updateQuestion))


module.exports = router
4 changes: 4 additions & 0 deletions services/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const questionService = {
return { items, count }
},

getQuestionById: async (id) => {
return await Question.findById(id).populate({ path: 'category', select: 'name' }).lean().exec()
},

createQuestion: async (author, data) => {
const { title, text, answers, type, category } = data

Expand Down

0 comments on commit 7fd7f1c

Please sign in to comment.