diff --git a/comment-service/src/controllers/Comment.ts b/comment-service/src/controllers/Comment.ts index 686076ef09fd3b7e27fc0e32df1fccffec50ad0e..645187a6dbc4d8ad422de50db79a6f4ef5fdcb81 100644 --- a/comment-service/src/controllers/Comment.ts +++ b/comment-service/src/controllers/Comment.ts @@ -2,40 +2,41 @@ import { NextFunction, Request, Response } from "express"; import mongoose from 'mongoose'; import Comment from "../models/Comment"; -const createComment = (req: Request, res: Response, next: NextFunction) => { - const { body } = req.body; + + +const createComment = async ( req: Request, res: Response, next: NextFunction,) => { + const { post_id, user_id, body} = req.body; const comment = new Comment({ _id: new mongoose.Types.ObjectId(), + post_id, + user_id, body }); - return comment - .save() - .then((comment) => res.status(201).json({ comment })) - .catch((error) => res.status(500).json({ error })); + try { + const comment_1 = await comment + .save(); + return res.status(201).json({ comment_1 }); + } catch (error) { + return res.status(500).json({ error }); + } }; - - -/* -const readComment = (req: Request, res: Response, next: NextFunction) => { - const commentId = req.params.commentId; - - return Comment.findById(commentId) - .then((comment) => ( comment ? res.status(200).json({ comment }) : res.status(404).json({ message: ' Not Found'}))) - .catch((error) => res.status(500).json({ error })); - -} -*/ - -const readAllComments = (req: Request, res: Response, next: NextFunction) => { - - return Comment.find() - .then((comments) => res.status(200).json({ comments })) - .catch((error) => res.status(500).json({ error })); +const readAllComments = async (req: Request, res: Response, next: NextFunction) => { + const id = req.params.post_id; + console.log(id) + 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 }); + } } diff --git a/comment-service/src/models/Comment.ts b/comment-service/src/models/Comment.ts index 068734b9b750dd65abdeb082c3eccaf400f03d0e..07690121813f182be3841539b49a0a58adbf3248 100644 --- a/comment-service/src/models/Comment.ts +++ b/comment-service/src/models/Comment.ts @@ -1,21 +1,29 @@ +import { number } from 'joi'; import mongoose, {Document, Schema} from 'mongoose'; export interface IComment { - user_id: number; - post_id: number; + //Commented section used for testing + user_id: mongoose.Schema.Types.ObjectId; //number; + post_id: mongoose.Schema.Types.ObjectId; //number; body: string; + users_liked: [Number]; + } export interface ICommentModel extends IComment, Document{} const CommentSchema: Schema = new Schema( - { //Commented Out for testing - //user_id:{ type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true}, - //post_id:{ type: mongoose.Schema.Types.ObjectId, ref: 'Post', required: true}, + { + user_id:{ type: mongoose.Schema.Types.ObjectId,required: true, ref: 'User', }, + post_id:{ type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Post'}, body: { type: String, required: true }, + //Need to look at how to do this? + users_liked: { type : [Number]} + }, { timestamps: true, + versionkey: false } ) diff --git a/comment-service/src/routes/Comment.ts b/comment-service/src/routes/Comment.ts index f40982819736875d02f871f8d4ceabe490583e19..e05c88d68ee9ad5b9ecf00ab3db33b347edf397e 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/', controller.readAllComments); +router.get('/get/:post_id', controller.readAllComments); export = router;