Skip to content

Instantly share code, notes, and snippets.

@russiann
Created March 20, 2016 16:10
Show Gist options
  • Save russiann/f5bd8d9e9d76ab6cfc17 to your computer and use it in GitHub Desktop.
Save russiann/f5bd8d9e9d76ab6cfc17 to your computer and use it in GitHub Desktop.
Controller de Pacientes
'use strict';
var _ = require('lodash');
var Patient = require('./patient.model');
var jwt = require('../../auth/jwt');
var Hash = require('hashids');
var hashids = new Hash('666');
function handleError (res, err) {
return res.status(500).send(err);
}
exports.index = function (req, res) {
Patient
.find()
.where({unit: req.user.unit})
.where({active: true})
.exec(function (err, patients) {
if (err) { return handleError(res, err); }
return res.status(200).json(patients);
});
};
exports.registerPushToken = function (req, res) {
Patient.findById(req.params.id, function (err, patient) {
if (err) { return handleError(res, err); }
if (!patient) { return res.status(404).end(); }
console.log(req.body);
patient.pushToken = req.body.token;
console.log(req.body);
patient.save(function () {
return res.status(200).json(patient);
})
});
};
exports.show = function (req, res) {
Patient.findById(req.params.id, function (err, patient) {
if (err) { return handleError(res, err); }
if (!patient) { return res.status(404).end(); }
return res.status(200).json(patient);
});
};
exports.getByCode = function (req, res) {
Patient
.findOne({code: req.params.code}, function (err, patient) {
if (err) { return handleError(res, err); }
if (!patient) { return res.status(404).end(); }
return res.status(200).json(_.extend(patient, {token: jwt.createJWT(patient) }));
});
};
exports.getByCPF = function (req, res) {
Patient
.findOne({cpf: req.params.cpf}, function (err, patient) {
if (err) { return handleError(res, err); }
if (!patient) { return res.status(404).end(); }
return res.status(200).json(_.extend(patient, {token: jwt.createJWT(patient) }));
});
};
exports.create = function (req, res) {
var patient = new Patient({
name: req.body.name,
phone: req.body.phone,
photo: req.body.photo,
genre: req.body.genre.value,
birth: req.body.birth,
cpf: req.body.cpf,
unit: req.user.unit,
code: Date.now(),
});
patient.save(function(err, result) {
if (err) {
console.log(err);
res.status(500).send({ message: err.message });
}
res.json({ user: _.omit(result, 'password'), token: jwt.createJWT(result) });
});
};
exports.update = function (req, res) {
if (req.body._id) { delete req.body._id; }
Patient.findById(req.params.id, function (err, patient) {
if (err) { return handleError(res, err); }
if (!patient) { return res.status(404).end(); }
var updated = _.merge(patient, req.body);
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.status(200).json(patient);
});
});
};
exports.destroy = function (req, res) {
Patient.findById(req.params.id, function (err, patient) {
if (err) { return handleError(res, err); }
if (!patient) { return res.status(404).end(); }
patient.active = false;
patient.save(function (err) {
if (err) { return handleError(res, err); }
return res.status(204).end();
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment