Skip to content

Commit ac4575a

Browse files
authored
Merge pull request #57 from culacalo/feature/aulianza
Feature/aulianza
2 parents 97a8dc0 + b40585c commit ac4575a

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

modules/aulianza/controllers/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ class AulianzaController {
1111
}
1212

1313
async index(req, res) {
14+
const result = await this.aulianzaService.index(req.query);
1415
res.send({
15-
data: await this.aulianzaService.index()
16+
result
1617
});
1718
}
1819

modules/aulianza/models/index.js

+46-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,55 @@ class AulianzaModel {
66
this.dbService = new DBService();
77
}
88

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}`;
1436
return await this.dbService.query(query);
1537
}
1638

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+
1758
async getById(id) {
1859
const query = `SELECT * FROM ${this.table} WHERE id = ?`;
1960
return await this.dbService.query(query, id);

modules/aulianza/services/index.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,37 @@ class AulianzaServices {
2020
};
2121
}
2222

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+
};
2554
}
2655

2756
async getById(id) {

0 commit comments

Comments
 (0)