-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabaseRequests.js
126 lines (114 loc) · 3.49 KB
/
databaseRequests.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
var spicedPg = require("spiced-pg");
var db = spicedPg(
process.env.DATABASE_URL ||
"postgres:postgres:postgres@localhost:5432/petition"
);
exports.registration = function(first, last, email, hashpassword) {
return db.query(
"INSERT INTO users (first, last, email, hashpassword) VALUES ($1, $2, $3, $4) RETURNING id,first,last", //code it against sequel injections
[first, last, email, hashpassword]
);
};
exports.signing = function(user_id, signature) {
return db
.query(
"INSERT INTO signatures (user_id, signature) VALUES ($1, $2) RETURNING id", //code it against sequel injections
[user_id, signature]
)
.then(function(results) {
return results.rows[0].id;
});
};
exports.signersList = function() {
return db
.query(
`SELECT users.first, users.last, user_profiles.age, user_profiles.city, user_profiles.url
FROM users
LEFT JOIN user_profiles
ON users.id = user_profiles.user_id`
)
.then(function(results) {
return results.rows;
});
};
exports.getSignatureById = function(sigId) {
return db
.query("SELECT signature FROM signatures WHERE id=$1", [sigId])
.then(result => {
return result.rows[0].signature;
});
};
exports.getUserByEmail = function(email) {
return db
.query(
`SELECT users.id, users.first, users.last, users.email, users.hashpassword, signatures.id AS sigid
FROM users
LEFT JOIN signatures
ON users.id = signatures.user_id
WHERE email = $1 `,
[email]
)
.then(results => {
return results.rows[0];
});
};
exports.profileInformationToTable = function(userId, age, city, url) {
return db.query(
`INSERT INTO user_profiles (user_id, age, city, url) VALUES ($1, $2, $3, $4)`,
[userId, age ? Number(age) : null, city, url]
);
};
exports.getUsersRegistration = function(userId) {
return db
.query(
`SELECT users.first, users.last, users.email, user_profiles.age, user_profiles.city, user_profiles.url
FROM users
LEFT JOIN user_profiles
ON users.id = user_profiles.user_id
WHERE users.id = $1`,
[userId]
)
.then(function(results) {
return results.rows[0];
});
};
exports.updateProfileInUsersWithPass = function(
first,
last,
email,
hashpassword,
id
) {
return db.query(
`UPDATE users
SET first = $1, last = $2, email = $3, hashpassword =$4
WHERE id=$5
`,
[first, last, email, hashpassword, id || null]
);
};
exports.updateProfileInUsersNoPass = function(first, last, email, id) {
return db.query(
`UPDATE users
SET first = $1, last = $2, email = $3
WHERE id=$4
`,
[first, last, email, id || null]
);
};
exports.updateProfileInUserProfiles = function(age, city, url, user_id) {
return db.query(
`INSERT INTO user_profiles (age, city, url, user_id)
VALUES ($1, $2, $3, $4)
ON CONFLICT (user_id)
DO UPDATE SET age = $1, city = $2, url = $3`,
[age ? Number(age) : null, city, url, user_id]
);
};
exports.deleteSignature = function(userId) {
return db.query(
`DELETE FROM signatures
WHERE user_id = $1`,
[userId]
);
};