Skip to content

Commit

Permalink
🍁 Backend : Upvote and Downvote for Answers Fixed (#1110)
Browse files Browse the repository at this point in the history
* Upvote and Downvotes fixed

* Upvote/downvote fixed in backend
  • Loading branch information
BHS-Harish authored Aug 6, 2024
1 parent 5c08b64 commit 526b35e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
4 changes: 4 additions & 0 deletions backend/app/models/answers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const answerSchema = new Schema(
type: Number,
default: 0,
},
downvotes:{
type:Number,
default:0
},
created_on: {
type: Date,
required: true,
Expand Down
5 changes: 5 additions & 0 deletions backend/app/models/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const questionSchema = new Schema(
downvotes:{
type:Number,
default:0
},
created_by:{
type:String,
required:true,
trim:true
}
},
{ timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } }
Expand Down
24 changes: 4 additions & 20 deletions backend/app/routes/Q&A/answers/downvoteAnswer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@ const to = require('await-to-js').default;
const answer = require('../../../models/answers');
const { ErrorHandler } = require('../../../../helpers/error');
const constants = require('../../../../constants');
const { getVoteCookieName } = require('../../../../helpers/middlewares/cookie');

module.exports = async (req, res, next) => {
const { answerId } = req.body;
const [err] = await to(
answer.updateOne({ _id: answerId }, [
{
$set: {
upvotes: {
$cond: [
{
$gt: ['$upvotes', 0],
},
{
$subtract: ['$upvotes', 1],
},
0,
],
},
},
},
])
);

const [err] = await to(answer.updateOne({ _id: answerId }, { $inc: { downvotes: 1 } }));
if (err) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
Expand All @@ -35,6 +17,8 @@ module.exports = async (req, res, next) => {
return next(error);
}

res.cookie(getVoteCookieName('answer', answerId), true, { maxAge: 20 * 365 * 24 * 60 * 60 * 1000,sameSite:"none",secure:true });

res.status(200).send({
message: 'Answer has been down voted',
});
Expand Down
5 changes: 3 additions & 2 deletions backend/app/routes/Q&A/answers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const downvoteAnswer = require('./downvoteAnswer');
const updateAnswerStatus = require('./updateAnswerStatus');
const { authMiddleware } = require('../../../../helpers/middlewares/auth');
const deleteAnswer = require('./deleteAnswer');
const { checkVoteCookie } = require('../../../../helpers/middlewares/cookie');

// POST API FOR ANSWER
router.post('/', validation(answerValidationSchema), postAnswer);
Expand All @@ -17,10 +18,10 @@ router.post('/', validation(answerValidationSchema), postAnswer);
router.get('/:questionId', getAnswers);

// INCREASE UPVOTE FOR ANSWERS
router.patch('/upvote', upvoteAnswer);
router.patch('/upvote', checkVoteCookie,upvoteAnswer);

// DECREASE UPVOTE FOR ANSWERS
router.patch('/downvote', downvoteAnswer);
router.patch('/downvote', checkVoteCookie,downvoteAnswer);

// Update Answer Status
router.patch('/updateStatus', validation(updateAnswerStatusSchema), updateAnswerStatus);
Expand Down
3 changes: 3 additions & 0 deletions backend/app/routes/Q&A/answers/upvoteAnswer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const to = require('await-to-js').default;
const answer = require('../../../models/answers');
const { ErrorHandler } = require('../../../../helpers/error');
const constants = require('../../../../constants');
const { getVoteCookieName } = require('../../../../helpers/middlewares/cookie');

module.exports = async (req, res, next) => {
const { answerId } = req.body;
Expand All @@ -16,6 +17,8 @@ module.exports = async (req, res, next) => {
return next(error);
}

res.cookie(getVoteCookieName('answer', answerId), true, { maxAge: 20 * 365 * 24 * 60 * 60 * 1000,sameSite:"none",secure:true });

res.status(200).send({
message: 'Answer has been upvoted',
});
Expand Down
1 change: 1 addition & 0 deletions backend/app/routes/Q&A/question/@validationSchema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const QuestionValidationSchema = Joi.object().keys({
title: Joi.string().trim().required().min(5),
description: Joi.string().trim().required().min(10),
tags: Joi.array().required(),
created_by:Joi.string().trim().required().min(5)
});

const updateQuestionStatusSchema = Joi.object().keys({
Expand Down

0 comments on commit 526b35e

Please sign in to comment.