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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.env
39 changes: 8 additions & 31 deletions controller/PostBlog.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
const Blog = require('../models/blog')

postBlog = (req, res) => {
const body = req.body

if (!body) {
return res.status(400).json({
success: false,
error: 'You must provide a blog',
})
}

const blog = new Blog(body)

if (!blog) {
return res.status(400).json({ success: false, error: err })
}

blog
.save()
.then(() => {
return res.status(201).json({
success: true,
id: blog._id,
message: 'Blog post created!',
})
})
.catch(error => {
return res.status(400).json({
error,
message: 'Blog post not created!',
})
})
// ENDPOINT: /api/blog/post
const postBlog = async (req, res) => {
const blog = await Blog.create(req.body);

res.status(201).json({
success: true,
data: blog
})
}

module.exports = { postBlog }
21 changes: 21 additions & 0 deletions controller/getBlog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Blog = require('../models/blog')

// Original given function returned all blogs
// ENDPOINT: /api/blog/:id
const getBlog = async (req, res) => {
const blog = await Blog.findById(req.params.id)
.exec()
.then(
(blog) => {
return res.status(200).json({
success: true,
data: blog
});
},
(error) => {
console.log(error);
}
);
}

module.exports = { getBlog }
3 changes: 2 additions & 1 deletion controller/getBlogs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Blog = require('../models/blog')

getBlogs = async (req, res) => {
// ENDPOINT: /api/blog
const getBlogs = async (req, res) => {
await Blog.find({}, (err, blog) => {
if (err) {
return res.status(400).json({ success: false, error: err })
Expand Down
20 changes: 20 additions & 0 deletions controller/getComments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Comment = require('../models/comment')

// ENDPOINT: GET /api/blog/:id/comments (id = blog ID)

const getComments = async (req, res) => {
if (req.params.id) {
const comments = await Comment.find({blog: req.params.id})
return res.status(200).json({
success: true,
data: comments
})
} else {
res.status(200).json({
success: true,
data: `No comments found.`
})
}
}

module.exports = { getComments }
20 changes: 20 additions & 0 deletions controller/postComment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Blog = require('../models/blog')
const Comment = require('../models/comment')

// ENDPOINT: POST /api/blog/:id/comments (id = blog ID)
const createComment = async (req, res) => {
// Attach blog id to request body
req.body.blog = req.params.id

// Ensure blog exists
const blog = await Blog.findById(req.params.id)

const comment = await Comment.create(req.body)

res.status(201).json({
success: true,
data: comment
})
}

module.exports = { createComment }
13 changes: 5 additions & 8 deletions db/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
const mongoose = require('mongoose')

mongoose
.connect('mongodb://127.0.0.1:27017/blog', { useNewUrlParser: true })
.catch(e => {
console.error('Connection error', e.message)
})
const connectDB = async () => {
const con = await mongoose.connect(process.env.DB_URI, { useNewUrlParser: true, useUnifiedTopology: true})
console.log('DB Connected')
}

const db = mongoose.connection

module.exports = db
module.exports = connectDB
24 changes: 20 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
// NPM Packages
const express = require('express')
const dotenv = require("dotenv");
const cors = require('cors')

const db = require ('./db')
const router = require('./router')
// DB Connection
const connectDB = require ('./db')

// Import Routes
const blogRouter = require('./routes/blogRoutes')


// ENV Config
dotenv.config({ path: ".env" });

// Express Config
const app = express()

const port = 3001

app.use(express.urlencoded({ extended: true }))

app.use(cors())

app.use(express.json())

db.on('error', console.error.bind(console, 'MongoDB connection failed...'))
// Use DB Connection
connectDB()

app.use('/api', router)
// Use Routes
app.use('/api', blogRouter)

// Run App
app.listen(port, () => console.log(`Server running on ${port}`))
17 changes: 17 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongoose = require('mongoose');

const Comment = new mongoose.Schema({
// Comment Body
body: {
type: String,
required: true
},
// Associated Blog
blog: {
type: mongoose.Schema.ObjectId,
ref: "blog",
required: true
}
})

module.exports = mongoose.model('Comment', Comment)
Loading