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+ const Sequelize = db . Sequelize ;
9+
10+
11+ exports . create = ( req , res ) => {
12+ let customer = { } ;
13+
14+ try {
15+ // Building Customer object from upoading request's body
16+ customer . firstname = req . body . firstname ;
17+ customer . lastname = req . body . lastname ;
18+ customer . address = req . body . address ;
19+ customer . age = req . body . age ;
20+ customer . salary = req . body . salary ;
21+
22+ // Save to MySQL database
23+ Customer . create ( customer ) . then ( result => {
24+ // send uploading message to client
25+ res . status ( 200 ) . json ( {
26+ message : "Upload Successfully a Customer with id = " + result . id ,
27+ customer : result ,
28+ } ) ;
29+ } ) ;
30+ } catch ( error ) {
31+ res . status ( 500 ) . json ( {
32+ message : "Fail!" ,
33+ error : error . message
34+ } ) ;
35+ }
36+ }
37+
38+ exports . pagingfilteringsorting = async ( req , res ) => {
39+ try {
40+ let page = parseInt ( req . query . page ) ;
41+ let limit = parseInt ( req . query . size ) ;
42+ let agesorting = ( req . query . agesorting === 'true' ) ;
43+ let desc = ( req . query . desc === 'true' ) ;
44+ let salary = req . query . salary ? parseInt ( req . query . salary ) : - 1 ;
45+
46+ const offset = page ? page * limit : 0 ;
47+
48+ console . log ( "offset = " + offset ) ;
49+
50+ let result = { } ;
51+
52+ // NOT Filtering with salary
53+ if ( salary < 0 ) {
54+ // not sorting with age
55+ if ( agesorting == false ) {
56+ result = await Customer . findAndCountAll ( {
57+ attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' , 'salary' , 'copyrightby' ] ,
58+ limit : limit ,
59+ offset :offset
60+ } ) ;
61+ } else {
62+ if ( desc == false ) { // sorting with age and ascending
63+ result = await Customer . findAndCountAll ( {
64+ attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' , 'salary' , 'copyrightby' ] ,
65+ limit : limit ,
66+ offset :offset ,
67+ order : [
68+ [ 'age' , 'ASC' ]
69+ ]
70+ } ) ;
71+ } else { // sorting with age and descending
72+ result = await Customer . findAndCountAll ( {
73+ attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' , 'salary' , 'copyrightby' ] ,
74+ limit : limit ,
75+ offset :offset ,
76+ order : [
77+ [ 'age' , 'DESC' ]
78+ ]
79+ } ) ;
80+ }
81+ }
82+ } else { // Filtering with salary
83+ // not sorting with age
84+ if ( agesorting == false ) {
85+ result = await Customer . findAndCountAll ( {
86+ attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' , 'salary' , 'copyrightby' ] ,
87+ where : { salary : salary } ,
88+ limit : limit ,
89+ offset :offset
90+ } ) ;
91+ } else {
92+ if ( desc == false ) { // sorting with age and ascending
93+ result = await Customer . findAndCountAll ( {
94+ attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' , 'salary' , 'copyrightby' ] ,
95+ where : { salary : salary } ,
96+ limit : limit ,
97+ offset :offset ,
98+ order : [
99+ [ 'age' , 'ASC' ]
100+ ]
101+ } ) ;
102+ } else { // sorting with age and descending
103+ result = await Customer . findAndCountAll ( {
104+ attributes : [ 'id' , 'firstname' , 'lastname' , 'age' , 'address' , 'salary' , 'copyrightby' ] ,
105+ where : { salary : salary } ,
106+ limit : limit ,
107+ offset :offset ,
108+ order : [
109+ [ 'age' , 'DESC' ]
110+ ]
111+ } ) ;
112+ }
113+ }
114+ }
115+
116+ const totalPages = Math . ceil ( result . count / limit ) ;
117+ const response = {
118+ "copyrightby" : "https://loizenai.com" ,
119+ "totalPages" : totalPages ,
120+ "pageNumber" : page ,
121+ "pageSize" : result . rows . length ,
122+ "customers" : result . rows
123+ } ;
124+ res . send ( response ) ;
125+ } catch ( error ) {
126+ res . status ( 500 ) . send ( {
127+ message : "Error -> Can NOT complete a paging request!" ,
128+ error : error . message ,
129+ } ) ;
130+ }
131+ }
132+
133+ exports . getSalaries = ( req , res ) => {
134+ Customer . findAll ( {
135+ attributes : [
136+ [ Sequelize . fn ( 'DISTINCT' , Sequelize . col ( 'salary' ) ) , 'salary' ] ,
137+ ] ,
138+ order : [
139+ [ 'salary' , 'ASC' ]
140+ ] ,
141+ } ) . then ( result => {
142+ let salaries = result . map ( x => x . salary ) ;
143+ res . send ( salaries ) ;
144+ } ) . catch ( error => {
145+ res . status ( 500 ) . send ( {
146+ message : "Error -> Can NOT get all customer's salaries" ,
147+ error : error . message
148+ } ) ;
149+ } ) ;
150+ }
0 commit comments