1
+ const db = require ( '../config/db.config.js' ) ;
2
+ const Customer = db . Customer ;
3
+
4
+ /**
5
+ * Save a Customer object to database MySQL/PostgreSQL
6
+ * @param {* } req
7
+ * @param {* } res
8
+ */
9
+ exports . createCustomer = ( req , res ) => {
10
+ let customer = { } ;
11
+
12
+ try {
13
+ // Building Customer object from upoading request's body
14
+ customer . firstname = req . body . firstname ;
15
+ customer . lastname = req . body . lastname ;
16
+ customer . address = req . body . address ;
17
+ customer . age = req . body . age ;
18
+
19
+ // Save to MySQL database
20
+ Customer . create ( customer ,
21
+ { attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' , "copyright" ] } )
22
+ . then ( result => {
23
+ res . status ( 200 ) . json ( result ) ;
24
+ } ) ;
25
+ } catch ( error ) {
26
+ res . status ( 500 ) . json ( {
27
+ message : "Fail!" ,
28
+ error : error . message
29
+ } ) ;
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Retrieve Customer information from database
35
+ * @param {* } req
36
+ * @param {* } res
37
+ */
38
+ exports . customers = ( req , res ) => {
39
+ // find all Customer information from
40
+ try {
41
+ Customer . findAll ( { attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' , 'copyright' ] } )
42
+ . then ( customers => {
43
+ res . status ( 200 ) . json ( customers ) ;
44
+ } )
45
+ } catch ( error ) {
46
+ // log on console
47
+ console . log ( error ) ;
48
+
49
+ res . status ( 500 ) . json ( {
50
+ message : "Error!" ,
51
+ error : error
52
+ } ) ;
53
+ }
54
+ }
55
+
56
+ exports . getCustomer = ( req , res ) => {
57
+ Customer . findByPk ( req . params . id ,
58
+ { attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' , 'copyright' ] } )
59
+ . then ( customer => {
60
+ res . status ( 200 ) . json ( customer ) ;
61
+ } ) . catch ( error => {
62
+ // log on console
63
+ console . log ( error ) ;
64
+
65
+ res . status ( 500 ) . json ( {
66
+ message : "Error!" ,
67
+ error : error
68
+ } ) ;
69
+ } )
70
+ }
71
+
72
+ /**
73
+ * Updating a Customer
74
+ * @param {* } req
75
+ * @param {* } res
76
+ */
77
+ exports . updateCustomer = async ( req , res ) => {
78
+ try {
79
+ let customer = await Customer . findByPk ( req . body . id ) ;
80
+
81
+ if ( ! customer ) {
82
+ // return a response to client
83
+ res . status ( 404 ) . json ( {
84
+ message : "Not Found for updating a customer with id = " + customerId ,
85
+ error : "404"
86
+ } ) ;
87
+ } else {
88
+ // update new change to database
89
+ let updatedObject = {
90
+ firstname : req . body . firstname ,
91
+ lastname : req . body . lastname ,
92
+ address : req . body . address ,
93
+ age : req . body . age
94
+ }
95
+ let result = await Customer . update ( updatedObject ,
96
+ {
97
+ returning : true ,
98
+ where : { id : req . body . id } ,
99
+ attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' , 'copyright' ]
100
+ }
101
+ ) ;
102
+
103
+ // return the response to client
104
+ if ( ! result ) {
105
+ res . status ( 500 ) . json ( {
106
+ message : "Error -> Can not update a customer with id = " + req . params . id ,
107
+ error : "Can NOT Updated" ,
108
+ } ) ;
109
+ }
110
+
111
+ res . status ( 200 ) . json ( result ) ;
112
+ }
113
+ } catch ( error ) {
114
+ res . status ( 500 ) . json ( {
115
+ message : "Error -> Can not update a customer with id = " + req . params . id ,
116
+ error : error . message
117
+ } ) ;
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Delete a Customer by ID
123
+ * @param {* } req
124
+ * @param {* } res
125
+ */
126
+ exports . deleteCustomer = async ( req , res ) => {
127
+ try {
128
+ let customerId = req . params . id ;
129
+ let customer = await Customer . findByPk ( customerId ) ;
130
+
131
+ if ( ! customer ) {
132
+ res . status ( 404 ) . json ( {
133
+ message : "Does Not exist a Customer with id = " + customerId ,
134
+ error : "404" ,
135
+ } ) ;
136
+ } else {
137
+ await customer . destroy ( ) ;
138
+ res . status ( 200 ) ;
139
+ }
140
+ } catch ( error ) {
141
+ res . status ( 500 ) . json ( {
142
+ message : "Error -> Can NOT delete a customer with id = " + req . params . id ,
143
+ error : error . message
144
+ } ) ;
145
+ }
146
+ }
0 commit comments