diff --git a/controller/getComments.js b/controller/getComments.js new file mode 100644 index 0000000..3dc6b53 --- /dev/null +++ b/controller/getComments.js @@ -0,0 +1,24 @@ +const Blog = require('../models/blog') + +getCommments = async (req, res) => { + // gets the id off the request + let blogId = req.params.id; + + // use it to find the related blog post + Blog.findById(blogId, function(err, blog) { + if (err) { + return res.status(400).json({success: false, error: err}); + } + if (!blog.length) { + return res + .status(404) + .json({success: false, error: 'Blog not found'}) + } + // if blog is found by id , returns the comments and blog as json + else{ + return res.status(200).json({success: true, data: blog}) + } + }) .catch(err => console.log(err)); +} + +module.exports = { getCommments } \ No newline at end of file diff --git a/controller/postComments.js b/controller/postComments.js new file mode 100644 index 0000000..a437ade --- /dev/null +++ b/controller/postComments.js @@ -0,0 +1,28 @@ +const blog = require('../models/blog'); +const Comment = require('../models/comment'); + + +const postComment = async (req, res) => { + // grabs the id for the post which you want to creat a new comment + const id = req.params.id; + const comment = new Comment({ + text: req.body.comment, + post: id + }) + + await comment.save(); + const relatedPost = await blog.findById(id); + // pushes the comment for the blog you want to add it to + relatedPost.comment.push(comment); + await relatedPost.save(function(err) { + if (err) return res.status(400).json({success: false, error: err}); + + // returns the pushed comment with its related blog post + else{ + return res.status(200).json({success: true, data: blog}) + } + + }) +} + +module.exports = { postComment } \ No newline at end of file diff --git a/index.js b/index.js index 7e9628e..a74f948 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,17 @@ const express = require('express') -const cors = require('cors') +const GetBlogs = require('../controller/getBlogs') +const PostBlog = require('../controller/postBlog') -const db = require ('./db') -const router = require('./router') +const GetComments = require('../controller/getComments') +const postComments = require('../controller/postComments') -const app = express() -const port = 3001 +const router = express.Router() -app.use(express.urlencoded({ extended: true })) -app.use(cors()) -app.use(express.json()) +router.get('/blog/:id', GetBlogs.getBlogs) +router.post('/blog/post', PostBlog.postBlog) -db.on('error', console.error.bind(console, 'MongoDB connection failed...')) +// setting up the endpoints to use for getComments & postComments +router.get('blog/post/:id/comment', GetComments.getCommments) +router.post('blog/post/:id/comment', postComments.postComment) -app.use('/api', router) - -app.listen(port, () => console.log(`Server running on ${port}`)) \ No newline at end of file +module.exports = router \ No newline at end of file diff --git a/models/comment.js b/models/comment.js new file mode 100644 index 0000000..e2fbd74 --- /dev/null +++ b/models/comment.js @@ -0,0 +1,23 @@ +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; + +// creating new Comment schema for requiring text and adding date & its post ref + +const Comment = new Schema ({ + text: { + type: String, + trim: true, + required: true + }, + date: { + type: Date, + default: Date.now + }, + post: { + type: mongoose.Schema.Types.ObjectId, + ref: 'Post' + } + +}) + +module.exports = mongoose.model('comment', Comment) \ No newline at end of file