22const CarDao = require ( '../dao/carDao' ) ;
33
44/* Load Controller Common function */
5- const controllerCommon = require ( './common/controllerCommon' ) ;
5+ const ControllerCommon = require ( './common/controllerCommon' ) ;
66
77/* Load Car entity */
88const Car = require ( '../model/car' ) ;
@@ -14,7 +14,7 @@ class CarController {
1414
1515 constructor ( ) {
1616 this . carDao = new CarDao ( ) ;
17- this . common = new controllerCommon ( ) ;
17+ this . common = new ControllerCommon ( ) ;
1818 }
1919
2020 /**
@@ -24,47 +24,31 @@ class CarController {
2424 */
2525 findById ( req , res ) {
2626 let id = req . params . id ;
27- let _self = this ;
2827
2928 this . carDao . findById ( id )
30- . then ( function ( result ) {
31- _self . common . findSuccess ( result , res ) ;
32- } )
33- . catch ( function ( error ) {
34- _self . common . findError ( error , res ) ;
35- } ) ;
29+ . then ( this . common . findSuccess ( res ) )
30+ . catch ( this . common . findError ( res ) ) ;
3631 } ;
3732
3833 /**
3934 * Finds all entities.
4035 * @return all entities
4136 */
4237 findAll ( res ) {
43- let _self = this ;
44-
4538 this . carDao . findAll ( )
46- . then ( function ( results ) {
47- _self . common . findSuccess ( results , res ) ;
48- } )
49- . catch ( function ( error ) {
50- _self . common . findError ( error , res ) ;
51- } ) ;
39+ . then ( this . common . findSuccess ( res ) )
40+ . catch ( this . common . findError ( res ) ) ;
5241 } ;
5342
5443 /**
5544 * Counts all the records present in the database
5645 * @return count
5746 */
5847 countAll ( res ) {
59- let _self = this ;
6048
6149 this . carDao . countAll ( )
62- . then ( function ( result ) {
63- _self . common . findSuccess ( result , res ) ;
64- } )
65- . catch ( function ( error ) {
66- _self . common . serverError ( error , res ) ;
67- } ) ;
50+ . then ( this . common . findSuccess ( res ) )
51+ . catch ( this . common . serverError ( res ) ) ;
6852 } ;
6953
7054 /**
@@ -73,16 +57,16 @@ class CarController {
7357 * @return true if the entity has been updated, false if not found and not updated
7458 */
7559 update ( req , res ) {
76- let _self = this ;
77- let car = new Car ( req . body . id , req . body . maker , req . body . model , req . body . year , req . body . driver ) ;
60+ let car = new Car ( ) ;
61+ car . id = req . body . id ;
62+ car . maker = req . body . maker ;
63+ car . model = req . body . model ;
64+ car . year = req . body . year ;
65+ car . driver = req . body . driver ;
7866
7967 return this . carDao . update ( car )
80- . then ( function ( ) {
81- _self . common . editSuccess ( res ) ;
82- } )
83- . catch ( function ( error ) {
84- _self . common . serverError ( error , res ) ;
85- } ) ;
68+ . then ( this . common . editSuccess ( res ) )
69+ . catch ( this . common . serverError ( res ) ) ;
8670 } ;
8771
8872 /**
@@ -91,16 +75,26 @@ class CarController {
9175 * returns database insertion status
9276 */
9377 create ( req , res ) {
94- let _self = this ;
95- let car = new Car ( 0 , req . body . maker , req . body . model , req . body . year , req . body . driver ) ;
96-
97- return this . carDao . create ( car )
98- . then ( function ( ) {
99- _self . common . editSuccess ( res ) ;
100- } )
101- . catch ( function ( error ) {
102- _self . common . serverError ( error , res ) ;
103- } ) ;
78+ let car = new Car ( ) ;
79+ if ( req . body . id ) {
80+ car . id = req . body . id ;
81+ }
82+ car . maker = req . body . maker ;
83+ car . model = req . body . model ;
84+ car . year = req . body . year ;
85+ car . driver = req . body . driver ;
86+
87+ if ( req . body . id ) {
88+ return this . carDao . createWithId ( car )
89+ . then ( this . common . editSuccess ( res ) )
90+ . catch ( this . common . serverError ( res ) ) ;
91+ }
92+ else {
93+ return this . carDao . create ( car )
94+ . then ( this . common . editSuccess ( res ) )
95+ . catch ( this . common . serverError ( res ) ) ;
96+ }
97+
10498 } ;
10599
106100 /**
@@ -109,16 +103,11 @@ class CarController {
109103 * returns database deletion status
110104 */
111105 deleteById ( req , res ) {
112- let _self = this ;
113106 let id = req . params . id ;
114107
115108 this . carDao . deleteById ( id )
116- . then ( function ( ) {
117- _self . common . editSuccess ( res ) ;
118- } )
119- . catch ( function ( error ) {
120- _self . common . serverError ( error , res ) ;
121- } ) ;
109+ . then ( this . common . editSuccess ( res ) )
110+ . catch ( this . common . serverError ( res ) ) ;
122111 } ;
123112
124113 /**
@@ -127,16 +116,11 @@ class CarController {
127116 * @return
128117 */
129118 exists ( req , res ) {
130- let _self = this ;
131119 let id = req . params . id ;
132120
133121 this . carDao . exists ( id )
134- . then ( function ( ) {
135- _self . common . existsSuccess ( res ) ;
136- } )
137- . catch ( function ( error ) {
138- _self . common . findError ( error , res ) ;
139- } ) ;
122+ . then ( this . common . existsSuccess ( res ) )
123+ . catch ( this . common . findError ( res ) ) ;
140124 } ;
141125}
142126
0 commit comments