From 6c89a09cbb1b15267de1d7750cd072be2ba4adcf Mon Sep 17 00:00:00 2001
From: pt00371 <pt00371@surrey.ac.uk>
Date: Fri, 31 Mar 2023 09:06:37 +0100
Subject: [PATCH] all response done for get request apart from 403

---
 comment-service/src/controllers/Comment.ts | 24 +++++++++++++++++-----
 comment-service/src/routes/Comment.ts      |  2 +-
 comment-service/src/server.ts              |  1 +
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/comment-service/src/controllers/Comment.ts b/comment-service/src/controllers/Comment.ts
index 9023f607..b37cbfe6 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 78d6da2e..4078f4ca 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 89b815b9..57ff1734 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}`));
 
-- 
GitLab