{"size": 1245, "ext": "js", "max_stars_repo_path": "controllers/follow.js", "max_stars_repo_name": "medhelp-app/api", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "controllers/follow.js", "max_issues_repo_name": "medhelp-app/api", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controllers/follow.js", "max_forks_repo_name": "medhelp-app/api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.8421052632, "max_line_length": 100, "alphanum_fraction": 0.6289156627}
var Follow = require('../models/follow');

function FollowController () {
	
}

FollowController.prototype.insert = function(_follow, callback) {
	Follow.findOne({idDoctor: _follow.idDoctor, idPatient: _follow.idPatient}, function(error,result) {
		if(error){
			callback(null,error);
		}
		else{
			if(result){
				callback(null,{error:"Este paciente já segue este médico"});
			}
			else{
				var follow = new Follow();
				follow.idDoctor = _follow.idDoctor;
				follow.idPatient = _follow.idPatient;
				follow.date = _follow.date;
				follow.save(function (error,follow) {
					if(error){
						callback(null,error);
					}
					else{
						callback({success:"ok"})
					}
				});
				
			}
		}
	});
};

FollowController.prototype.delete = function(_idDoctor, _idPatient, callback) {
	Follow.findOne({idDoctor: _idDoctor,idPatient: _idPatient}, function(error,follow) {
		if(error){
			callback(null, error);
		}
		else{
			if(follow){
				Follow.remove({_id: follow._id}, function (error, result) {
					if (error) {
						callback(null, error);
					} else {
						callback({success:"ok"});
					}
				});
			}
			else{
				callback(null, {error:"Este paciente não segue este médico"});
			}
		}
	});
};

module.exports = FollowController;