UNPKG

generator-angular-material-fullstack

Version:

Yeoman generator for creating MEAN stack applications, using MongoDB, Express, AngularJS, and Node

90 lines (81 loc) 3.13 kB
/** * Using Rails-like standard naming convention for endpoints. * GET /things -> index * POST /things -> create * GET /things/:id -> show * PUT /things/:id -> update * DELETE /things/:id -> destroy */ 'use strict'; var _ = require('lodash');<% if (filters.mongoose) { %> var Thing = require('./thing.model');<% } %> // Get list of things exports.index = function(req, res) {<% if (!filters.mongoose) { %> res.json([ { name : 'Development Tools', info : 'Integration with popular tools such as Bower, Grunt, Karma, Mocha, JSHint, Node Inspector, Livereload, Protractor, Jade, Stylus, Sass, CoffeeScript, and Less.' }, { name : 'Server and Client integration', info : 'Built with a powerful and fun stack: MongoDB, Express, AngularJS, and Node.' }, { name : 'Smart Build System', info : 'Build system ignores `spec` files, allowing you to keep tests alongside code. Automatic injection of scripts and styles into your index.html' }, { name : 'Modular Structure', info : 'Best practice client and server structures allow for more code reusability and maximum scalability' }, { name : 'Optimized Build', info : 'Build process packs up your templates as a single JavaScript payload, minifies your scripts/css/images, and rewrites asset names for caching.' },{ name : 'Deployment Ready', info : 'Easily deploy your app to Heroku or Openshift with the heroku and openshift subgenerators' } ]);<% } %><% if (filters.mongoose) { %> Thing.find(function (err, things) { if(err) { return handleError(res, err); } return res.status(200).json(things); });<% } %> };<% if (filters.mongoose) { %> // Get a single thing exports.show = function(req, res) { Thing.findById(req.params.id, function (err, thing) { if(err) { return handleError(res, err); } if(!thing) { return res.status(404).send('Not Found'); } return res.json(thing); }); }; // Creates a new thing in the DB. exports.create = function(req, res) { Thing.create(req.body, function(err, thing) { if(err) { return handleError(res, err); } return res.status(201).json(thing); }); }; // Updates an existing thing in the DB. exports.update = function(req, res) { if(req.body._id) { delete req.body._id; } Thing.findById(req.params.id, function (err, thing) { if (err) { return handleError(res, err); } if(!thing) { return res.status(404).send('Not Found'); } var updated = _.merge(thing, req.body); updated.save(function (err) { if (err) { return handleError(res, err); } return res.status(200).json(thing); }); }); }; // Deletes a thing from the DB. exports.destroy = function(req, res) { Thing.findById(req.params.id, function (err, thing) { if(err) { return handleError(res, err); } if(!thing) { return res.status(404).send('Not Found'); } thing.remove(function(err) { if(err) { return handleError(res, err); } return res.status(204).send('No Content'); }); }); }; function handleError(res, err) { return res.status(500).send(err); }<% } %>