Skip to content

Commit

Permalink
🍁 [Backend] Limiting the Number of upvotes/downvotes a particular use…
Browse files Browse the repository at this point in the history
…r can do Fixed (#942)

* cook

* Update .env

* Update .env

* Update .env

* Update .env

* Update .env

* Update .env
  • Loading branch information
Hemu21 authored May 22, 2024
1 parent 60fde3a commit 03ab618
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const helmet = require('helmet');
const responseTime = require('response-time');
const routes = require('./app/routes');
const { errorHandler } = require('./helpers/error');
const cookieParser = require('cookie-parser');
require('colors');
require('./helpers/dbConnection');

Expand All @@ -24,6 +25,9 @@ app.use(express.urlencoded({ limit: '50mb', extended: true }));
// Response time
app.use(responseTime({ suffix: false }));

// cookie
app.use(cookieParser());

// Use routes
app.use('/', routes);

Expand Down
2 changes: 2 additions & 0 deletions backend/app/routes/Q&A/question/downvoteQuestion.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 question = require('../../../models/question');
const { ErrorHandler } = require('../../../../helpers/error');
const constants = require('../../../../constants');
const { getVoteCookieName } = require('../../../../helpers/middlewares/cookie');

module.exports = async (req, res, next) => {
const { questionId } = req.body;
Expand Down Expand Up @@ -35,6 +36,7 @@ module.exports = async (req, res, next) => {
return next(error);
}

res.cookie(getVoteCookieName('question', questionId), true, { maxAge: 20 * 365 * 24 * 60 * 60 * 1000 });
res.status(200).send({
message: 'Question has been down voted',
});
Expand Down
5 changes: 3 additions & 2 deletions backend/app/routes/Q&A/question/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const downvoteQuestion = require('./downvoteQuestion');
const updateQuestionStatus = require('./updateQuestionStatus');
const { authMiddleware } = require('../../../../helpers/middlewares/auth');
const deleteQuestion = require('./deleteQuestion');
const { checkVoteCookie } = require('../../../../helpers/middlewares/cookie');

router.post('/', validation(QuestionValidationSchema), postQuestion);

Expand All @@ -19,10 +20,10 @@ router.get('/getallquestions', getAllQuestion);
router.get('/getQuestionById/:questionId', getQuestionById);

// This route will increase upvote by one.
router.patch('/upvote', upvoteQuestion);
router.patch('/upvote',checkVoteCookie, upvoteQuestion);

// This route will decrease upvote by one.
router.patch('/downvote', downvoteQuestion);
router.patch('/downvote',checkVoteCookie, downvoteQuestion);

// route for updating the question status
router.patch('/updateStatus', validation(updateQuestionStatusSchema), updateQuestionStatus);
Expand Down
4 changes: 3 additions & 1 deletion backend/app/routes/Q&A/question/upvoteQuestion.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 question = require('../../../models/question');
const { ErrorHandler } = require('../../../../helpers/error');
const constants = require('../../../../constants');
const { getVoteCookieName } = require('../../../../helpers/middlewares/cookie');

module.exports = async (req, res, next) => {
const { questionId } = req.body;
Expand All @@ -12,10 +13,11 @@ module.exports = async (req, res, next) => {
message: 'Database Error',
errStack: err,
});

return next(error);
}

res.cookie(getVoteCookieName('question', questionId), true, { maxAge: 20 * 365 * 24 * 60 * 60 * 1000 });

res.status(200).send({
message: 'Question has been upvoted',
});
Expand Down
16 changes: 16 additions & 0 deletions backend/helpers/middlewares/cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const getVoteCookieName = (type, id) => `${type}_voted_${id}`;

const checkVoteCookie = (req, res, next) => {
const { questionId, answerId } = req.body;
const voteType = questionId ? 'question' : 'answer';
const voteId = questionId || answerId;
const voteCookieName = getVoteCookieName(voteType, voteId);

if (req.cookies[voteCookieName]) {
return res.status(400).json({ error: 'You have already voted' });
}

next();
};

module.exports = { checkVoteCookie, getVoteCookieName }
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"chai-http": "^4.3.0",
"colors": "^1.4.0",
"config": "^3.3.3",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"ejs": "^3.1.6",
Expand Down
2 changes: 1 addition & 1 deletion frontend/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
REACT_APP_API="https://community-website-backend.onrender.com"
REACT_APP_API="https://community-website-backend.onrender.com"

0 comments on commit 03ab618

Please sign in to comment.