text: const {validationResult} = require("express-validator");
  const path = require("path");
  const fs = require("fs");
  const BlogPost = require("../models/blog");
  
  exports.createBlogPost = (req, res, next) => {
      const errors = validationResult(req);
  
      if(!errors.isEmpty()){
          const err = new Error('Invalid Value');
          err.errorStatus = 400;
          err.data = errors.array();
          throw err;
      }
  
      if(!req.file){
          const err = new Error('Image Harus Di Upload');
          err.errorStatus = 422;
          throw err;
      }
  
      const title = req.body.title;
      const image = req.file.path;
      const body = req.body.body;
  
      const Posting = new BlogPost({
          title: title,
          body: body,
          image: image,
          author: {
              uid: 1,
              name: "Prasetyawan"
          }
      });
  
      Posting.save()
      .then(result => {
          res.status(201).json({
              message: "Blog Post Berhasil Dibuat",
              data: result
          });
      })
      .catch(err => {
          next(err);
      });
  }
  
  exports.getAllBlogPost = (req, res, next) => {
      BlogPost.find()
      .then(result => {
          res.status(200).json({
              message: "Data Blog Post Berhasil Dipanggil",
              data: result
          });
      })
      .catch(err => {
          next(err);
      });
  }
  
  exports.getBlogPostById = (req, res, next) => {
      const postId = req.params.postId;
      BlogPost.findById(postId)
      .then(result => {
          if(!result){
              const error = new Error("Blog Post Tidak Ditemukan");
              error.errorStatus = 404;
              throw error;
          }else{
              res.status(200).json({
                  message: "Data Blog Post Berhasil Dipanggil",
                  data: result
              });
          }
      })
      .catch(err => {
          next(err);
      })
  }
  
  exports.updateBlogPost = (req, res, next) => {
      const errors = validationResult(req);
  
      if(!errors.isEmpty()){
          const err = new Error('Invalid Value');
          err.errorStatus = 400;
          err.data = errors.array();
          throw err;
      }
  
      if(!req.file){
          const err = new Error('Image Harus Di Upload');
          err.errorStatus = 422;
          throw err;
      }
  
      const title = req.body.title;
      const image = req.file.path;
      const body = req.body.body;
      const postId = req.params.postId;
  
      BlogPost.findById(postId)
      .then(post => {
          if(!post){
              const err = new Error('Blog Post Tidak Ditemukan');
              err.errorStatus = 404;
              throw err;
          }
  
          post.title = title;
          post.body = body;
          post.image = image;
  
          return post.save();
      })
      .then(result => {
          res.status(200).json({
              message: "Blog Post Berhasil Diupdate",
              data: result
          });
      })
      .catch(err => {
          next(err);
      })
  }
  
  exports.deleteBlogPost = (req, res, next) => {
      const postId = req.params.postId;
  
      BlogPost.findById(postId)
      .then(post => {
          if(!post){
              const err = new Error('Blog Post Tidak Ditemukan');
              err.errorStatus = 404;
              throw err;
          }
  
          removeImage(post.image);
          return BlogPost.findByIdAndRemove(postId);
      })
      .then(result => {
          res.status(200).json({
              message: "Blog Post Berhasil Dihapus",
              data: result
          });
      })
      .catch(err => {
          next(err);
      });
  }
  
  const removeImage = (filePath) => {
      filePath = path.join(__dirname, '../..', filePath);
      fs.unlink(filePath, err => console.log(err));
  }