From 4494269965040f469203c568755b19d3b0aa2b4c Mon Sep 17 00:00:00 2001 From: Tiernan330 <83978295+Tiernan330@users.noreply.github.com> Date: Fri, 30 Sep 2022 15:59:10 -0400 Subject: [PATCH 1/4] Add files via upload --- controller/GetComments.js | 18 +++++++++++++++ controller/PostBlog.js | 2 ++ controller/PostComment.js | 48 +++++++++++++++++++++++++++++++++++++++ controller/getBlogs.js | 4 +++- models/Comment.js | 10 ++++++++ models/blog.js | 5 +++- routes/index.js | 12 ++++++---- 7 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 controller/GetComments.js create mode 100644 controller/PostComment.js create mode 100644 models/Comment.js 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/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 From 16b817dac7cd5a634a360a3367b758b365a494a7 Mon Sep 17 00:00:00 2001 From: Tiernan330 <83978295+Tiernan330@users.noreply.github.com> Date: Fri, 30 Sep 2022 16:00:36 -0400 Subject: [PATCH 2/4] Add files via upload --- getBlogs.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 getBlogs.js diff --git a/getBlogs.js b/getBlogs.js new file mode 100644 index 0000000..6edfb6d --- /dev/null +++ b/getBlogs.js @@ -0,0 +1,19 @@ +const Blog = require('../models/blog') + +getBlogs = async (req, res) => { + 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 }) + } + 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 = { getBlogs } \ No newline at end of file From 4b1ff65fa97b42321060cc7eecd5843c7d031f49 Mon Sep 17 00:00:00 2001 From: Tiernan330 <83978295+Tiernan330@users.noreply.github.com> Date: Fri, 30 Sep 2022 16:03:50 -0400 Subject: [PATCH 3/4] Delete getBlogs.js --- getBlogs.js | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 getBlogs.js diff --git a/getBlogs.js b/getBlogs.js deleted file mode 100644 index 6edfb6d..0000000 --- a/getBlogs.js +++ /dev/null @@ -1,19 +0,0 @@ -const Blog = require('../models/blog') - -getBlogs = async (req, res) => { - 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 }) - } - 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 = { getBlogs } \ No newline at end of file From 69ac3c2a654e43b772f783a29107983b939e05fc Mon Sep 17 00:00:00 2001 From: Tiernan330 <83978295+Tiernan330@users.noreply.github.com> Date: Fri, 30 Sep 2022 16:05:03 -0400 Subject: [PATCH 4/4] Add files via upload --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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