text: 'use strict';
  
  /**
   * Module dependencies.
   */
  var mongoose = require('mongoose'),
  	errorHandler = require('./errors.server.controller'),
  	Patient = mongoose.model('Patient'),
  	_ = require('lodash');
  
  /**
   * Create a Patient
   */
  exports.create = function(req, res) {
  	var patient = new Patient(req.body);
  	patient.user = req.user;
  
  	patient.save(function(err) {
  		if (err) {
  			return res.status(400).send({
  				message: errorHandler.getErrorMessage(err)
  			});
  		} else {
  			res.jsonp(patient);
  		}
  	});
  };
  
  /**
   * Show the current Patient
   */
  exports.read = function(req, res) {
  	res.jsonp(req.patient);
  };
  
  /**
   * Update a Patient
   */
  exports.update = function(req, res) {
  	var patient = req.patient ;
  
  	patient = _.extend(patient , req.body);
  
  	patient.save(function(err) {
  		if (err) {
  			return res.status(400).send({
  				message: errorHandler.getErrorMessage(err)
  			});
  		} else {
  			res.jsonp(patient);
  		}
  	});
  };
  
  /**
   * Delete an Patient
   */
  exports.delete = function(req, res) {
  	var patient = req.patient ;
  
  	patient.remove(function(err) {
  		if (err) {
  			return res.status(400).send({
  				message: errorHandler.getErrorMessage(err)
  			});
  		} else {
  			res.jsonp(patient);
  		}
  	});
  };
  
  /**
   * List of Patients
   */
  exports.list = function(req, res) { 
      console.log('asdashdkjashgdaksjhdaksjhd');
      
  	Patient.find({}).exec(function(err, patients) {
  		if (err) {
  			return res.status(400).send({
  				message: errorHandler.getErrorMessage(err)
  			});
  		} else {
  			res.jsonp(patients);
  		}
  	});
  };
  
  
  exports.patId = function(req, res){
      var id = req.body.id;
  	Patient.find({_id:id}).exec(function(err, patient) {
              if (err) {
  			return res.status(400).send({
  				message: errorHandler.getErrorMessage(err)
  			});
  		} else {
  			res.jsonp(patient);
  		}
              
          });
  };
  
  
  /**
   * Patient middleware
   */
  exports.patientByID = function(req, res, next, id) { 
  	Patient.findById(id).populate('user', 'displayName').exec(function(err, patient) {
  		if (err) return next(err);
  		if (! patient) return next(new Error('Failed to load Patient ' + id));
  		req.patient = patient ;
  		next();
  	});
  };
  
  /**
   * Patient authorization middleware
   */
  exports.hasAuthorization = function(req, res, next) {
  	if (req.patient.user.id !== req.user.id) {
  		return res.status(403).send('User is not authorized');
  	}
  	next();
  };
  