const { log } = require("handlebars"); const Blog = require("../models/Blog"); const User = require("../models/User"); const mongoose = require("mongoose"); const GridFsStorage = require("multer-gridfs-storage"); const Grid = require("gridfs-stream"); const multer = require("multer"); const MONGO_URI = `mongodb+srv://Cantilever:Cantilever@cluster0.dqxva.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`; const connect = mongoose.createConnection(MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true, }); let gfs; connect.once("open", () => { // initialize stream gfs = Grid(connect.db, mongoose.mongo); gfs.collection("blogImages"); }); module.exports.getAllBlogs = async (req, res, next) => { try { let blog = await Blog.find(); if (blog) { gfs.files.find().toArray((err, files) => { //check if files exist if (!files || files.length == 0) { return res.status(404).json({ err: "No files exist", }); } // files exist res.json({ files, blogs: blog }); }); } else { console.log("Error in Blog", blog); return; } } catch { (err) => { res.status(422).json({ error: err }); }; } }; 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); }; } }; module.exports.addBlog = async (req, res, next) => { const { _id, title, body, author } = req.body; //_id is of user from frontend who is adding the blog. try { console.log("Image File", req.file); 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: req.file, date: new Date().toISOString(), 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 = await Blog.findById({ _id }); if (blog) { blog.title = title; blog.body = body; blog.image = image; blog = 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 }); }; } };