text: /** */
  const Article = require('./../models/Article')
  const User = require('./../models/User')
  const fs = require('fs')
  const cloudinary = require('cloudinary')
  
  module.exports = {
      
      addArticle: (req, res, next) => {
          let { text, title, claps, description } = req.body
          //let obj = { text, title, claps, description, feature_img: _feature_img != null ? `/uploads/${_filename}` : '' }
          if (req.files.image) {
              cloudinary.uploader.upload(req.files.image.path, (result) => {
                  let obj = { text, title, claps, description, feature_img: result.url != null ? result.url : '' }
                  saveArticle(obj)
                  /*(new Student({...{url: result.url},...req.body})).save((err, newStudent) => {
                  const cloud_res = {
                      url: result.url
                  }
                  const newS = newStudent.toObject()
                  console.log({...{url: result.url},...req.body})
                  if(err)
                      res.send(err)
                  else if (!newStudent)
                      res.send(400)
                  else
                      res.send({...newS,...cloud_res})
                  next()
              })*/
              },{
                  resource_type: 'image',
                  eager: [
                      {effect: 'sepia'}
                  ]
              })
          }else {
              saveArticle({ text, title, claps, description, feature_img: '' })
          }
          function saveArticle(obj) {
              new Article(obj).save((err, article) => {
                  if (err)
                      res.send(err)
                  else if (!article)
                      res.send(400)
                  else {
                      return article.addAuthor(req.body.author_id).then((_article) => {
                          return res.send(_article)
                      })
                  }
                  next()
              })
          }
          /*new Article(obj).save((err, article) => {
              if (err)
                  res.send(err)
              else if (!article)
                  res.send(400)
              else {
                  return article.addAuthor(req.body.author_id).then((_article) => {
                      return res.send(_article)
                  })
              }
              next()
          })*/
  
          /*var storage = multer.diskStorage({
              destination: function (req, file, callback) {
                  callback(null, './uploads')
              },
              filename: function () {
                  callback(null, )
              }
          })
          var upload = multer({
              storage: storage
          }).single('userFile')
          upload(req, res, function(err) {
          })*/
      },
      getAll: (req, res, next) => {
          Article.find(req.params.id)
          .populate('author')
          .populate('comments.author').exec((err, article)=> {
              if (err)
                  res.send(err)
              else if (!article)
                  res.send(404)
              else
                  res.send(article)
              next()            
          })
      },
  
      /**
       * article_id
       */
      clapArticle: (req, res, next) => {
          Article.findById(req.body.article_id).then((article)=> {
              return article.clap().then(()=>{
                  return res.json({msg: "Done"})
              })
          }).catch(next)
      },
  
      /**
       * comment, author_id, article_id
       */
      commentArticle: (req, res, next) => {
          Article.findById(req.body.article_id).then((article)=> {
              return article.comment({
                  author: req.body.author_id,
                  text: req.body.comment
              }).then(() => {
                  return res.json({msg: "Done"})
              })
          }).catch(next)
      },
  
      /**
       * article_id
       */
      getArticle: (req, res, next) => {
          Article.findById(req.params.id)
          .populate('author')
          .populate('comments.author').exec((err, article)=> {
              if (err)
                  res.send(err)
              else if (!article)
                  res.send(404)
              else
                  res.send(article)
              next()            
          })
      }
  }