From 5567767d972e5c30ac23f16251d465dedf175d7a Mon Sep 17 00:00:00 2001 From: yashrajverma Date: Wed, 19 May 2021 15:48:57 +0530 Subject: [PATCH] Fixed! --- .history/controllers/blog_20210519114159.js | 103 ------------------- .history/controllers/blog_20210519145955.js | 104 ------------------- .history/controllers/blog_20210519150616.js | 104 ------------------- .history/controllers/blog_20210519151427.js | 104 ------------------- .history/controllers/blog_20210519151454.js | 104 ------------------- .history/controllers/blog_20210519151725.js | 106 -------------------- .history/controllers/blog_20210519151735.js | 106 -------------------- .history/controllers/blog_20210519151822.js | 106 -------------------- .history/controllers/blog_20210519151942.js | 104 ------------------- .history/controllers/blog_20210519152021.js | 104 ------------------- .history/controllers/blog_20210519152037.js | 104 ------------------- .history/controllers/blog_20210519152116.js | 104 ------------------- .history/controllers/blog_20210519152132.js | 104 ------------------- .history/controllers/blog_20210519152207.js | 104 ------------------- .history/controllers/blog_20210519152224.js | 104 ------------------- .history/controllers/blog_20210519152240.js | 104 ------------------- .history/controllers/blog_20210519152310.js | 104 ------------------- .history/controllers/blog_20210519152325.js | 104 ------------------- .history/controllers/blog_20210519152415.js | 104 ------------------- .history/controllers/blog_20210519152419.js | 104 ------------------- .history/controllers/blog_20210519152443.js | 104 ------------------- .history/controllers/blog_20210519152758.js | 104 ------------------- .history/controllers/blog_20210519152820.js | 105 ------------------- .history/controllers/blog_20210519152920.js | 105 ------------------- .history/controllers/blog_20210519153031.js | 105 ------------------- .history/controllers/blog_20210519153142.js | 105 ------------------- .history/controllers/blog_20210519153454.js | 105 ------------------- .history/controllers/blog_20210519153459.js | 105 ------------------- .history/controllers/blog_20210519153836.js | 105 ------------------- .history/controllers/blog_20210519153855.js | 105 ------------------- .history/controllers/blog_20210519153953.js | 106 -------------------- .history/controllers/blog_20210519154014.js | 106 -------------------- .history/controllers/blog_20210519154059.js | 106 -------------------- .history/controllers/blog_20210519154126.js | 105 ------------------- .history/controllers/blog_20210519154454.js | 106 -------------------- .history/controllers/blog_20210519154456.js | 106 -------------------- .history/controllers/blog_20210519154625.js | 106 -------------------- .history/controllers/blog_20210519154733.js | 106 -------------------- .history/controllers/blog_20210519154825.js | 104 ------------------- .history/models/Blog_20210519103433.js | 27 ----- .history/models/Blog_20210519150611.js | 28 ------ .history/models/Blog_20210519150818.js | 25 ----- .history/models/Blog_20210519152942.js | 25 ----- .history/models/Blog_20210519153800.js | 25 ----- .history/routes/blog_20210519114750.js | 14 --- .history/routes/blog_20210519151054.js | 14 --- 46 files changed, 4242 deletions(-) delete mode 100644 .history/controllers/blog_20210519114159.js delete mode 100644 .history/controllers/blog_20210519145955.js delete mode 100644 .history/controllers/blog_20210519150616.js delete mode 100644 .history/controllers/blog_20210519151427.js delete mode 100644 .history/controllers/blog_20210519151454.js delete mode 100644 .history/controllers/blog_20210519151725.js delete mode 100644 .history/controllers/blog_20210519151735.js delete mode 100644 .history/controllers/blog_20210519151822.js delete mode 100644 .history/controllers/blog_20210519151942.js delete mode 100644 .history/controllers/blog_20210519152021.js delete mode 100644 .history/controllers/blog_20210519152037.js delete mode 100644 .history/controllers/blog_20210519152116.js delete mode 100644 .history/controllers/blog_20210519152132.js delete mode 100644 .history/controllers/blog_20210519152207.js delete mode 100644 .history/controllers/blog_20210519152224.js delete mode 100644 .history/controllers/blog_20210519152240.js delete mode 100644 .history/controllers/blog_20210519152310.js delete mode 100644 .history/controllers/blog_20210519152325.js delete mode 100644 .history/controllers/blog_20210519152415.js delete mode 100644 .history/controllers/blog_20210519152419.js delete mode 100644 .history/controllers/blog_20210519152443.js delete mode 100644 .history/controllers/blog_20210519152758.js delete mode 100644 .history/controllers/blog_20210519152820.js delete mode 100644 .history/controllers/blog_20210519152920.js delete mode 100644 .history/controllers/blog_20210519153031.js delete mode 100644 .history/controllers/blog_20210519153142.js delete mode 100644 .history/controllers/blog_20210519153454.js delete mode 100644 .history/controllers/blog_20210519153459.js delete mode 100644 .history/controllers/blog_20210519153836.js delete mode 100644 .history/controllers/blog_20210519153855.js delete mode 100644 .history/controllers/blog_20210519153953.js delete mode 100644 .history/controllers/blog_20210519154014.js delete mode 100644 .history/controllers/blog_20210519154059.js delete mode 100644 .history/controllers/blog_20210519154126.js delete mode 100644 .history/controllers/blog_20210519154454.js delete mode 100644 .history/controllers/blog_20210519154456.js delete mode 100644 .history/controllers/blog_20210519154625.js delete mode 100644 .history/controllers/blog_20210519154733.js delete mode 100644 .history/controllers/blog_20210519154825.js delete mode 100644 .history/models/Blog_20210519103433.js delete mode 100644 .history/models/Blog_20210519150611.js delete mode 100644 .history/models/Blog_20210519150818.js delete mode 100644 .history/models/Blog_20210519152942.js delete mode 100644 .history/models/Blog_20210519153800.js delete mode 100644 .history/routes/blog_20210519114750.js delete mode 100644 .history/routes/blog_20210519151054.js diff --git a/.history/controllers/blog_20210519114159.js b/.history/controllers/blog_20210519114159.js deleted file mode 100644 index f6ea6c1..0000000 --- a/.history/controllers/blog_20210519114159.js +++ /dev/null @@ -1,103 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519145955.js b/.history/controllers/blog_20210519145955.js deleted file mode 100644 index 16d1963..0000000 --- a/.history/controllers/blog_20210519145955.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: course._id, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519150616.js b/.history/controllers/blog_20210519150616.js deleted file mode 100644 index 16d1963..0000000 --- a/.history/controllers/blog_20210519150616.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: course._id, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519151427.js b/.history/controllers/blog_20210519151427.js deleted file mode 100644 index b77e01b..0000000 --- a/.history/controllers/blog_20210519151427.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: course, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519151454.js b/.history/controllers/blog_20210519151454.js deleted file mode 100644 index 16d1963..0000000 --- a/.history/controllers/blog_20210519151454.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: course._id, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519151725.js b/.history/controllers/blog_20210519151725.js deleted file mode 100644 index 41e17ac..0000000 --- a/.history/controllers/blog_20210519151725.js +++ /dev/null @@ -1,106 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }) - .populate("_id ") - .select("-password"); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: course._id, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519151735.js b/.history/controllers/blog_20210519151735.js deleted file mode 100644 index bb6bf06..0000000 --- a/.history/controllers/blog_20210519151735.js +++ /dev/null @@ -1,106 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }) - .populate("_id ") - .select("-password"); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: course, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519151822.js b/.history/controllers/blog_20210519151822.js deleted file mode 100644 index 41e17ac..0000000 --- a/.history/controllers/blog_20210519151822.js +++ /dev/null @@ -1,106 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }) - .populate("_id ") - .select("-password"); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: course._id, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519151942.js b/.history/controllers/blog_20210519151942.js deleted file mode 100644 index 6e3a814..0000000 --- a/.history/controllers/blog_20210519151942.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }).populate("").select("-password"); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: course, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152021.js b/.history/controllers/blog_20210519152021.js deleted file mode 100644 index f3614e1..0000000 --- a/.history/controllers/blog_20210519152021.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: , - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152037.js b/.history/controllers/blog_20210519152037.js deleted file mode 100644 index ecb3602..0000000 --- a/.history/controllers/blog_20210519152037.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }); - console.log(course); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152116.js b/.history/controllers/blog_20210519152116.js deleted file mode 100644 index e57c2c9..0000000 --- a/.history/controllers/blog_20210519152116.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find(_id); - console.log(course); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152132.js b/.history/controllers/blog_20210519152132.js deleted file mode 100644 index ecb3602..0000000 --- a/.history/controllers/blog_20210519152132.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }); - console.log(course); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152207.js b/.history/controllers/blog_20210519152207.js deleted file mode 100644 index 8d93c09..0000000 --- a/.history/controllers/blog_20210519152207.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.findById({ _id }); - console.log(course); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152224.js b/.history/controllers/blog_20210519152224.js deleted file mode 100644 index eb2bed9..0000000 --- a/.history/controllers/blog_20210519152224.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.findById({ _id }); - console.log("Course ", course); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152240.js b/.history/controllers/blog_20210519152240.js deleted file mode 100644 index 3fcbb8d..0000000 --- a/.history/controllers/blog_20210519152240.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.findById(_id); - console.log("Course ", course); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152310.js b/.history/controllers/blog_20210519152310.js deleted file mode 100644 index 16ff14d..0000000 --- a/.history/controllers/blog_20210519152310.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find(_id); - console.log("Course ", course); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152325.js b/.history/controllers/blog_20210519152325.js deleted file mode 100644 index 1fe749e..0000000 --- a/.history/controllers/blog_20210519152325.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }); - console.log("Course ", course); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152415.js b/.history/controllers/blog_20210519152415.js deleted file mode 100644 index 72b3d1c..0000000 --- a/.history/controllers/blog_20210519152415.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.findOne({ _id }); - console.log("Course ", course); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152419.js b/.history/controllers/blog_20210519152419.js deleted file mode 100644 index 72b3d1c..0000000 --- a/.history/controllers/blog_20210519152419.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.findOne({ _id }); - console.log("Course ", course); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152443.js b/.history/controllers/blog_20210519152443.js deleted file mode 100644 index 1fe749e..0000000 --- a/.history/controllers/blog_20210519152443.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const Course = require("../models/Course"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let course = await Course.find({ _id }); - console.log("Course ", course); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (course) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152758.js b/.history/controllers/blog_20210519152758.js deleted file mode 100644 index 2f29cf6..0000000 --- a/.history/controllers/blog_20210519152758.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152820.js b/.history/controllers/blog_20210519152820.js deleted file mode 100644 index b0cfe9a..0000000 --- a/.history/controllers/blog_20210519152820.js +++ /dev/null @@ -1,105 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519152920.js b/.history/controllers/blog_20210519152920.js deleted file mode 100644 index 596af29..0000000 --- a/.history/controllers/blog_20210519152920.js +++ /dev/null @@ -1,105 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user.firstName + " " + user.lastName, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519153031.js b/.history/controllers/blog_20210519153031.js deleted file mode 100644 index a0f4d7d..0000000 --- a/.history/controllers/blog_20210519153031.js +++ /dev/null @@ -1,105 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user._id == _id) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user.firstName + " " + user.lastName, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519153142.js b/.history/controllers/blog_20210519153142.js deleted file mode 100644 index 1b4efdc..0000000 --- a/.history/controllers/blog_20210519153142.js +++ /dev/null @@ -1,105 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user._id == _id) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user._id, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519153454.js b/.history/controllers/blog_20210519153454.js deleted file mode 100644 index b8c7f06..0000000 --- a/.history/controllers/blog_20210519153454.js +++ /dev/null @@ -1,105 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user._id == _id) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519153459.js b/.history/controllers/blog_20210519153459.js deleted file mode 100644 index 13ac015..0000000 --- a/.history/controllers/blog_20210519153459.js +++ /dev/null @@ -1,105 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - // console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user._id == _id) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519153836.js b/.history/controllers/blog_20210519153836.js deleted file mode 100644 index fa3e179..0000000 --- a/.history/controllers/blog_20210519153836.js +++ /dev/null @@ -1,105 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - // console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user._id == _id) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: User.find({ _id }), - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519153855.js b/.history/controllers/blog_20210519153855.js deleted file mode 100644 index 1eb7526..0000000 --- a/.history/controllers/blog_20210519153855.js +++ /dev/null @@ -1,105 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - // console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user._id == _id) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: , - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519153953.js b/.history/controllers/blog_20210519153953.js deleted file mode 100644 index ae60f82..0000000 --- a/.history/controllers/blog_20210519153953.js +++ /dev/null @@ -1,106 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - // console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user._id == _id) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user.firstName, - }); - console.log("Brfore save", blog); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519154014.js b/.history/controllers/blog_20210519154014.js deleted file mode 100644 index 98a24d4..0000000 --- a/.history/controllers/blog_20210519154014.js +++ /dev/null @@ -1,106 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - // console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user.firstName, - }); - console.log("Brfore save", blog); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519154059.js b/.history/controllers/blog_20210519154059.js deleted file mode 100644 index 98274a1..0000000 --- a/.history/controllers/blog_20210519154059.js +++ /dev/null @@ -1,106 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - // console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user, - }); - console.log("Brfore save", blog); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519154126.js b/.history/controllers/blog_20210519154126.js deleted file mode 100644 index b7d1f2b..0000000 --- a/.history/controllers/blog_20210519154126.js +++ /dev/null @@ -1,105 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - // console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - }); - console.log("Brfore save", blog); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519154454.js b/.history/controllers/blog_20210519154454.js deleted file mode 100644 index 959a2a9..0000000 --- a/.history/controllers/blog_20210519154454.js +++ /dev/null @@ -1,106 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - // console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author:user. - }); - console.log("Before save", blog); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519154456.js b/.history/controllers/blog_20210519154456.js deleted file mode 100644 index 7ff2191..0000000 --- a/.history/controllers/blog_20210519154456.js +++ /dev/null @@ -1,106 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - // console.log("User ", user); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user, - }); - console.log("Before save", blog); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519154625.js b/.history/controllers/blog_20210519154625.js deleted file mode 100644 index b18be73..0000000 --- a/.history/controllers/blog_20210519154625.js +++ /dev/null @@ -1,106 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.find({ _id }); - console.log("User ", user._id); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user, - }); - // console.log("Before save", blog); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519154733.js b/.history/controllers/blog_20210519154733.js deleted file mode 100644 index 7150d52..0000000 --- a/.history/controllers/blog_20210519154733.js +++ /dev/null @@ -1,106 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.findById({ _id }); - console.log("User ", user._id); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user, - }); - // console.log("Before save", blog); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/controllers/blog_20210519154825.js b/.history/controllers/blog_20210519154825.js deleted file mode 100644 index b7c474e..0000000 --- a/.history/controllers/blog_20210519154825.js +++ /dev/null @@ -1,104 +0,0 @@ -const { log } = require("handlebars"); -const Blog = require("../models/Blog"); -const User = require("../models/User"); - -module.exports.getAllBlogs = async (req, res, next) => { - try { - let blog = await Blog.find(); - if (blog) { - res.json({ blogs: blog }); - } else { - console.log("Error in Blog", blog); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. - try { - let user = await User.findById({ _id }); - if (!title) { - res.status(422).json({ message: "Please, Add Title of the Blog" }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add Body of the Blog" }); - return; - } - if (user) { - let blog = new Blog({ - title, - body, - image, - date: new Date(), - author: user, - }); - await blog.save(); - res.json({ message: "Blog Saved Successfully!" }); - } else { - res.status.json({ error: "User Doesn't Exists" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.deleteBlog = async (req, res, next) => { - const { _id } = req.body; - try { - let blog = await Blog.find({ _id }); - if (blog) { - await Blog.findByIdAndDelete(_id); - res.json({ message: "Blog Deleted!" }); - } else { - res.status(404).json({ message: "Blog Not Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; - -module.exports.editBlog = async (req, res, next) => { - const { _id, body, title, image } = req.body; - - try { - if (!title) { - res.status(422).json({ message: "Please, Add the Title of the Blog." }); - return; - } - if (!body) { - res.status(422).json({ message: "Please, Add the Body of the Blog." }); - return; - } - if (!image) { - res.status(422).json({ message: "Please, Add the Image of the Blog." }); - return; - } - let blog = Blog.findById(_id); - if (blog) { - blog.title = title; - blog.body = body; - blog.image = image; - await blog.save(); - res.json({ message: "Blog Updated!" }); - } else { - res.status(422).json({ error: "Blog Doesn't Found" }); - return; - } - } catch { - (err) => { - res.status(422).json({ error: err }); - }; - } -}; diff --git a/.history/models/Blog_20210519103433.js b/.history/models/Blog_20210519103433.js deleted file mode 100644 index 33e6c55..0000000 --- a/.history/models/Blog_20210519103433.js +++ /dev/null @@ -1,27 +0,0 @@ -const mongoose = require("mongoose"); -const blogSchema = new mongoose.Schema({ - title: { - type: String, - require: true, - }, - author: { - type: String, - ref: "Course", - }, - date: { - type: Date, - }, - image: { - type: String, - default: "", - }, - body: { - type: String, - required: true, - }, - isBookMarked: { - type: Boolean, - default: false, - }, -}); -module.exports = mongoose.model("Blog", blogSchema); diff --git a/.history/models/Blog_20210519150611.js b/.history/models/Blog_20210519150611.js deleted file mode 100644 index ffc56dd..0000000 --- a/.history/models/Blog_20210519150611.js +++ /dev/null @@ -1,28 +0,0 @@ -const mongoose = require("mongoose"); -const { ObjectId } = mongoose.Schema.Types; -const blogSchema = new mongoose.Schema({ - title: { - type: String, - require: true, - }, - author: { - type: ObjectId, - ref: "Course", - }, - date: { - type: Date, - }, - image: { - type: String, - default: "", - }, - body: { - type: String, - required: true, - }, - isBookMarked: { - type: Boolean, - default: false, - }, -}); -module.exports = mongoose.model("Blog", blogSchema); diff --git a/.history/models/Blog_20210519150818.js b/.history/models/Blog_20210519150818.js deleted file mode 100644 index 6ad1746..0000000 --- a/.history/models/Blog_20210519150818.js +++ /dev/null @@ -1,25 +0,0 @@ -const mongoose = require("mongoose"); -const { ObjectId } = mongoose.Schema.Types; -const blogSchema = new mongoose.Schema({ - title: { - type: String, - require: true, - }, - author: { ref: "Course", type: ObjectId }, - date: { - type: Date, - }, - image: { - type: String, - default: "", - }, - body: { - type: String, - required: true, - }, - isBookMarked: { - type: Boolean, - default: false, - }, -}); -module.exports = mongoose.model("Blog", blogSchema); diff --git a/.history/models/Blog_20210519152942.js b/.history/models/Blog_20210519152942.js deleted file mode 100644 index a4bd0fc..0000000 --- a/.history/models/Blog_20210519152942.js +++ /dev/null @@ -1,25 +0,0 @@ -const mongoose = require("mongoose"); -const { ObjectId } = mongoose.Schema.Types; -const blogSchema = new mongoose.Schema({ - title: { - type: String, - require: true, - }, - author: { ref: "User", type: ObjectId }, - date: { - type: Date, - }, - image: { - type: String, - default: "", - }, - body: { - type: String, - required: true, - }, - isBookMarked: { - type: Boolean, - default: false, - }, -}); -module.exports = mongoose.model("Blog", blogSchema); diff --git a/.history/models/Blog_20210519153800.js b/.history/models/Blog_20210519153800.js deleted file mode 100644 index 774666e..0000000 --- a/.history/models/Blog_20210519153800.js +++ /dev/null @@ -1,25 +0,0 @@ -const mongoose = require("mongoose"); - -const blogSchema = new mongoose.Schema({ - title: { - type: String, - require: true, - }, - author: { ref: "User", type: mongoose.Schema.Types.ObjectId }, - date: { - type: Date, - }, - image: { - type: String, - default: "", - }, - body: { - type: String, - required: true, - }, - isBookMarked: { - type: Boolean, - default: false, - }, -}); -module.exports = mongoose.model("Blog", blogSchema); diff --git a/.history/routes/blog_20210519114750.js b/.history/routes/blog_20210519114750.js deleted file mode 100644 index 51e5d57..0000000 --- a/.history/routes/blog_20210519114750.js +++ /dev/null @@ -1,14 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const BlogController = require("../controllers/blog"); -const isAuth = require("../middleware/requirelogin"); - -router.post("/add-blog", BlogController.addBlog); - -router.get("/get-all-blogs", BlogController.getAllBlogs); - -router.post("/delete-blog/", isAuth, BlogController.deleteBlog); - -router.post("/edit-blog/", isAuth, BlogController.editBlog); - -module.exports = router; diff --git a/.history/routes/blog_20210519151054.js b/.history/routes/blog_20210519151054.js deleted file mode 100644 index 9336f60..0000000 --- a/.history/routes/blog_20210519151054.js +++ /dev/null @@ -1,14 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const BlogController = require("../controllers/blog"); -const isAuth = require("../middleware/requirelogin"); - -router.post("/add-blog", BlogController.addBlog); - -router.get("/get-all-blogs", BlogController.getAllBlogs); - -router.post("/delete-blog/", BlogController.deleteBlog); - -router.post("/edit-blog/", BlogController.editBlog); - -module.exports = router;