text: 'use strict';
  
  /**
   * Module dependencies
   */
  var path = require('path'),
    mongoose = require('mongoose'),
    Project = mongoose.model('Project'),
    errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller'));
  
  /**
   * Create an project
   */
  exports.create = function (req, res) {
    var project = new Project(req.body);
    project.user = req.user;
  
    project.save(function (err) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(project);
      }
    });
  };
  
  /**
   * Show the current project
   */
  exports.read = function (req, res) {
    // convert mongoose document to JSON
    var project = req.project ? req.project.toJSON() : {};
  
    // Add a custom field to the Project, for determining if the current User is the "owner".
    // NOTE: This field is NOT persisted to the database, since it doesn't exist in the Project model.
    project.isCurrentUserOwner = !!(req.user && project.user && project.user._id.toString() === req.user._id.toString());
  
    res.json(project);
  };
  
  /**
   * Update an project
   */
  exports.update = function (req, res) {
    var project = req.project;
  
    project.name = req.body.name;
    project.city = req.body.city;
    project.type = req.body.type;
    project.year = req.body.year;
    project.image = req.body.image;
  
    project.save(function (err) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        console.log('fired the update');
        res.json(project);
      }
    });
  };
  
  /**
   * Delete an project
   */
  exports.delete = function (req, res) {
    var project = req.project;
  
    project.remove(function (err) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(project);
      }
    });
  };
  
  /**
   * List of Projects
   */
  exports.list = function (req, res) {
    Project.find(req.query).sort('-created').populate('user', 'displayName').exec(function (err, projects) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        res.json(projects);
      }
    });
  };
  
  /**
   * Search for projects
   */
  exports.search = function(req, res){
    console.log("searching...");
  };
  
  /**
   * Project middleware
   */
  exports.projectByID = function (req, res, next, id) {
  
    if (!mongoose.Types.ObjectId.isValid(id)) {
      return res.status(400).send({
        message: 'Project is invalid'
      });
    }
  
    Project.findById(id).populate('user', 'displayName').exec(function (err, project) {
      if (err) {
        return next(err);
      } else if (!project) {
        return res.status(404).send({
          message: 'No project with that identifier has been found'
        });
      }
      req.project = project;
      next();
    });
  };
  