diff --git a/comment-service/src/controllers/Comment.ts b/comment-service/src/controllers/Comment.ts index 9023f6070475185efa50eec3da38c6017e60ddd3..b37cbfe6737086c3b1297eb8f5f7e56dd0a627ff 100644 --- a/comment-service/src/controllers/Comment.ts +++ b/comment-service/src/controllers/Comment.ts @@ -73,7 +73,7 @@ const createComment = async ( req: Request, res: Response, next: NextFunction,) return res.status(201).json({ comment_1 }); } else{ - return res.status(401).send({ message: 'Unauthorized' }); + return res.status(401).send({ message: 'Unauthorised' }); } @@ -90,10 +90,27 @@ const readAllComments = async (req: Request, res: Response, next: NextFunction) //401 //403 const id = req.params.post_id; + if (id == null || 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 ); + } + catch{ + return res.status(401).send({ message: 'Unauthorised' }); + } + + try{ const posts = {post_id : new mongoose.Types.ObjectId(id)} const post_exist = await Comment.countDocuments(posts, { limit: 1 }) if(post_exist == 0) { @@ -107,16 +124,13 @@ const readAllComments = async (req: Request, res: Response, next: NextFunction) try { - - - const query = {post_id : new mongoose.Types.ObjectId(id)} const comments = await Comment.find(query).limit(50) //const comments: object[] = await Comment.find({}, { _id: 1 }).toArray() .select('-__v'); return res.status(200).json({ comments }); } catch (error) { - return res.status(500).json({ error }); + return res.status(500).send({ message: 'Internal server errors' }); } } diff --git a/comment-service/src/routes/Comment.ts b/comment-service/src/routes/Comment.ts index 78d6da2eb62be676bf9e1b5b4c867b4ba68b0114..4078f4ca92e2a12e7a910f32c9067e8b40a1d10c 100644 --- a/comment-service/src/routes/Comment.ts +++ b/comment-service/src/routes/Comment.ts @@ -5,6 +5,6 @@ const router = express.Router(); router.post('/create', controller.createComment); router.get('/:post_id', controller.readAllComments); - +router.get('/', controller.readAllComments); export = router; diff --git a/comment-service/src/server.ts b/comment-service/src/server.ts index 89b815b9570a15cf9f70c45767358922cfab00fa..57ff1734e52ce6ac32347c5b7c85026947a0257a 100644 --- a/comment-service/src/server.ts +++ b/comment-service/src/server.ts @@ -65,6 +65,7 @@ const StartServer = () => { message: error.message }); }); + http.createServer(router).listen(config.server.port, () => Logging.info(`Server is running on port ${config.server.port}`));