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";
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 });
}
}
......
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
}
)
......
......@@ -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;
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