diff --git a/backend-services/comment-service/src/controllers/Comment.ts b/backend-services/comment-service/src/controllers/Comment.ts index 66b17ded12516609b15652abe1a3d208be2a25fa..bc98ff8dd26e82cdbe3eba831e8e29f8383e09c5 100644 --- a/backend-services/comment-service/src/controllers/Comment.ts +++ b/backend-services/comment-service/src/controllers/Comment.ts @@ -167,53 +167,7 @@ const toggleLike = async ( req: Request, res: Response, next: NextFunction,) => } - - - - /* - const query = {_id : new mongoose.Types.ObjectId(_id)}; - const comment = await Comment.findOne(query) - - - - - - try{ - - if (!comment?.users_liked.length) { - let update = {$push: {users_liked : [user_id]}}; - await Comment.findOneAndUpdate(query, update); - } - else{ - let likedByUser = false - for (var val of comment.users_liked) { - if(val == user_id){ - console.log("Already Liked") - likedByUser = true - } - } - if(likedByUser){ - let update = {$pull: {users_liked : user_id}}; - console.log("Removed Like"); - Comment.findOneAndUpdate(query, update); - await Comment.findOneAndUpdate(query, update); - - }else{ - let update = {$push: {users_liked : [user_id]}}; - console.log("Added Like"); - await Comment.findOneAndUpdate(query, update); - } - } - - - - } - catch{ - Logging.error("Incorrect") - } - */ - - + const token = req.headers.authorization?.split(' ')[1]; if (!token) { return res.status(401).send({ message: 'Unauthorised' }); @@ -281,6 +235,79 @@ const toggleLike = async ( req: Request, res: Response, next: NextFunction,) => }; -export default { createComment, readAllComments, toggleLike} + +const likedState = async ( req: Request, res: Response, next: NextFunction,) => { + const { _id, user_id, } = req.body; + + + if (_id == null || _id === 'undefined') { + return res.status(400).send({ message: 'Bad Request' }); + } + + if (user_id == null || user_id === 'undefined') { + return res.status(400).send({ message: 'Bad Request' }); + } + + const token = req.headers.authorization?.split(' ')[1]; + if (!token) { + return res.status(401).send({ message: 'Unauthorised' }); + } + + + + try { + const decodedToken = jwt.verify(token, config.server.token.secret ); + const decodeTokenToString = decodedToken; + const tokenString = JSON.stringify(decodeTokenToString); + const JSobj = JSON.parse(tokenString).user_id; + + if(JSobj == user_id){ + + try{ + const query = {_id : new mongoose.Types.ObjectId(_id)}; + let comment = await Comment.findOne(query); + if(comment == null){ + + }else{ + for (var val of comment.users_liked) { + if(val == user_id){ + console.log("Already Liked") + const responseDataTrue = { + likedState:"true", + } + const jsonContent = JSON.stringify(responseDataTrue); + return res.status(200).send(jsonContent) + } + } + console.log("Not Liked") + const responseDataFalse = { + likedState:"false", + } + const jsonContent = JSON.stringify(responseDataFalse); + return res.status(200).send(jsonContent) + + }} + catch{ + Logging.error("Incorrect") + return res.status(400).send({message: 'Bad Request'}) + } + + } + else{ + return res.status(401).send({ message: 'Unauthorised' }); + } + + + } catch (error) { + return res.status(400).send({ message: 'Bad Request' }); + } + + + +} + + + +export default { createComment, readAllComments, toggleLike, likedState} diff --git a/backend-services/comment-service/src/routes/Comment.ts b/backend-services/comment-service/src/routes/Comment.ts index e681275b4c6fdb7aae3c4128599c10dfa3a82c01..9c7889633b9c02ff4ce5c1d92f99d99acc4bd0f3 100644 --- a/backend-services/comment-service/src/routes/Comment.ts +++ b/backend-services/comment-service/src/routes/Comment.ts @@ -6,7 +6,8 @@ const router = express.Router(); router.post('/create', controller.createComment); router.put('/toggle-like', controller.toggleLike); +router.get('/liked-state', controller.likedState); router.get('/:post_id', controller.readAllComments); -router.get('/', controller.readAllComments); +//router.get('/', controller.readAllComments); export = router;