Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend : Upvote and Downvote for Answers Fixed #1110

Merged
merged 5 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 } }));
BHS-Harish marked this conversation as resolved.
Show resolved Hide resolved
if (err) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
Expand All @@ -35,6 +17,8 @@
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
Loading