1+ const Customer = require ( '../models/customer.model.js' ) ;
2+
3+ // POST a Customer
4+ exports . create = ( req , res ) => {
5+
6+ const customer = new Customer ( {
7+ firstname : req . body . firstname ,
8+ lastname : req . body . lastname ,
9+ age : req . body . age ,
10+ salary : req . body . salary ,
11+ address : req . body . address ,
12+ } ) ;
13+
14+ // Save a Customer in the MongoDB
15+ customer . save ( ) . then ( data => {
16+ // send uploading message to client
17+ res . status ( 200 ) . json ( {
18+ message : "Upload Successfully a Customer to MongoDB with id = " + data . id ,
19+ customer : data ,
20+ } ) ;
21+ } ) . catch ( err => {
22+ res . status ( 500 ) . json ( {
23+ message : "Fail!" ,
24+ error : err . message
25+ } ) ;
26+ } ) ;
27+ } ;
28+
29+ exports . getSalaries = ( req , res ) => {
30+ try {
31+ Customer . find ( { } ) . distinct ( "salary" )
32+ . then ( result => {
33+ let salaries = result . sort ( function ( a , b ) { return b - a } ) ;
34+ res . send ( salaries ) ;
35+ } ) ;
36+ } catch ( error ) {
37+ res . status ( 500 ) . send ( {
38+ message : "Error -> Can NOT get all customer's salaries" ,
39+ error : error . message
40+ } ) ;
41+ }
42+ }
43+
44+ exports . paginationfilterandsort = async ( req , res ) => {
45+ try {
46+ let page = parseInt ( req . query . page ) ;
47+ let limit = parseInt ( req . query . size ) ;
48+ let agesorting = ( req . query . agesorting === 'true' ) ;
49+ let desc = ( req . query . desc === 'true' ) ;
50+ let salary = req . query . salary ? parseInt ( req . query . salary ) : - 1 ;
51+
52+ const offset = page ? page * limit : 0 ;
53+
54+ console . log ( "offset = " + offset ) ;
55+
56+ let result = { } ;
57+ let numOfCustomer ;
58+
59+ // NOT Filtering with salary
60+ if ( salary < 0 ) {
61+ numOfCustomer = await Customer . countDocuments ( { } ) ;
62+ // not sorting with age
63+ if ( agesorting == false ) {
64+ result = await Customer . find ( { } ) // You may want to add a query
65+ . skip ( offset ) // Always apply 'skip' before 'limit'
66+ . limit ( limit )
67+ . select ( "-__v" ) ; // This is your 'page size'
68+ } else {
69+ if ( desc == false ) { // sorting with age and ascending
70+ result = await Customer . find ( { } ) // You may want to add a query
71+ . skip ( offset ) // Always apply 'skip' before 'limit'
72+ . limit ( limit )
73+ . sort ( { "age" : 1 } )
74+ . select ( "-__v" ) ; // This is your 'page size'
75+ } else { // sorting with age and descending
76+ result = await Customer . find ( { } ) // You may want to add a query
77+ . skip ( offset ) // Always apply 'skip' before 'limit'
78+ . limit ( limit )
79+ . sort ( { "age" : - 1 } )
80+ . select ( "-__v" ) ; // This is your 'page size'
81+ }
82+ }
83+ } else { // Filtering with salary
84+
85+ numOfCustomer = await Customer . countDocuments ( { salary : salary } ) ;
86+ // not sorting with age
87+ if ( agesorting == false ) {
88+ if ( desc == false ) { // sorting with age and ascending
89+ result = await Customer . find ( { salary : salary } ) // You may want to add a query
90+ . skip ( offset ) // Always apply 'skip' before 'limit'
91+ . limit ( limit )
92+ . select ( "-__v" ) ; // This is your 'page size'
93+ }
94+ } else {
95+ if ( desc == false ) { // sorting with age and ascending
96+ result = await Customer . find ( { salary : salary } ) // You may want to add a query
97+ . skip ( offset ) // Always apply 'skip' before 'limit'
98+ . limit ( limit )
99+ . sort ( { "age" : 1 } )
100+ . select ( "-__v" ) ; // This is your 'page size'
101+ } else { // sorting with age and descending
102+ result = await Customer . find ( { salary : salary } ) // You may want to add a query
103+ . skip ( offset ) // Always apply 'skip' before 'limit'
104+ . limit ( limit )
105+ . sort ( { "age" : - 1 } )
106+ . select ( "-__v" ) ; // This is your 'page size'
107+ }
108+ }
109+ }
110+
111+ const response = {
112+ "copyrightby" : "https://loizenai.com" ,
113+ "totalItems" : numOfCustomer ,
114+ "totalPages" : Math . ceil ( numOfCustomer / limit ) ,
115+ "pageNumber" : page ,
116+ "pageSize" : result . length ,
117+ "customers" : result
118+ } ;
119+
120+ res . status ( 200 ) . json ( response ) ;
121+ } catch ( error ) {
122+ res . status ( 500 ) . send ( {
123+ message : "Error -> Can NOT complete a paging request!" ,
124+ error : error . message ,
125+ } ) ;
126+ }
127+ } ;
0 commit comments