Repository navigation
Expand file tree
/
Copy pathcustomers.js
More file actions
163 lines (153 loc) · 3.97 KB
/
Copy pathcustomers.js
File metadata and controls
163 lines (153 loc) · 3.97 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// connect to db and then get the
// query handler method
var db = require('./db')
// uses json api conventions
function getAllCustomers (req, res, next) {
/*
#todo: get customer and customer networks
#todo: sideload relations
*/
var sqlAllCustomers = db.miniQuery('.sql/customers/allCustomers.sql')
db.foddb.any(sqlAllCustomers)
.then(function (data) {
// create json api array
var jsonarr = []
var jsonobj
data.map(function (e) {
jsonobj = {
type: 'customers',
id: parseInt(e.customerid)
}
// remove duplicate id property from attributes
delete e.customerid
jsonobj.attributes = e
jsonarr.push(jsonobj)
})
// show jsonapi
res.status(200)
.json({
data: jsonarr,
meta: {
total: data.length
}
})
})
.catch(function (err) {
console.error(err.stack)
return next(err.message)
})
}
// uses json api conventions
// // return all networks of all customers
function getAllNetworks (req, res, next) {
var sqlallNetworks = db.miniQuery('.sql/customers/allNetworks.sql')
db.foddb.any(sqlallNetworks)
.then(function (data) {
// create json api array
var jsonarr = []
var jsonobj
data.map(function (e) {
jsonobj = {
type: 'networks',
id: parseInt(e.customernetworkid)
}
// remove duplicate id property from attributes
delete e.customernetworkid
jsonobj.attributes = e
jsonarr.push(jsonobj)
})
// show jsonapi
res.status(200)
.json({
data: jsonarr,
meta: {
total: data.length
}
})
})
.catch(function (err) {
console.error(err.stack)
return next(err.message)
})
}
// uses json api conventions
// return all networks of a customer based on customerid
function getCustomerNetworks (req, res, next) {
var sqlCustomerNetworks = db.miniQuery('.sql/customers/customerNetworks.sql')
db.foddb.any(sqlCustomerNetworks, {customerid: req.params.customerid})
.then(function (data) {
// create json api array
var jsonarr = []
var jsonobj
data.map(function (e) {
jsonobj = {
type: 'networks',
id: parseInt(e.customernetworkid)
}
// remove duplicate id property from attributes
delete e.customernetworkid
jsonobj.attributes = e
jsonarr.push(jsonobj)
})
// show jsonapi
res.status(200)
.json({
data: jsonarr,
meta: {
total: data.length
}
})
})
.catch(function (err) {
console.error(err.stack)
return next(err.message)
})
}
// uses json api conventions
function getOneCustomer (req, res, next) {
/*
#todo: get customer and customer networks
#todo: sideload relations
*/
var sqlOneCustomer = db.miniQuery('.sql/customers/oneCustomer.sql')
db.foddb.one(sqlOneCustomer, {customerid: req.params.customerid})
.then(function (data) {
var jsonobj = {
type: 'customers',
id: parseInt(data.customerid)
}
// remove duplicate id property from attributes
delete data.customerid
jsonobj.attributes = data
// show jsonapi
res.status(200)
.json({
data: jsonobj
})
})
.catch(function (err) {
console.error(err.stack)
return next(err.message)
})
}
function createCustomer (req, res, next) {
// add customer info
// add customer networks
}
function updateCustomer (req, res, next) {
// update customer info
// update customer networks
}
function removeCustomer (req, res, next) {
// delete customer info
// delete customer networks
}
module.exports = {
getAllCustomers: getAllCustomers,
getAllNetworks: getAllNetworks,
getCustomerNetworks: getCustomerNetworks,
getOneCustomer: getOneCustomer,
createCustomer: createCustomer,
updateCustomer: updateCustomer,
removeCustomer: removeCustomer
}