3 var _ = require('lodash');
4 var Contact = require('../../models').Contact;
6 // Get list of contacts
7 exports.index = function(req, res) {
10 .then(function (contacts) {
11 return res.status(200).send(contacts);
13 .catch(function(err) {
14 return handleError(res, err);
18 // Get a single contact
19 exports.show = function(req, res) {
21 .findById(req.params.id)
22 .then(function (contact) {
23 if(!contact) { return res.sendStatus(404); }
24 return res.send(contact);
27 return handleError(res, err);
31 // Creates a new contact in the DB.
32 exports.create = function(req, res) {
35 .then(function(contact) {
36 return res.status(201).send(contact);
38 .catch(function(err) {
39 return handleError(res, err);
43 // Updates an existing contact in the DB.
44 exports.update = function(req, res) {
45 if(req.body.id) { delete req.body.id; }
47 .findById(req.params.id)
48 .then(function (contact) {
49 if(!contact) { return res.sendStatus(404); }
50 var updated = _.merge(contact, req.body);
53 return res.status(200).send(contact);
55 .catch(function(err) {
56 return handleError(res, err);
59 .catch(function(err) {
60 return handleError(res, err);
64 // Deletes a contact from the DB.
65 exports.destroy = function(req, res) {
67 .findById(req.params.id)
68 .then(function (contact) {
69 if(!contact) { return res.sendStatus(404); }
72 return res.sendStatus(204);
74 .catch(function(err) {
75 return handleError(res, err);
78 .catch(function(err) {
79 return handleError(res, err);
83 function handleError(res, err) {
84 return res.status(500).send(err);