diff --git a/controller/GetComments.js b/controller/GetComments.js new file mode 100644 index 0000000..eb3dbd6 --- /dev/null +++ b/controller/GetComments.js @@ -0,0 +1,18 @@ +const Blog = require('../models/blog') + +getComments = async (req, res) => { + var id = req.params.id; + await Blog.find({"_id": id}, {comments: 1, _id: 0}, (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` }) + } + return res.status(200).json({ success: true, data: blog }) + }).catch(err => console.log(err)) +} + +module.exports = { getComments } \ No newline at end of file diff --git a/controller/PostBlog.js b/controller/PostBlog.js index 6f5ef3e..e83e211 100644 --- a/controller/PostBlog.js +++ b/controller/PostBlog.js @@ -2,6 +2,7 @@ const Blog = require('../models/blog') postBlog = (req, res) => { const body = req.body + console.log(body); if (!body) { return res.status(400).json({ @@ -11,6 +12,7 @@ postBlog = (req, res) => { } const blog = new Blog(body) + console.log(blog); if (!blog) { return res.status(400).json({ success: false, error: err }) diff --git a/controller/PostComment.js b/controller/PostComment.js new file mode 100644 index 0000000..f20915f --- /dev/null +++ b/controller/PostComment.js @@ -0,0 +1,48 @@ +const Comment = require('../models/Comment') +const Blog = require('../models/blog') + +postComment = async(req, res) => { + const body = req.body + if (body.author == undefined || body.text == undefined) { + return res.status(400).json({ + success: false, + error: 'You must provide a comment', + }) + } + + var id = req.params.id; //_id param of blog + + //---- Creates a new database just for comments ----\\ + + //uncomment line 18, 23, and line 7 in ./models/Comment.js to put comments in their own database + //body.Blogid = id; + const comment = new Comment(body) + if (!comment) { + return res.status(400).json({ success: false, error: err }) + } + //await comment.save() + + //---- Creates a new database just for comments ----\\ + + + + //------ Checks if blog exists ------\\ + + await Blog.find({"_id": id}, (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` }) + } + return res.status(200).json({ success: true, data: comment }) + }).catch(err => console.log(err)) + + //------ Checks if blog exists ------\\ + + if(res.statusCode == 200) { await Blog.findOneAndUpdate({"_id": id}, {$push: {"comments": comment}}); } //Pushes comment into array if blog exists +} + +module.exports = { postComment } \ No newline at end of file diff --git a/controller/getBlogs.js b/controller/getBlogs.js index 1cef141..6edfb6d 100644 --- a/controller/getBlogs.js +++ b/controller/getBlogs.js @@ -1,7 +1,9 @@ const Blog = require('../models/blog') getBlogs = async (req, res) => { - await Blog.find({}, (err, blog) => { + var id = req.params.id; + console.log(id); + await Blog.find({"_id": id}, {comments: 0}, (err, blog) => { if (err) { return res.status(400).json({ success: false, error: err }) } diff --git a/index.js b/index.js index 7e9628e..bff4d8d 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ const express = require('express') const cors = require('cors') const db = require ('./db') -const router = require('./router') +const router = require('./routes') const app = express() const port = 3001 diff --git a/models/Comment.js b/models/Comment.js new file mode 100644 index 0000000..205c823 --- /dev/null +++ b/models/Comment.js @@ -0,0 +1,10 @@ +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; + +const Comment = new Schema({ + author: { type: String, required: true }, + text: { type: String, required: true }, + //Blogid: { type: String, required: true } +}) + +module.exports = mongoose.model('comment', Comment) \ No newline at end of file diff --git a/models/blog.js b/models/blog.js index 530d4d4..b0de0e5 100644 --- a/models/blog.js +++ b/models/blog.js @@ -2,7 +2,10 @@ const mongoose = require('mongoose'); const Schema = mongoose.Schema; const Blog = new Schema({ - id: { type: String, required: true } + author: { type: String, required: true }, + title: { type: String, required: true }, + text: { type: String, required: true }, + comments: { type: Array, required: false } }) module.exports = mongoose.model('blog', Blog) \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index 4858665..2d8df99 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,10 +1,14 @@ const express = require('express') -const GetBlogs = require('./controller/getBlogs') -const PostBlog = require('./controller/postBlog') +const GetBlogs = require('../controller/getBlogs') +const PostBlog = require('../controller/PostBlog') +const GetComments = require('../controller/GetComments') +const PostComment = require('../controller/PostComment') const router = express.Router() -router.get('/blog/:id', GetBlogs) -router.post('/blog/post', PostBlog) +router.get('/blog/:id', GetBlogs.getBlogs) +router.post('/blog/post', PostBlog.postBlog) +router.get('/blog/:id/comment', GetComments.getComments) +router.post('/blog/:id/comment', PostComment.postComment) module.exports = router \ No newline at end of file