forked from deic-dk-retired/ddosapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.js
More file actions
104 lines (98 loc) · 2.32 KB
/
rules.js
File metadata and controls
104 lines (98 loc) · 2.32 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
// connect to db and then get the
// query handler method
var db = require('./db')
function getRules (req, res, next) {
var sqlGetAllRules = db.miniQuery('.sql/rules/allRules.sql')
// var rows = parseInt(req.params.rows)
// var offset = parseInt(req.params.offset)
// db.foddb.any(sqlGetAllRules, {rows: rows, offset: offset})
db.foddb.any(sqlGetAllRules)
.then(function (data) {
// json api
var jsonarr = []
var jsonobj
data.map(function (e) {
jsonobj = {
type: 'rules',
id: parseInt(e.id)
}
// remove duplicate id property from attributes
delete e.id
jsonobj.attributes = e
jsonarr.push(jsonobj)
})
res.status(200)
.json({
data: jsonarr,
meta: {
total: data.length
}
})
})
.catch(function (err) {
return next(err.message)
})
}
function getRuleById (req, res, next) {
var ruleId = parseInt(req.params.id)
var sqlRuleByID = db.miniQuery('.sql/rules/RuleById.sql')
db.foddb.one(sqlRuleByID, {id: ruleId})
.then(function (data) {
res.status(200)
.json({
data: {
type: 'rules',
id: parseInt(data.id),
attributes: data
}
})
})
.catch(function (err) {
return next(err.message)
})
}
function getRuleDetail (req, res, next) {
var sqlRuleDetail = db.miniQuery('.sql/rules/RuleDetail.sql')
db.foddb.any(sqlRuleDetail,
{ prot: req.params.prot,
dest: req.params.dest,
action: req.params.action,
isexp: req.params.isexp,
isact: req.params.isact,
vfrom: req.params.vfrom,
vto: req.params.vto})
.then(function (data) {
res.status(200)
.json({
rules: data,
meta: {
total: data.length
}
})
})
.catch(function (err) {
return next(err.message)
})
}
/**
* create a rule with form parameters
*/
function createRule (req, res, next) {
db.foddb.none('', req.body)
.then(function () {
res.status(201)
.json({
status: 'success',
message: 'Inserted one rule'
})
})
.catch(function (err) {
return next(err.message)
})
}
module.exports = {
getRules: getRules,
getRuleById: getRuleById,
getRuleDetail: getRuleDetail,
createRule: createRule
}