This commit is contained in:
yashrajverma 2021-05-20 12:14:03 +05:30
parent c003038778
commit bcf391a7d0
3 changed files with 7 additions and 10 deletions

View File

@ -4,10 +4,7 @@ const User = require("../models/User");
module.exports.getAllBlogs = async (req, res, next) => { module.exports.getAllBlogs = async (req, res, next) => {
try { try {
let blog = await Blog.find().populate( let blog = await Blog.find();
"author",
"-passwordResetToken -password"
);
if (blog) { if (blog) {
res.json({ blogs: blog }); res.json({ blogs: blog });
} else { } else {
@ -22,7 +19,7 @@ module.exports.getAllBlogs = async (req, res, next) => {
}; };
module.exports.addBlog = async (req, res, next) => { 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. const { _id, title, body, image, author } = req.body; //_id is of user from frontend who is adding the blog.
try { try {
let user = await User.findById({ _id }); let user = await User.findById({ _id });
if (!title) { if (!title) {
@ -39,7 +36,7 @@ module.exports.addBlog = async (req, res, next) => {
body, body,
image, image,
date: new Date(), date: new Date(),
author: user, author,
}); });
await blog.save(); await blog.save();
res.json({ message: "Blog Saved Successfully!" }); res.json({ message: "Blog Saved Successfully!" });
@ -88,12 +85,12 @@ module.exports.editBlog = async (req, res, next) => {
res.status(422).json({ message: "Please, Add the Image of the Blog." }); res.status(422).json({ message: "Please, Add the Image of the Blog." });
return; return;
} }
let blog = Blog.findById(_id); let blog = await Blog.findById({ _id });
if (blog) { if (blog) {
blog.title = title; blog.title = title;
blog.body = body; blog.body = body;
blog.image = image; blog.image = image;
await blog.save(); blog = await blog.save();
res.json({ message: "Blog Updated!" }); res.json({ message: "Blog Updated!" });
} else { } else {
res.status(422).json({ error: "Blog Doesn't Found" }); res.status(422).json({ error: "Blog Doesn't Found" });

View File

@ -5,7 +5,7 @@ const blogSchema = new mongoose.Schema({
type: String, type: String,
require: true, require: true,
}, },
author: { ref: "User", type: mongoose.Schema.Types.ObjectId }, author: { required: true, type: String },
date: { date: {
type: Date, type: Date,
}, },

View File

@ -21,7 +21,7 @@ router.post("/set-coupon", (req, res) => {
}); });
} }
}); });
router.get("/getAllCoupons", isAuth, isAdmin, couponController.getAllCoupons); router.get("/getAllCoupons", couponController.getAllCoupons);
router.post("/addCoupon", isAuth, isAdmin, couponController.addCoupon); router.post("/addCoupon", isAuth, isAdmin, couponController.addCoupon);