Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var _ = require('lodash');
- var Attribute = require('./attribute.model.js');
- // Get list of mappings
- exports.index = function (req, res) {
- Attribute.find(req.query, function (err, mappings) {
- if (err) {
- return handleError(res, err);
- }
- return res.json(200, mappings);
- });
- };
- // Get a single mapping
- exports.show = function (req, res) {
- Attribute.findById(req.params.id, function (err, mapping) {
- if (err) {
- return handleError(res, err);
- }
- if (!mapping) {
- return res.send(404);
- }
- return res.json(mapping);
- });
- };
- // Creates a new mapping in the DB.
- exports.create = function (req, res) {
- Attribute.create(req.body, function (err, mapping) {
- if (err) {
- return handleError(res, err);
- }
- return res.json(201, mapping);
- });
- };
- // Updates an existing mapping in the DB.
- exports.update = function (req, res) {
- if (req.body._id) {
- delete req.body._id;
- }
- if (req.body.__v) {
- delete req.body.__v;
- }
- Attribute.findById(req.params.id, function (err, mapping) {
- if (err) {
- return handleError(res, err);
- }
- if (!mapping) {
- return res.send(404);
- }
- var updated = _.merge(mapping, req.body);
- updated.markModified('groups');
- updated.groups = req.body.groups;
- updated.save(function (err) {
- if (err) {
- return handleError(res, err);
- }
- return res.json(200, mapping);
- });
- });
- };
- // Deletes a mapping from the DB.
- exports.destroy = function (req, res) {
- Attribute.findById(req.params.id, function (err, mapping) {
- if (err) {
- return handleError(res, err);
- }
- if (!mapping) {
- return res.send(404);
- }
- mapping.remove(function (err) {
- if (err) {
- return handleError(res, err);
- }
- return res.send(204);
- });
- });
- };
- function handleError(res, err) {
- return res.send(500, err);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement