text: 'use strict';
  
  /**
   * Module dependencies.
   */
  var path = require('path'),
    mongoose = require('mongoose'),
    Company = mongoose.model('Company'),
    errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller'));
  
  /**
   * Create a company
   */
  exports.create = function (req, res) {
    var company = new Company(req.body);
    company.createdBy = req.user;
  
    company.save(function (err) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(company);
      }
    });
  };
  
  /**
   * Show the current company
   */
  exports.read = function (req, res) {
    res.json(req.company);
  };
  
  /**
   * Update a company
   */
  exports.update = function (req, res) {
    var company = req.company;
  
    company.name = req.body.name;
    company.description = req.body.description;
  
    company.save(function (err) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(company);
      }
    });
  };
  
  /**
   * Delete an company
   */
  exports.delete = function (req, res) {
    var company = req.company;
  
    company.remove(function (err) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(company);
      }
    });
  };
  
  /**
   * List of companies
   */
  exports.list = function (req, res) {
    Company.find().sort('-created').populate('createdBy', 'displayName').exec(function (err, companies) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(companies);
      }
    });
  };
  
  /**
   * Company middleware
   */
  exports.companyByID = function (req, res, next, id) {
  
    if (!mongoose.Types.ObjectId.isValid(id)) {
      return res.status(400).send({
        message: 'Company is invalid'
      });
    }
  
    Company.findById(id).populate('createdBy', 'displayName').exec(function (err, company) {
      if (err) {
        return next(err);
      } else if (!company) {
        return res.status(404).send({
          message: 'No company with that identifier has been found'
        });
      }    
      req.company = company;
      next();
    });
  };
  