diff --git a/comment-service/src/controllers/Comment.ts b/comment-service/src/controllers/Comment.ts index e28e27341108b09facb122cdc85715284953a4e3..9023f6070475185efa50eec3da38c6017e60ddd3 100644 --- a/comment-service/src/controllers/Comment.ts +++ b/comment-service/src/controllers/Comment.ts @@ -78,16 +78,38 @@ const createComment = async ( req: Request, res: Response, next: NextFunction,) } catch (error) { - return res.status(400).json({ message: 'Bad Request' }); + return res.status(400).send({ message: 'Bad Request' }); } }; const readAllComments = async (req: Request, res: Response, next: NextFunction) => { + + //400 + //401 + //403 const id = req.params.post_id; - console.log(id) + + + try{ + + const posts = {post_id : new mongoose.Types.ObjectId(id)} + const post_exist = await Comment.countDocuments(posts, { limit: 1 }) + if(post_exist == 0) { + return res.status(404).send({message: 'Specified Post Not Found.'}); + } + } + catch{ + return res.status(404).send({message: 'Specified Post Not Found.'}); + + } + + 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() diff --git a/comment-service/src/routes/Comment.ts b/comment-service/src/routes/Comment.ts index e05c88d68ee9ad5b9ecf00ab3db33b347edf397e..78d6da2eb62be676bf9e1b5b4c867b4ba68b0114 100644 --- a/comment-service/src/routes/Comment.ts +++ b/comment-service/src/routes/Comment.ts @@ -4,7 +4,7 @@ import controller from '../controllers/Comment'; const router = express.Router(); router.post('/create', controller.createComment); -router.get('/get/:post_id', controller.readAllComments); +router.get('/:post_id', controller.readAllComments); export = router;