Cantilever-Labs/controllers/blog.js

163 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-05-18 23:52:26 -07:00
const { log } = require("handlebars");
const Blog = require("../models/Blog");
2021-05-19 03:18:45 -07:00
const User = require("../models/User");
2021-05-18 23:52:26 -07:00
module.exports.getAllBlogs = async (req, res, next) => {
try {
2021-05-19 23:44:03 -07:00
let blog = await Blog.find();
2021-05-18 23:52:26 -07:00
if (blog) {
2021-05-27 07:46:20 -07:00
res.json({ blogs: blog });
2021-05-18 23:52:26 -07:00
} else {
console.log("Error in Blog", blog);
return;
}
} catch {
(err) => {
res.status(422).json({ error: err });
};
}
};
2021-05-26 03:05:16 -07:00
module.exports.getSingleBlog = async (req, res, next) => {
try {
const { id } = req.params;
let blog = await Blog.findById({ _id: id });
if (blog) {
res.json({ blog });
} else {
res.json({ error: "Blog Not Found" });
}
} catch {
(err) => {
console.log("Error", err);
};
}
};
2021-05-18 23:52:26 -07:00
module.exports.addBlog = async (req, res, next) => {
2021-05-26 03:05:16 -07:00
const { _id, title, body, author } = req.body; //_id is of user from frontend who is adding the blog.
2021-05-18 23:52:26 -07:00
try {
2021-05-26 03:05:16 -07:00
console.log("Image File", req.file);
2021-05-19 03:18:45 -07:00
let user = await User.findById({ _id });
2021-05-18 23:52:26 -07:00
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;
}
2021-05-19 03:18:45 -07:00
if (user) {
2021-05-18 23:52:26 -07:00
let blog = new Blog({
title,
body,
2021-05-27 07:46:20 -07:00
image: req.file.location,
2021-05-26 03:05:16 -07:00
date: new Date().toISOString(),
2021-05-19 23:44:03 -07:00
author,
2021-05-18 23:52:26 -07:00
});
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;
}
2021-05-19 23:44:03 -07:00
let blog = await Blog.findById({ _id });
2021-05-18 23:52:26 -07:00
if (blog) {
blog.title = title;
blog.body = body;
blog.image = image;
2021-05-19 23:44:03 -07:00
blog = await blog.save();
2021-05-18 23:52:26 -07:00
res.json({ message: "Blog Updated!" });
} else {
res.status(422).json({ error: "Blog Doesn't Found" });
return;
}
} catch {
(err) => {
res.status(422).json({ error: err });
};
}
};
2021-06-02 04:28:01 -07:00
module.exports.commentBlog = async (req, res, next) => {
const comment = req.body.comment;
const comments1 = {
user: req.user._id,
review: comment,
2021-06-04 05:07:17 -07:00
date: Date.now().toString(),
2021-06-02 04:28:01 -07:00
};
try {
if (comment) {
Blog.findByIdAndUpdate(
{ _id: req.params.id },
{
$push: {
comments: comments1,
},
},
2021-06-04 06:20:03 -07:00
{ new: true }
)
.populate("comments.user", "_id name")
.exec((err, result) => {
2021-06-02 04:28:01 -07:00
if (err) {
res.status(503).json({
2021-06-04 06:20:03 -07:00
message: "Some Error Occured!" + err,
2021-06-02 04:28:01 -07:00
});
} else {
res.status(201).json({
2021-06-04 06:20:03 -07:00
message: "Comment posted!" + result,
2021-06-02 04:28:01 -07:00
});
}
2021-06-04 06:20:03 -07:00
});
2021-06-02 04:28:01 -07:00
}
} catch (err) {
if (err) {
res.status(501).json({
message: "internal Server error please try again after some time",
err,
});
}
}
};