From 854ce1d8be5579d8dd88ad07683a3763abe7911c Mon Sep 17 00:00:00 2001 From: Akalanka-1996 Date: Sun, 3 Jul 2022 13:16:33 +0530 Subject: [PATCH] get logged in user questions --- config/connection.js | 4 +++- controller/questionController.js | 11 ++++++++++- routes/questions.js | 3 ++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/config/connection.js b/config/connection.js index 33d2eb8..54d7859 100644 --- a/config/connection.js +++ b/config/connection.js @@ -17,4 +17,6 @@ const connectDB = async () => { } } -module.exports = connectDB \ No newline at end of file +module.exports = connectDB + + diff --git a/controller/questionController.js b/controller/questionController.js index dc3a99a..2e1caad 100644 --- a/controller/questionController.js +++ b/controller/questionController.js @@ -21,12 +21,21 @@ const getQuestionsById = asyncHandler(async (req, res) => { } }) +// get logged in user questions + +const getQuestionByUser = asyncHandler(async (req, res) => { + const questions = await Question.find({user: req.user._id}) + res.json(questions) +}) + // create a question const createQuestion = asyncHandler(async (req, res) => { const {grade, subject, topic, description, image, replies} = req.body + console.log(req.body) + console.log(req.user) if(!grade || !subject || !description) { res.status(400) @@ -124,4 +133,4 @@ const searchQuestion = asyncHandler(async (req, res) => { res.send(questions) }) -module.exports = {getQuestions, createQuestion, getQuestionsById, updateQuestion, deleteQuestion, searchQuestion} +module.exports = {getQuestions, createQuestion, getQuestionsById, updateQuestion, deleteQuestion, searchQuestion, getQuestionByUser} diff --git a/routes/questions.js b/routes/questions.js index 226ef86..bf681f2 100644 --- a/routes/questions.js +++ b/routes/questions.js @@ -1,10 +1,11 @@ const router = require('express').Router() -const {getQuestions, getQuestionsById, createQuestion, updateQuestion, deleteQuestion, searchQuestion} = require('../controller/questionController') +const {getQuestions, getQuestionsById, createQuestion, updateQuestion, deleteQuestion, searchQuestion, getQuestionByUser} = require('../controller/questionController') const {protect} = require('../middleware/authMiddleware') const {grantAccess} = require('../permission/permission') router.route('/').get(getQuestions) +router.route('/get-question-by-user').get(protect, getQuestionByUser) router.route('/getquestions').get(protect, searchQuestion) router.route('/create').post(protect, createQuestion) router.route('/:id').get(getQuestionsById).put(protect, updateQuestion).delete(protect, deleteQuestion)