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

reading comments from specifc post cap 50

parent 14c2ad98
No related branches found
No related tags found
1 merge request!13Create Comment service requirements
...@@ -2,40 +2,41 @@ import { NextFunction, Request, Response } from "express"; ...@@ -2,40 +2,41 @@ import { NextFunction, Request, Response } from "express";
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import Comment from "../models/Comment"; 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({ const comment = new Comment({
_id: new mongoose.Types.ObjectId(), _id: new mongoose.Types.ObjectId(),
post_id,
user_id,
body body
}); });
return comment try {
.save() const comment_1 = await comment
.then((comment) => res.status(201).json({ comment })) .save();
.catch((error) => res.status(500).json({ error })); return res.status(201).json({ comment_1 });
} catch (error) {
return res.status(500).json({ error });
}
}; };
const readAllComments = async (req: Request, res: Response, next: NextFunction) => {
const id = req.params.post_id;
/* console.log(id)
const readComment = (req: Request, res: Response, next: NextFunction) => { try {
const commentId = req.params.commentId; const query = {post_id : new mongoose.Types.ObjectId(id)}
const comments = await Comment.find(query).limit(50)
return Comment.findById(commentId) //const comments: object[] = await Comment.find({}, { _id: 1 }).toArray()
.then((comment) => ( comment ? res.status(200).json({ comment }) : res.status(404).json({ message: ' Not Found'}))) .select('-__v');
.catch((error) => res.status(500).json({ error })); return res.status(200).json({ comments });
} catch (error) {
} return 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 }));
} }
......
import { number } from 'joi';
import mongoose, {Document, Schema} from 'mongoose'; import mongoose, {Document, Schema} from 'mongoose';
export interface IComment { export interface IComment {
user_id: number; //Commented section used for testing
post_id: number; user_id: mongoose.Schema.Types.ObjectId; //number;
post_id: mongoose.Schema.Types.ObjectId; //number;
body: string; body: string;
users_liked: [Number];
} }
export interface ICommentModel extends IComment, Document{} export interface ICommentModel extends IComment, Document{}
const CommentSchema: Schema = new Schema( const CommentSchema: Schema = new Schema(
{ //Commented Out for testing {
//user_id:{ type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true}, user_id:{ type: mongoose.Schema.Types.ObjectId,required: true, ref: 'User', },
//post_id:{ type: mongoose.Schema.Types.ObjectId, ref: 'Post', required: true}, post_id:{ type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Post'},
body: { type: String, required: true }, body: { type: String, required: true },
//Need to look at how to do this?
users_liked: { type : [Number]}
}, },
{ {
timestamps: true, timestamps: true,
versionkey: false
} }
) )
......
...@@ -4,7 +4,7 @@ import controller from '../controllers/Comment'; ...@@ -4,7 +4,7 @@ import controller from '../controllers/Comment';
const router = express.Router(); const router = express.Router();
router.post('/create', controller.createComment); router.post('/create', controller.createComment);
router.get('/get/', controller.readAllComments); router.get('/get/:post_id', controller.readAllComments);
export = router; export = router;
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