From 8836396ea1c2b52db05d691877618bb4f03a4513 Mon Sep 17 00:00:00 2001 From: Hemanth kumar Date: Sat, 18 May 2024 13:37:10 +0530 Subject: [PATCH] =?UTF-8?q?=E2=90=A1=20[Backend]=20delete=20Q&A=20API=20(#?= =?UTF-8?q?920)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/routes/Q&A/answers/deleteAnswer.js | 26 +++++++++++++++++ backend/app/routes/Q&A/answers/index.js | 5 ++++ .../app/routes/Q&A/question/deleteQuestion.js | 29 +++++++++++++++++++ backend/app/routes/Q&A/question/index.js | 5 ++++ 4 files changed, 65 insertions(+) create mode 100644 backend/app/routes/Q&A/answers/deleteAnswer.js create mode 100644 backend/app/routes/Q&A/question/deleteQuestion.js diff --git a/backend/app/routes/Q&A/answers/deleteAnswer.js b/backend/app/routes/Q&A/answers/deleteAnswer.js new file mode 100644 index 00000000..96a2469a --- /dev/null +++ b/backend/app/routes/Q&A/answers/deleteAnswer.js @@ -0,0 +1,26 @@ +const mongoose = require('mongoose'); +const Answer = require('../../../models/answers'); + +module.exports = async (req, res, next) => { + try { + const payload = res.locals.decode; + const { answerId } = req.body; + + if (!payload.isSuperAdmin) { + return res.status(401).json({ error: 'You are not authorized to perform this action' }); + } + + if (!mongoose.Types.ObjectId.isValid(answerId)) { + return res.status(400).json({ error: 'Invalid answer ID' }); + } + + const answer = await Answer.findByIdAndDelete(answerId); + if (!answer) { + return res.status(404).json({ error: 'Answer not found' }); + } + + return res.status(200).json({ message: 'Answer deleted successfully' }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/backend/app/routes/Q&A/answers/index.js b/backend/app/routes/Q&A/answers/index.js index 0984a1ba..86ea6772 100644 --- a/backend/app/routes/Q&A/answers/index.js +++ b/backend/app/routes/Q&A/answers/index.js @@ -7,6 +7,8 @@ const getAnswers = require('./getAnswers'); const upvoteAnswer = require('./upvoteAnswer'); const downvoteAnswer = require('./downvoteAnswer'); const updateAnswerStatus = require('./updateAnswerStatus'); +const { authMiddleware } = require('../../../../helpers/middlewares/auth'); +const deleteAnswer = require('./deleteAnswer'); // POST API FOR ANSWER router.post('/', validation(answerValidationSchema), postAnswer); @@ -23,4 +25,7 @@ router.patch('/downvote', downvoteAnswer); // Update Answer Status router.patch('/updateStatus', validation(updateAnswerStatusSchema), updateAnswerStatus); +// Delete Answer by Id +router.delete('/deleteanswer', authMiddleware, deleteAnswer); + module.exports = router; diff --git a/backend/app/routes/Q&A/question/deleteQuestion.js b/backend/app/routes/Q&A/question/deleteQuestion.js new file mode 100644 index 00000000..e8d21190 --- /dev/null +++ b/backend/app/routes/Q&A/question/deleteQuestion.js @@ -0,0 +1,29 @@ +const mongoose = require('mongoose'); +const Question = require('../../../models/question'); +const Answer = require('../../../models/answers'); + +module.exports = async (req, res, next) => { + try { + const payload = res.locals.decode; + const { questionId } = req.body; + + if (!payload.isSuperAdmin) { + return res.status(401).json({ error: 'You are not authorized to perform this action' }); + } + + if (!mongoose.Types.ObjectId.isValid(questionId)) { + return res.status(400).json({ error: 'Invalid question ID' }); + } + + const question = await Question.findByIdAndDelete(questionId); + if (!question) { + return res.status(404).json({ error: 'Question not found' }); + } + + await Answer.deleteMany({ question_id: questionId }); + + return res.status(200).json({ message: 'Question and its answers deleted successfully' }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/backend/app/routes/Q&A/question/index.js b/backend/app/routes/Q&A/question/index.js index 9271bdc9..2f1c35dd 100644 --- a/backend/app/routes/Q&A/question/index.js +++ b/backend/app/routes/Q&A/question/index.js @@ -7,6 +7,8 @@ const validation = require('../../../../helpers/middlewares/validation'); const upvoteQuestion = require('./upvoteQuestion'); const downvoteQuestion = require('./downvoteQuestion'); const updateQuestionStatus = require('./updateQuestionStatus'); +const { authMiddleware } = require('../../../../helpers/middlewares/auth'); +const deleteQuestion = require('./deleteQuestion'); router.post('/', validation(QuestionValidationSchema), postQuestion); @@ -25,4 +27,7 @@ router.patch('/downvote', downvoteQuestion); // route for updating the question status router.patch('/updateStatus', validation(updateQuestionStatusSchema), updateQuestionStatus); +// route for deleting the question with answers +router.delete('/deletequestion', authMiddleware, deleteQuestion); + module.exports = router;