Skip to content
Snippets Groups Projects
Commit 6c89a09c authored by Talty-Kerr, Patrick (UG - Comp Sci & Elec Eng)'s avatar Talty-Kerr, Patrick (UG - Comp Sci & Elec Eng)
Browse files

all response done for get request apart from 403

parent 6d05e389
No related branches found
No related tags found
1 merge request!13Create Comment service requirements
...@@ -73,7 +73,7 @@ const createComment = async ( req: Request, res: Response, next: NextFunction,) ...@@ -73,7 +73,7 @@ const createComment = async ( req: Request, res: Response, next: NextFunction,)
return res.status(201).json({ comment_1 }); return res.status(201).json({ comment_1 });
} }
else{ 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) ...@@ -90,10 +90,27 @@ const readAllComments = async (req: Request, res: Response, next: NextFunction)
//401 //401
//403 //403
const id = req.params.post_id; 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{ 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 posts = {post_id : new mongoose.Types.ObjectId(id)}
const post_exist = await Comment.countDocuments(posts, { limit: 1 }) const post_exist = await Comment.countDocuments(posts, { limit: 1 })
if(post_exist == 0) { if(post_exist == 0) {
...@@ -107,16 +124,13 @@ const readAllComments = async (req: Request, res: Response, next: NextFunction) ...@@ -107,16 +124,13 @@ const readAllComments = async (req: Request, res: Response, next: NextFunction)
try { try {
const query = {post_id : new mongoose.Types.ObjectId(id)} const query = {post_id : new mongoose.Types.ObjectId(id)}
const comments = await Comment.find(query).limit(50) const comments = await Comment.find(query).limit(50)
//const comments: object[] = await Comment.find({}, { _id: 1 }).toArray() //const comments: object[] = await Comment.find({}, { _id: 1 }).toArray()
.select('-__v'); .select('-__v');
return res.status(200).json({ comments }); return res.status(200).json({ comments });
} catch (error) { } catch (error) {
return res.status(500).json({ error }); return res.status(500).send({ message: 'Internal server errors' });
} }
} }
......
...@@ -5,6 +5,6 @@ const router = express.Router(); ...@@ -5,6 +5,6 @@ const router = express.Router();
router.post('/create', controller.createComment); router.post('/create', controller.createComment);
router.get('/:post_id', controller.readAllComments); router.get('/:post_id', controller.readAllComments);
router.get('/', controller.readAllComments);
export = router; export = router;
...@@ -65,6 +65,7 @@ const StartServer = () => { ...@@ -65,6 +65,7 @@ const StartServer = () => {
message: error.message message: error.message
}); });
}); });
http.createServer(router).listen(config.server.port, () => Logging.info(`Server is running on port ${config.server.port}`)); http.createServer(router).listen(config.server.port, () => Logging.info(`Server is running on port ${config.server.port}`));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment