File tree 3 files changed +79
-8
lines changed
3 files changed +79
-8
lines changed Original file line number Diff line number Diff line change @@ -11,8 +11,9 @@ class AulianzaController {
11
11
}
12
12
13
13
async index ( req , res ) {
14
+ const result = await this . aulianzaService . index ( req . query ) ;
14
15
res . send ( {
15
- data : await this . aulianzaService . index ( )
16
+ result
16
17
} ) ;
17
18
}
18
19
Original file line number Diff line number Diff line change @@ -6,14 +6,55 @@ class AulianzaModel {
6
6
this . dbService = new DBService ( ) ;
7
7
}
8
8
9
- // index function
10
- async index ( ) {
11
- // intial query from database
12
- const query = `SELECT * FROM ${ this . table } WHERE is_deleted = 0` ;
13
- // return query
9
+ async index (
10
+ offset = 0 ,
11
+ limit = 10 ,
12
+ minAge ,
13
+ maxAge ,
14
+ search ,
15
+ sort_by = "id" ,
16
+ order = "DESC"
17
+ ) {
18
+ let query = `SELECT *
19
+ FROM ${ this . table }
20
+ WHERE is_deleted = 0` ;
21
+
22
+ if ( minAge ) {
23
+ query += ` AND age >= ${ minAge } ` ;
24
+ }
25
+
26
+ if ( maxAge ) {
27
+ query += ` AND age <= ${ maxAge } ` ;
28
+ }
29
+
30
+ if ( search ) {
31
+ query += ` AND name LIKE '%${ search } %'` ;
32
+ }
33
+
34
+ query += ` ORDER BY ${ sort_by } ${ order }
35
+ LIMIT ${ offset } , ${ limit } ` ;
14
36
return await this . dbService . query ( query ) ;
15
37
}
16
38
39
+ async getTotalUser ( minAge , maxAge , search ) {
40
+ let query = `SELECT COUNT(id) AS total_user FROM ${ this . table } WHERE is_deleted=0` ;
41
+
42
+ if ( minAge ) {
43
+ query += ` AND age >= ${ minAge } ` ;
44
+ }
45
+
46
+ if ( maxAge ) {
47
+ query += ` AND age <= ${ maxAge } ` ;
48
+ }
49
+
50
+ if ( search ) {
51
+ query += ` AND name LIKE '%${ search } %'` ;
52
+ }
53
+
54
+ const result = await this . dbService . query ( query ) ;
55
+ return result [ 0 ] . total_user ;
56
+ }
57
+
17
58
async getById ( id ) {
18
59
const query = `SELECT * FROM ${ this . table } WHERE id = ?` ;
19
60
return await this . dbService . query ( query , id ) ;
Original file line number Diff line number Diff line change @@ -20,8 +20,37 @@ class AulianzaServices {
20
20
} ;
21
21
}
22
22
23
- async index ( ) {
24
- return await this . aulianzaModel . index ( ) ;
23
+ async index ( query ) {
24
+ const offset = query . offset || 0 ;
25
+ const limit = query . limit || 10 ;
26
+ const minAge = query . min_age ;
27
+ const maxAge = query . max_age ;
28
+ const seacrh = query . q ;
29
+ const sortBy = query . sort_by ;
30
+ const order = query . order ;
31
+ const totalUser = await this . aulianzaModel . getTotalUser (
32
+ minAge ,
33
+ maxAge ,
34
+ seacrh
35
+ ) ;
36
+ const userData = await this . aulianzaModel . index (
37
+ offset ,
38
+ limit ,
39
+ minAge ,
40
+ maxAge ,
41
+ seacrh ,
42
+ sortBy ,
43
+ order
44
+ ) ;
45
+
46
+ return {
47
+ data : userData ,
48
+ pagination : {
49
+ total_item : totalUser ,
50
+ offset,
51
+ limit
52
+ }
53
+ } ;
25
54
}
26
55
27
56
async getById ( id ) {
You can’t perform that action at this time.
0 commit comments