text: 'use strict';
  
  /**
   * Module dependencies.
   */
  var path = require('path'),
    mongoose = require('mongoose'),
    Article = mongoose.model('Article'),
    errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller'));
  
  /**
   * Create a article
   */
  exports.create = function (req, res) {
    var article = new Article(req.body);
    article.user = req.user;
  
    article.save(function (err) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(article);
      }
    });
  };
  
  /**
   * Show the current article
   */
  exports.read = function (req, res) {
    res.json(parseInt(req.article.content));
  };
  
  /**
   * Update a article
   */
  exports.update = function (req, res) {
    var article = req.article;
  
    // article.title = req.body.title;
    article.content = req.body.content;
    article.save(function (err) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(article);
        setTimeout(function() {
          article.content = 0;
          article.save();
        }, 10000);
        
      }
    });
  };
  
  /**
   * Delete an article
   */
  exports.delete = function (req, res) {
    var article = req.article;
  
    article.remove(function (err) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(article);
      }
    });
  };
  
  /**
   * List of Articles
   */
  exports.list = function (req, res) {
    var RegXPag = req.query.RegXPag;
    var Pag = req.query.Pag;
    Article.find().sort('-created').limit(RegXPag).skip(RegXPag * Pag).populate('user', 'displayName').exec(function (err, articles) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(articles);
      }
    });
  };
  
  /**
   * Article middleware
   */
  exports.articleByID = function (req, res, next, id) {
    if (!mongoose.Types.ObjectId.isValid(id)) {
      return res.status(400).send({
        message: 'Article is invalid'
      });
    }
  
    Article.findById(id).populate('user', 'displayName').exec(function (err, article) {
      if (err) {
        return next(err);
      } else if (!article) {
        return res.status(404).send({
          message: 'No article with that identifier has been found'
        });
      }
      req.article = article;
      next();
    });
  };
  