Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions controller/getComments.js
Original file line number Diff line number Diff line change
@@ -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 }
28 changes: 28 additions & 0 deletions controller/postComments.js
Original file line number Diff line number Diff line change
@@ -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 }
23 changes: 11 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -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}`))
module.exports = router
23 changes: 23 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -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)