-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataService.js
More file actions
122 lines (108 loc) · 3.84 KB
/
Copy pathdataService.js
File metadata and controls
122 lines (108 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const mysql = require("mysql")
const sql = require("sql-query")
const dbConfigJson = require("./dbConfig.json")
const FiltersService = require("./filtersService")
const _ = require("lodash")
const filtersService = new FiltersService()
class DataService {
constructor() {
this.dbConfig = dbConfigJson
this.pool = mysql.createPool({
connectionLimit: this.dbConfig.connectionLimit,
host: this.dbConfig.host,
user: this.dbConfig.user,
password: this.dbConfig.password,
database: this.dbConfig.dbName
})
}
getAll(res) {
const query = sql.Query()
.select()
.from(this.dbConfig.geoObjectsTable)
.build()
this.usePoolQuery(res, query)
}
getCustom(res) {
const query = sql.Query()
.select()
.from(this.dbConfig.geoObjectsTable)
.where({ isCustom: 1 })
.build()
this.usePoolQuery(res, query)
}
getById(res, id) {
const query = sql.Query()
.select()
.from(this.dbConfig.geoObjectsTable)
.where({ id: id })
.build()
this.usePoolQuery(res, query, filtersService.getFirstResult)
}
getInRange(res, lat, lng, radius) {
if (lat === NaN || lng === NaN || radius === NaN) {
res.json({ "code": 100, "status": "Filters missing" })
return
}
this.usePoolQuery(res, `call GETINRADIUS(${lat}, ${lng}, ${radius})`, filtersService.getFirstResult)
}
filter(res, filters) {
const withGeoFilters = (filters.lat || filters.lng || filters.radius)
if (withGeoFilters && (!filters.lat || !filters.lng || !filters.radius)) {
res.json({ "code": 100, "status": "Filters missing" })
return
}
const postProcess = filtersService.getFiltersPredicate(filters.categories)
const query = withGeoFilters
? `call GETINRADIUS(${filters.lat}, ${filters.lng}, ${filters.radius})`
: sql.Query()
.select()
.from(this.dbConfig.geoObjectsTable)
.build()
this.usePoolQuery(res, query, postProcess)
}
add(res, parameters) {
const query = sql.Query()
.insert()
.into(this.dbConfig.geoObjectsTable)
.set({ name: parameters.name, categories: parameters.categories, lng: parameters.lng, lat: parameters.lat, isCustom: 1 })
.build()
console.log("query" + query)
this.usePoolQuery(res, query)
}
useConnection(res, sqlQuery) {
this.pool.getConnection((err, connection) => {
if (err) {
console.log(err)
console.log(connection)
res.json({ "code": 100, "status": "Can't establish database connection." })
return
}
connection.query(sqlQuery, (err, rows) => {
connection.release()
if (!err) {
const result = rows || "Nothing found"
res.json(result)
}
})
connection.on('error', (err) => {
res.json({ "code": 100, "status": "Error in connection database" })
return
})
})
}
usePoolQuery(res, sqlQuery, postProcess) {
this.pool.query(sqlQuery, (err, rows) => {
if (err) {
console.log(err)
res.json({ "error": true, "message": "Can't establish database connection." + err })
}
if (!rows) {
res.json("Nothing found")
} else {
const result = postProcess ? postProcess(JSON.parse(JSON.stringify(rows))) : rows
res.json(result)
}
})
}
}
module.exports = DataService