forked from angular-fullstack/generator-angular-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththing.controller.js
134 lines (120 loc) · 3.37 KB
/
thing.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* 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';
<% if (filters.mongoose) { %>
var _ = require('lodash');
var Thing = require('./thing.model');
function handleError(res, statusCode){
statusCode = statusCode || 500;
return function(err){
res.send(statusCode, err);
};
}
function responseWithResult(res, statusCode){
statusCode = statusCode || 200;
return function(entity){
if(entity){
return res.json(statusCode, entity);
}
};
}
function handleEntityNotFound(res){
return function(entity){
if(!entity){
res.send(404);
return null;
}
return entity;
};
}
function saveUpdates(updates){
return function(entity){
var updated = _.merge(entity, updates);
return updated
.saveAsync()
.then(function () {
return updated;
});
};
}
function removeEntity(res){
return function (entity) {
if(entity){
return entity.removeAsync()
.then(function() {
return res.send(204);
});
}
};
}
<% } %>
// 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.findAsync()
.then(responseWithResult(res))
.catch(handleError(res));
<% } %>
};
<% if (filters.mongoose) { %>
// Get a single thing
exports.show = function(req, res) {
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(responseWithResult(res))
.catch(handleError(res));
};
// Creates a new thing in the DB.
exports.create = function(req, res) {
Thing.createAsync(req.body)
.then(responseWithResult(res, 201))
.catch(handleError(res));
};
// Updates an existing thing in the DB.
exports.update = function(req, res) {
if(req.body._id) {
delete req.body._id;
}
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(responseWithResult(res))
.catch(handleError(res));
};
// Deletes a thing from the DB.
exports.destroy = function(req, res) {
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
};
<% } %>