1
+ /**
2
+ * Copyright by https://loizenai.com
3
+ * youtube loizenai
4
+ */
5
+
6
+ const db = require ( '../config/db.config.js' ) ;
7
+ const Customer = db . Customer ;
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
+ customer : result ,
25
+ } ) ;
26
+ } ) ;
27
+ } catch ( error ) {
28
+ res . status ( 500 ) . json ( {
29
+ message : "Fail!" ,
30
+ error : error . message
31
+ } ) ;
32
+ }
33
+ }
34
+
35
+ exports . pagination = ( req , res ) => {
36
+ try {
37
+ let page = parseInt ( req . query . page ) ;
38
+ let limit = parseInt ( req . query . limit ) ;
39
+
40
+ const offset = page ? page * limit : 0 ;
41
+
42
+ Customer . findAndCountAll ( { limit : limit , offset :offset } )
43
+ . then ( data => {
44
+ const totalPages = Math . ceil ( data . count / limit ) ;
45
+ const response = {
46
+ message : "Paginating is completed! Query parameters: page = " + page + ", limit = " + limit ,
47
+ data : {
48
+ "copyrightby" : "https://loizenai.com" ,
49
+ "totalItems" : data . count ,
50
+ "totalPages" : totalPages ,
51
+ "limit" : limit ,
52
+ "currentPageNumber" : page + 1 ,
53
+ "currentPageSize" : data . rows . length ,
54
+ "customers" : data . rows
55
+ }
56
+ } ;
57
+ res . send ( response ) ;
58
+ } ) ;
59
+ } catch ( error ) {
60
+ res . status ( 500 ) . send ( {
61
+ message : "Error -> Can NOT complete a paging request!" ,
62
+ error : error . message ,
63
+ } ) ;
64
+ }
65
+ }
66
+
67
+ exports . filteringByAge = ( req , res ) => {
68
+ let age = req . query . age ;
69
+
70
+ Customer . findAll ( {
71
+ attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' , 'copyrightby' ] ,
72
+ where : { age : age }
73
+ } )
74
+ . then ( results => {
75
+ res . status ( 200 ) . json ( {
76
+ message : "Get all Customers with age = " + age ,
77
+ totalItems : results . length ,
78
+ customers : results ,
79
+ } ) ;
80
+ } )
81
+ . catch ( error => {
82
+ console . log ( error ) ;
83
+ res . status ( 500 ) . json ( {
84
+ message : "Error!" ,
85
+ error : error
86
+ } ) ;
87
+ } ) ;
88
+ }
89
+
90
+ exports . pagingfilteringsorting = ( req , res ) => {
91
+ try {
92
+ let page = parseInt ( req . query . page ) ;
93
+ let limit = parseInt ( req . query . limit ) ;
94
+ let age = parseInt ( req . query . age ) ;
95
+
96
+ const offset = page ? page * limit : 0 ;
97
+
98
+ console . log ( "offset = " + offset ) ;
99
+
100
+ Customer . findAndCountAll ( {
101
+ attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' ] ,
102
+ where : { age : age } ,
103
+ order : [
104
+ [ 'firstname' , 'ASC' ] ,
105
+ [ 'lastname' , 'DESC' ]
106
+ ] ,
107
+ limit : limit ,
108
+ offset :offset
109
+ } )
110
+ . then ( data => {
111
+ const totalPages = Math . ceil ( data . count / limit ) ;
112
+ const response = {
113
+ message : "Pagination Filtering Sorting request is completed! Query parameters: page = " + page + ", limit = " + limit + ", age = " + age ,
114
+ data : {
115
+ "copyrightby" : "https://loizenai.com" ,
116
+ "totalItems" : data . count ,
117
+ "totalPages" : totalPages ,
118
+ "limit" : limit ,
119
+ "age-filtering" : age ,
120
+ "currentPageNumber" : page + 1 ,
121
+ "currentPageSize" : data . rows . length ,
122
+ "customers" : data . rows
123
+ }
124
+ } ;
125
+ res . send ( response ) ;
126
+ } ) ;
127
+ } catch ( error ) {
128
+ res . status ( 500 ) . send ( {
129
+ message : "Error -> Can NOT complete a paging request!" ,
130
+ error : error . message ,
131
+ } ) ;
132
+ }
133
+ }
0 commit comments