1
+ export class ApiFeatures {
2
+ constructor ( mongooseQuery , queryString ) {
3
+ this . mongooseQuery = mongooseQuery ;
4
+ this . queryString = queryString ;
5
+ }
6
+
7
+ pagination ( ) {
8
+ const PAGE_LIMIT = 3 ;
9
+ let PAGE_NUMBER = this . queryString . page * 1 || 1 ;
10
+ if ( this . queryString . page <= 0 ) PAGE_NUMBER = 1 ;
11
+ const PAGE_SKIP = ( PAGE_NUMBER - 1 ) * PAGE_LIMIT ;
12
+
13
+ this . mongooseQuery . skip ( PAGE_SKIP ) . limit ( PAGE_LIMIT ) ;
14
+ return this ;
15
+ }
16
+
17
+ filteration ( ) {
18
+ let filterObj = { ...this . queryString } ;
19
+
20
+ let excludedQuery = [ "page" , "sort" , "fields" , "keyword" ] ;
21
+
22
+ excludedQuery . forEach ( ( ele ) => {
23
+ delete filterObj [ ele ] ;
24
+ } ) ;
25
+ filterObj = JSON . stringify ( filterObj ) ;
26
+
27
+ filterObj = filterObj . replace (
28
+ / \b ( g t | g t e | l t | l t e ) \b / g,
29
+ ( match ) => `$${ match } `
30
+ ) ;
31
+ filterObj = JSON . parse ( filterObj ) ;
32
+
33
+ this . mongooseQuery . find ( filterObj ) ;
34
+ return this ;
35
+ }
36
+
37
+ sort ( ) {
38
+ if ( this . queryString . sort ) {
39
+ let sortedBy = this . queryString . sort . split ( "," ) . join ( " " ) ;
40
+ this . mongooseQuery . sort ( sortedBy ) ;
41
+ }
42
+ return this ;
43
+ }
44
+
45
+ search ( ) {
46
+ if ( this . queryString . keyword ) {
47
+
48
+ this . mongooseQuery . find ( {
49
+ $or : [
50
+ { title : { $regex : this . queryString . keyword , $options : "i" } } ,
51
+ { descripton : { $regex : this . queryString . keyword , $options : "i" } } ,
52
+ ] ,
53
+ } ) ;
54
+ }
55
+ return this ;
56
+ }
57
+
58
+
59
+ fields ( ) {
60
+ if ( this . queryString . fields ) {
61
+ let fields = this . queryString . fields . split ( "," ) . join ( " " ) ;
62
+ console . log ( fields ) ;
63
+ }
64
+ return this ;
65
+ }
66
+ }
0 commit comments