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 . create = ( 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 ) . then ( result => {
21+ // send uploading message to client
22+ res . status ( 200 ) . json ( {
23+ message : "Upload Successfully a Customer with id = " + result . id ,
24+ customers : [ result ] ,
25+ error : ""
26+ } ) ;
27+ } ) ;
28+ } catch ( error ) {
29+ res . status ( 500 ) . json ( {
30+ message : "Fail!" ,
31+ customers : [ ] ,
32+ error : error . message
33+ } ) ;
34+ }
35+ }
36+
37+ /**
38+ * Retrieve Customer information from database
39+ * @param {* } req
40+ * @param {* } res
41+ */
42+ exports . retrieveAllCustomers = ( req , res ) => {
43+ // find all Customer information from
44+ try {
45+ Customer . findAll ( { attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' ] } )
46+ . then ( customerInfos => {
47+ res . status ( 200 ) . json ( {
48+ message : "Get all Customers' Infos Successfully!" ,
49+ customers : customerInfos ,
50+ error : ""
51+ } ) ;
52+ } )
53+ } catch ( error ) {
54+ // log on console
55+ console . log ( error ) ;
56+
57+ res . status ( 500 ) . json ( {
58+ message : "Error!" ,
59+ customers : [ ] ,
60+ error : error
61+ } ) ;
62+ }
63+ }
64+
65+ /**
66+ * Updating a Customer
67+ * @param {* } req
68+ * @param {* } res
69+ */
70+ exports . updateById = async ( req , res ) => {
71+ try {
72+ let customerId = req . params . id ;
73+ let customer = await Customer . findByPk ( customerId ) ;
74+
75+ if ( ! customer ) {
76+ // return a response to client
77+ res . status ( 404 ) . json ( {
78+ message : "Not Found for updating a customer with id = " + customerId ,
79+ customers : [ ] ,
80+ error : "404"
81+ } ) ;
82+ } else {
83+ // update new change to database
84+ let updatedObject = {
85+ firstname : req . body . firstname ,
86+ lastname : req . body . lastname ,
87+ address : req . body . address ,
88+ age : req . body . age
89+ }
90+ let result = await Customer . update ( updatedObject , { returning : true , where : { id : customerId } } ) ;
91+
92+ // return the response to client
93+ if ( ! result ) {
94+ res . status ( 500 ) . json ( {
95+ message : "Error -> Can not update a customer with id = " + req . params . id ,
96+ error : "Can NOT Updated" ,
97+ customers : [ ]
98+ } ) ;
99+ }
100+
101+ res . status ( 200 ) . json ( {
102+ message : "Update successfully a Customer with id = " + customerId ,
103+ customers : [ updatedObject ] ,
104+ error : ""
105+ } ) ;
106+ }
107+ } catch ( error ) {
108+ res . status ( 500 ) . json ( {
109+ message : "Error -> Can not update a customer with id = " + req . params . id ,
110+ error : error . message ,
111+ customers : [ ]
112+
113+ } ) ;
114+ }
115+ }
116+
117+ /**
118+ * Delete a Customer by ID
119+ * @param {* } req
120+ * @param {* } res
121+ */
122+ exports . deleteById = async ( req , res ) => {
123+ try {
124+ let customerId = req . params . id ;
125+ let customer = await Customer . findByPk ( customerId ) ;
126+
127+ if ( ! customer ) {
128+ res . status ( 404 ) . json ( {
129+ message : "Does Not exist a Customer with id = " + customerId ,
130+ error : "404" ,
131+ customers : [ ]
132+ } ) ;
133+ } else {
134+ await customer . destroy ( ) ;
135+ res . status ( 200 ) . json ( {
136+ message : "Delete Successfully a Customer with id = " + customerId ,
137+ customers : [ customer ] ,
138+ error : ""
139+ } ) ;
140+ }
141+ } catch ( error ) {
142+ res . status ( 500 ) . json ( {
143+ message : "Error -> Can NOT delete a customer with id = " + req . params . id ,
144+ error : error . message ,
145+ customers : [ ]
146+ } ) ;
147+ }
148+ }
0 commit comments