10
10
'use strict' ; < % if ( filters . models ) { % >
11
11
12
12
import _ from 'lodash' ; < % if ( filters . mongooseModels ) { % >
13
- var < %= classedName % > = require ( './<%= basename %>.model' ) ; < % } if ( filters . sequelizeModels ) { % >
14
- var sqldb = require ( '<%= relativeRequire(config.get(' registerModelsFile ')) %>' ) ;
15
- var < %= classedName % > = sqldb . < %= classedName % > ; < % } % >
13
+ import < %= classedName % > from './<%= basename %>.model' ; < % } if ( filters . sequelizeModels ) { % >
14
+ import { < %= classedName % > } from '<%= relativeRequire(config.get(' registerModelsFile ')) %>' ; < % } % >
16
15
17
- function handleError ( res , statusCode ) {
18
- statusCode = statusCode || 500 ;
19
- return function ( err ) {
20
- res . status ( statusCode ) . send ( err ) ;
21
- } ;
22
- }
23
-
24
- function responseWithResult ( res , statusCode ) {
16
+ function respondWithResult ( res , statusCode ) {
25
17
statusCode = statusCode || 200 ;
26
18
return function ( entity ) {
27
19
if ( entity ) {
@@ -30,16 +22,6 @@ function responseWithResult(res, statusCode) {
30
22
} ;
31
23
}
32
24
33
- function handleEntityNotFound ( res ) {
34
- return function ( entity ) {
35
- if ( ! entity ) {
36
- res . status ( 404 ) . end ( ) ;
37
- return null ;
38
- }
39
- return entity ;
40
- } ;
41
- }
42
-
43
25
function saveUpdates ( updates ) {
44
26
return function ( entity ) {
45
27
< % if ( filters . mongooseModels ) { % > var updated = _ . merge ( entity , updates ) ;
@@ -62,14 +44,31 @@ function removeEntity(res) {
62
44
} ) ;
63
45
}
64
46
} ;
47
+ }
48
+
49
+ function handleEntityNotFound ( res ) {
50
+ return function ( entity ) {
51
+ if ( ! entity ) {
52
+ res . status ( 404 ) . end ( ) ;
53
+ return null ;
54
+ }
55
+ return entity ;
56
+ } ;
57
+ }
58
+
59
+ function handleError ( res , statusCode ) {
60
+ statusCode = statusCode || 500 ;
61
+ return function ( err ) {
62
+ res . status ( statusCode ) . send ( err ) ;
63
+ } ;
65
64
} < % } % >
66
65
67
66
// Gets a list of <%= classedName %>s
68
67
export function index ( req , res ) { < % if ( ! filters . models ) { % >
69
68
res . json ( [ ] ) ; < % } else { % >
70
69
< % if ( filters . mongooseModels ) { % > < %= classedName % > .findAsync()< % }
71
70
if ( filters . sequelizeModels ) { % > < %= classedName % > .findAll()< % } % >
72
- . then ( responseWithResult ( res ) )
71
+ . then ( respondWithResult ( res ) )
73
72
. catch ( handleError ( res ) ) ; < % } % >
74
73
} < % if ( filters . models ) { % >
75
74
@@ -82,15 +81,15 @@ export function show(req, res) {
82
81
}
83
82
} )< % } % >
84
83
. then ( handleEntityNotFound ( res ) )
85
- . then ( responseWithResult ( res ) )
84
+ . then ( respondWithResult ( res ) )
86
85
. catch ( handleError ( res ) ) ;
87
86
}
88
87
89
88
// Creates a new <%= classedName %> in the DB
90
89
export function create ( req , res ) {
91
90
< % if ( filters . mongooseModels ) { % > < %= classedName % > .createAsync(req.body)< % }
92
91
if ( filters . sequelizeModels ) { % > < %= classedName % > .create(req.body)< % } % >
93
- . then ( responseWithResult ( res , 201 ) )
92
+ . then ( respondWithResult ( res , 201 ) )
94
93
. catch ( handleError ( res ) ) ;
95
94
}
96
95
@@ -107,7 +106,7 @@ export function update(req, res) {
107
106
} )< % } % >
108
107
. then ( handleEntityNotFound ( res ) )
109
108
. then ( saveUpdates ( req . body ) )
110
- . then ( responseWithResult ( res ) )
109
+ . then ( respondWithResult ( res ) )
111
110
. catch ( handleError ( res ) ) ;
112
111
}
113
112
0 commit comments