-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathbasename.controller.js
144 lines (130 loc) · 4.32 KB
/
basename.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
135
136
137
138
139
140
141
142
143
144
/**
* Using Rails-like standard naming convention for endpoints.
* GET <%= route %> -> index<% if(filters.models) { %>
* POST <%= route %> -> create
* GET <%= route %>/:id -> show
* PUT <%= route %>/:id -> upsert
* PATCH <%= route %>/:id -> patch
* DELETE <%= route %>/:id -> destroy<% } %>
*/
'use strict';<% if(filters.models) { %>
import jsonpatch from 'fast-json-patch';<% if(filters.mongooseModels) { %>
import <%= classedName %> from './<%= basename %>.model';<% } if(filters.sequelizeModels) { %>
import {<%= classedName %>} from '<%= relativeRequire(config.get('registerModelsFile')) %>';<% } %>
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if(entity) {
return res.status(statusCode).json(entity);
}
return null;
};
}
function patchUpdates(patches) {
return function(entity) {
try {
// eslint-disable-next-line prefer-reflect
jsonpatch.apply(entity, patches, /*validate*/ true);
} catch(err) {
return Promise.reject(err);
}
return entity.save();
};
}
function removeEntity(res) {
return function(entity) {
if(entity) {
<% if(filters.mongooseModels) { %>return entity.remove()<% }
if(filters.sequelizeModels) { %>return entity.destroy()<% } %>
.then(() => {
res.status(204).end();
});
}
};
}
function handleEntityNotFound(res) {
return function(entity) {
if(!entity) {
res.status(404).end();
return null;
}
return entity;
};
}
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}<% } %>
// Gets a list of <%= classedName %>s
export function index(req, res) {<% if(!filters.models) { %>
res.json([]);<% } else { %>
<% if(filters.mongooseModels) { %>return <%= classedName %>.find().exec()<% }
if(filters.sequelizeModels) { %>return <%= classedName %>.findAll()<% } %>
.then(respondWithResult(res))
.catch(handleError(res));<% } %>
}<% if(filters.models) { %>
// Gets a single <%= classedName %> from the DB
export function show(req, res) {
<% if(filters.mongooseModels) { %>return <%= classedName %>.findById(req.params.id).exec()<% }
if(filters.sequelizeModels) { %>return <%= classedName %>.find({
where: {
_id: req.params.id
}
})<% } %>
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Creates a new <%= classedName %> in the DB
export function create(req, res) {
<% if(filters.mongooseModels) { %>return <%= classedName %>.create(req.body)<% }
if(filters.sequelizeModels) { %>return <%= classedName %>.create(req.body)<% } %>
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
// Upserts the given <%= classedName %> in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
<%_ if(filters.mongooseModels) { -%>
return <%= classedName %>.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()<% } %>
<%_ if(filters.sequelizeModels) { -%>
return <%= classedName %>.upsert(req.body, {
where: {
_id: req.params.id
}
})<% } %>
.then(respondWithResult(res))
.catch(handleError(res));
}
// Updates an existing <%= classedName %> in the DB
export function patch(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
<% if(filters.mongooseModels) { %>return <%= classedName %>.findById(req.params.id).exec()<% }
if(filters.sequelizeModels) { %>return <%= classedName %>.find({
where: {
_id: req.params.id
}
})<% } %>
.then(handleEntityNotFound(res))
.then(patchUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Deletes a <%= classedName %> from the DB
export function destroy(req, res) {
<% if(filters.mongooseModels) { %>return <%= classedName %>.findById(req.params.id).exec()<% }
if(filters.sequelizeModels) { %>return <%= classedName %>.find({
where: {
_id: req.params.id
}
})<% } %>
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}<% } %>