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