-
Notifications
You must be signed in to change notification settings - Fork 0
created: Like post controller #2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| const { Like, TargetType_ENUM } = require("../models/Like"); | ||
| const Post = require("../models/Post"); | ||
| const mongoose = require("mongoose"); | ||
|
|
||
| /** | ||
| * @desc user can like the post of the other developers | ||
| * @route GET /posts/:id/like | ||
| */ | ||
| async function likePost(req, res) { | ||
| try { | ||
| const PostId = req.params.id; | ||
| if (!PostId) | ||
| return res.status(400).json({ msg: "Post can't be undefined" }); | ||
| if (!mongoose.Types.ObjectId.isValid(PostId)) | ||
| return res.status(400).json({ msg: "Post Id is not Valid" }); | ||
| const postExists = Post.findById(PostId); | ||
| if (!postExists) | ||
| return res.status(404).json({ msg: "Post does not Exists" }); | ||
| const likeExists = Like.findOne({ | ||
| targetId: new mongoose.Types.ObjectId(PostId), | ||
| }).select("_id"); | ||
| if (!likeExists) { | ||
| const likedPost = await Like.create({ | ||
| targetType: TargetType_ENUM.POST, | ||
| count: 1, | ||
| targetId: PostId, | ||
| }); | ||
| console.log(likedPost); | ||
| } | ||
| else{ | ||
| const likeUpdated = await Like.updateOne({ targetId: PostId},{ $inc: {count: 1}}); | ||
| console.log(likeUpdated); | ||
| } | ||
| return res.status(200).json({ msg: "Post Liked Successfully" }); | ||
| } catch (err) { | ||
| console.log(err.stack); | ||
| return res.status(500).json({ msg: "Internal Server Error" }); | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| likePost, | ||
| }; |
| Original file line number | Diff line number | Diff line change | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,16 +4,19 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const {generateToken, verifyToken} = require('../middleware/tokenVerification.js'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { createPost, showPost, showAllPost } = require('../controller/postController.js'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { sendFollow } = require('../controller/followController.js'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { likePost } = require('../controller/likeController.js'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const router = express.Router(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const app = express(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.use(express.json()); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.post('/signup', isUserAuthenticated, generateToken, createUser); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.post('/login', generateToken, loginUser); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.get('/:id', findUser); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.get('/:id', verifyToken, findUser); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Check failureCode scanning / CodeQL Missing rate limiting High
This route handler performs
a database access Error loading related location Loading
Copilot AutofixAI 4 months ago The best way to fix this is to add a rate limiting middleware to the Express router in Specifically:
Suggested changeset
2
backend/routes/user-route.js
backend/package.json
Outside changed files
This fix introduces these dependencies
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.post('/post/create', verifyToken, createPost); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.get('/post/:id', verifyToken, showPost); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.post('/posts', verifyToken, showAllPost); // this should work with GET request but it is not working, instead it returing a 404 on send request in GET format | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.post('/people', verifyToken, getAllUser); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.post('/follow/:id', verifyToken, sendFollow); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.put('/posts/:id/like', verifyToken, likePost); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Check failureCode scanning / CodeQL Missing rate limiting High
This route handler performs
authorization Error loading related location Loading
Copilot AutofixAI 4 months ago The best way to fix this issue is to add a rate-limiting middleware to the relevant route handler(s), specifically to the endpoint
Suggested changeset
2
backend/routes/user-route.js
backend/package.json
Outside changed files
This fix introduces these dependencies
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
Check failureCode scanning / CodeQL Missing rate limiting High
This route handler performs
a database access Error loading related location Loading
Copilot AutofixAI 4 months ago The best way to fix the problem is to add rate-limiting middleware to the relevant endpoint(s), ensuring that requests to endpoints that trigger database actions (such as liking a post) cannot be abused to cause excessive load. In this context, the route
These changes should be made only within the shown code in
Suggested changeset
2
backend/routes/user-route.js
backend/package.json
Outside changed files
This fix introduces these dependencies
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| module.exports = router; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Check failure
Code scanning / CodeQL
Missing rate limiting High
Copilot Autofix
AI 4 months ago
To fix this issue, add a rate limiting middleware, preferably at the router level or for the specific route(s) that perform database operations. We'll use the popular
express-rate-limitpackage for simplicity and robustness.express-rate-limitimport at the top of the file./user/:id(line 14), and optionally to the other database-backed routes visible in this code snippet.This only requires changes to
backend/routes/user-route.js, specifically:express-rate-limitlimiterto the middleware chain for/user/:idroute (router.get('/:id', ...)).