-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.js
337 lines (308 loc) · 10 KB
/
queries.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const Pool = require('pg').Pool
const bcrypt = require('bcrypt')
const pool = new Pool({
user: 'me',
host: 'localhost',
database: 'api',
password: 'password',
port: 5432,
})
const getUsers = (request, response) => {
pool.query('SELECT * FROM users ORDER BY id ASC', (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
const getAllDataTab2 = (request, response) => {
var user_email = request.headers['authorization']
pool.query('SELECT * FROM diary WHERE email = $1',[user_email], (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
const getAllDataTab1 = (request, response) => {
var user_email = request.headers['authorization']
//console.log('the header has value '+ user_email)
pool.query('SELECT * FROM tab1 WHERE email = $1',[user_email], (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
const getAllDataTab3 = (request, response) => {
var user_email = request.headers['authorization']
pool.query('SELECT * FROM tab3 WHERE email = $1',[user_email], (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
const createLineTab2 = (request, response) => {
var user_email = request.headers['authorization']
const { data, title, tag, evento } = request.body
pool.query('INSERT INTO diary (email, data, title, tag, evento) VALUES ($1, $2, $3, $4, $5)', [user_email, data, title, tag, evento], (error, results) => {
if (error) {
throw error
}
response.status(201).send(`Line created`)
})
}
const createLineTab1 = (request, response) => {
var user_email = request.headers['authorization']
const { data, title, tag, evento } = request.body
pool.query('INSERT INTO tab1 (email, data, title, tag, evento) VALUES ($1, $2, $3, $4, $5)', [user_email, data, title, tag, evento], (error, results) => {
if (error) {
throw error
}
response.status(201).send(`Line created`)
})
}
const createLineTab3 = (request, response) => {
var user_email = request.headers['authorization']
const { data, title, tag, evento } = request.body
pool.query('INSERT INTO tab3 (email, data, title, tag, evento) VALUES ($1, $2, $3, $4, $5)', [user_email, data, title, tag, evento], (error, results) => {
if (error) {
throw error
}
response.status(201).send(`Line created`)
})
}
const deleteLineTab2 = (request, response) => {
var user_email = request.headers['authorization']
//const id = parseInt(request.params.id)
var data = request.params.data
var title = request.params.title
if(title === undefined) title = ""
console.log('deleting line from tab2 with data = ' + data + ' and title = ' + title)
pool.query('DELETE FROM diary WHERE data = $1 AND title = $2 AND email = $3', [data, title, user_email], (error, results) => {
if (error) {
throw error
}
response.status(200).send(`Line deleted`)
})
}
const deleteLineTab1 = (request, response) => {
var user_email = request.headers['authorization']
//const id = parseInt(request.params.id)
var data = request.params.data
var title = request.params.title
if(title === undefined) title = ""
console.log('deleting line from tab1 with data = ' + data + ' and title = ' + title)
pool.query('DELETE FROM tab1 WHERE data = $1 AND title = $2 AND email = $3', [data, title, user_email], (error, results) => {
if (error) {
throw error
}
response.status(200).send(`Line deleted`)
})
}
const deleteLineTab3 = (request, response) => {
var user_email = request.headers['authorization']
//const id = parseInt(request.params.id)
var data = request.params.data
var title = request.params.title
if(title === undefined) title = ""
console.log('deleting line from tab3 with data = ' + data + ' and title = ' + title)
pool.query('DELETE FROM tab3 WHERE data = $1 AND title = $2 AND email = $3', [data, title, user_email], (error, results) => {
if (error) {
throw error
}
response.status(200).send(`Line deleted`)
})
}
const updateLineTab2 = (request, response) => {
var user_email = request.headers['authorization']
var old_data = request.params.data
var old_title = request.params.title
const { title, tag } = request.body
if(old_title === undefined) old_title = ""
if(title === undefined) title = ""
console.log('updating line from data = ' + old_data + ' and title = ' + old_title)
console.log('to new line with title = ' + title + ' and tag = ' + tag)
pool.query(
'UPDATE diary SET title = $1, tag = $2 WHERE data = $3 AND title = $4 AND email = $5',[title, tag, old_data, old_title, user_email],
(error, results) => {
if (error) {
throw error
}
response.status(200).send(`Line modified`)
}
)
}
const updateDataLineTab1 = (request, response) => {
var user_email = request.headers['authorization']
var old_data = request.params.data
var old_title = request.params.title
const {data} = request.body
if(old_title === undefined) old_title = ""
console.log('updating line from data = ' + old_data + ' and title = ' + old_title)
console.log('to new line with data = ' + data)
pool.query(
'UPDATE tab1 SET data = $1 WHERE data = $2 AND title = $3 AND email = $4',[data, old_data, old_title, user_email],
(error, results) => {
if (error) {
throw error
}
response.status(200).send(`Line modified`)
}
)
}
const updateTitleLineTab1 = (request, response) => {
var user_email = request.headers['authorization']
var old_data = request.params.data
var old_title = request.params.title
const {title} = request.body
if(old_title === undefined) old_title = ""
if(title === undefined) title = ""
console.log('updating line from data = ' + old_data + ' and title = ' + old_title)
console.log('to new line with title = ' + title)
pool.query(
'UPDATE tab1 SET title = $1 WHERE data = $2 AND title = $3 AND email = $4',[title, old_data, old_title, user_email],
(error, results) => {
if (error) {
throw error
}
response.status(200).send(`Line modified`)
}
)
}
const updateLineTab3 = (request, response) => {
var user_email = request.headers['authorization']
var old_data = request.params.data
var old_title = request.params.title
const { title, data } = request.body
if(old_title === undefined) old_title = ""
if(title === undefined) title = ""
console.log('updating line from data = ' + old_data + ' and title = ' + old_title)
console.log('to new line with title = ' + title + ' and data = ' + data)
pool.query(
'UPDATE tab3 SET title = $1, data = $2 WHERE data = $3 AND title = $4 AND email = $5',[title, data, old_data, old_title, user_email],
(error, results) => {
if (error) {
throw error
}
response.status(200).send(`Line modified`)
}
)
}
const loginUser = (request, response) => {
const { name, email, password } = request.body
pool.query('SELECT email,password FROM users WHERE email = $1', [email], (error, results) => {
if(error){
throw error
}
if(results.rows[0]){
var userData = results.rows[0]
var pwd = bcrypt.hashSync(password,5)/*.substring(0,6)*/;
console.log('the hashed password is ' + pwd)
console.log('the stored hashed password is '+ userData.password/*.substring(0,6)*/)
if(bcrypt.compareSync(password,userData.password)){
console.log('email and password matches!')
console.log('Logging the user in!')
response.status(201).send('')
}
else{
console.log('Login failed!')
console.log('Wrong password!!!')
response.status(400).send(`Wrong password!`)
}
}
else{
console.log('Login failed!')
console.log('Wrong email!')
response.status(400).send(`Wrong email!`)
}
}
)
}
const registerUser = (request, response) => {
const { name, email, password } = request.body
pool.query('SELECT id FROM users WHERE email = $1', [email], (error, results) => {
if(error){
throw error
}
if(results.rows[0]){
console.log('User with email '+ email + ' already exist')
response.status(400).send(`User with that EMAIL already exist`)
}
else{
var pwd = bcrypt.hashSync(password,5);
console.log('Registering user: ' + email)
pool.query('INSERT INTO users (name, email, password) VALUES ($1, $2, $3)', [name, email, pwd], (error, results) => {
if (error) {
throw error
}
response.status(201).send(`User added with ID: ${results.insertId}`)
})
}
}
)
}
const getUserById = (request, response) => {
const id = parseInt(request.params.id)
pool.query('SELECT * FROM users WHERE id = $1', [id], (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
const createUser = (request, response) => {
const { name, email } = request.body
pool.query('INSERT INTO users (name, email) VALUES ($1, $2)', [name, email], (error, results) => {
if (error) {
throw error
}
response.status(201).send(`User added with ID: ${result.insertId}`)
})
}
const updateUser = (request, response) => {
const id = parseInt(request.params.id)
const { name, email } = request.body
pool.query(
'UPDATE users SET name = $1, email = $2 WHERE id = $3',
[name, email, id],
(error, results) => {
if (error) {
throw error
}
response.status(200).send(`User modified with ID: ${id}`)
}
)
}
const deleteUser = (request, response) => {
const id = parseInt(request.params.id)
pool.query('DELETE FROM users WHERE id = $1', [id], (error, results) => {
if (error) {
throw error
}
response.status(200).send(`User deleted with ID: ${id}`)
})
}
module.exports = {
getUsers,
getAllDataTab2,
getAllDataTab1,
getAllDataTab3,
createLineTab2,
createLineTab1,
createLineTab3,
deleteLineTab2,
deleteLineTab1,
deleteLineTab3,
updateLineTab2,
updateDataLineTab1,
updateTitleLineTab1,
updateLineTab3,
loginUser,
registerUser,
getUserById,
createUser,
updateUser,
deleteUser,
}