Skip to content

Commit

Permalink
␑ [Backend] delete Q&A API (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemu21 authored May 18, 2024
1 parent 115f049 commit 8836396
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
26 changes: 26 additions & 0 deletions backend/app/routes/Q&A/answers/deleteAnswer.js
Original file line number Diff line number Diff line change
@@ -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' });
}
};
5 changes: 5 additions & 0 deletions backend/app/routes/Q&A/answers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
29 changes: 29 additions & 0 deletions backend/app/routes/Q&A/question/deleteQuestion.js
Original file line number Diff line number Diff line change
@@ -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' });
}
};
5 changes: 5 additions & 0 deletions backend/app/routes/Q&A/question/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;

0 comments on commit 8836396

Please sign in to comment.