Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var _ = require('lodash');
  4. var Attribute = require('./attribute.model.js');
  5.  
  6. // Get list of mappings
  7. exports.index = function (req, res) {
  8. Attribute.find(req.query, function (err, mappings) {
  9. if (err) {
  10. return handleError(res, err);
  11. }
  12. return res.json(200, mappings);
  13. });
  14. };
  15.  
  16. // Get a single mapping
  17. exports.show = function (req, res) {
  18. Attribute.findById(req.params.id, function (err, mapping) {
  19. if (err) {
  20. return handleError(res, err);
  21. }
  22. if (!mapping) {
  23. return res.send(404);
  24. }
  25. return res.json(mapping);
  26. });
  27. };
  28.  
  29. // Creates a new mapping in the DB.
  30. exports.create = function (req, res) {
  31. Attribute.create(req.body, function (err, mapping) {
  32. if (err) {
  33. return handleError(res, err);
  34. }
  35. return res.json(201, mapping);
  36. });
  37. };
  38.  
  39. // Updates an existing mapping in the DB.
  40. exports.update = function (req, res) {
  41. if (req.body._id) {
  42. delete req.body._id;
  43. }
  44. if (req.body.__v) {
  45. delete req.body.__v;
  46. }
  47.  
  48. Attribute.findById(req.params.id, function (err, mapping) {
  49. if (err) {
  50. return handleError(res, err);
  51. }
  52. if (!mapping) {
  53. return res.send(404);
  54. }
  55.  
  56. var updated = _.merge(mapping, req.body);
  57. updated.markModified('groups');
  58. updated.groups = req.body.groups;
  59. updated.save(function (err) {
  60. if (err) {
  61. return handleError(res, err);
  62. }
  63. return res.json(200, mapping);
  64. });
  65. });
  66. };
  67.  
  68. // Deletes a mapping from the DB.
  69. exports.destroy = function (req, res) {
  70. Attribute.findById(req.params.id, function (err, mapping) {
  71. if (err) {
  72. return handleError(res, err);
  73. }
  74. if (!mapping) {
  75. return res.send(404);
  76. }
  77. mapping.remove(function (err) {
  78. if (err) {
  79. return handleError(res, err);
  80. }
  81. return res.send(204);
  82. });
  83. });
  84. };
  85.  
  86. function handleError(res, err) {
  87. return res.send(500, err);
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement