-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
116 lines (98 loc) · 3.88 KB
/
db.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
const spicedPg = require("spiced-pg");
const db = spicedPg(
process.env.DATABASE_URL || "postgres:leo@localhost/petition"
);
const capitalize = (str) => str.replace(/^\w/, (c) => c.toUpperCase());
module.exports.listUsers = () =>
db.query("SELECT * FROM users").then((result) => result.rows);
module.exports.listSignatures = (city) => {
let queryString =
"SELECT users.first, users.last, " +
"user_profiles.age, user_profiles.city, user_profiles.url " +
"FROM users " +
"INNER JOIN signatures ON users.id = signatures.user_id " +
"LEFT OUTER JOIN user_profiles ON users.id = user_profiles.user_id ";
if (city) queryString += "WHERE user_profiles.city = $1";
return db
.query(queryString, city ? [city] : null)
.then((result) => result.rows);
};
module.exports.addUser = ({ first, last, email, password }) =>
db
.query(
"INSERT INTO users (first, last, email, password) " +
"VALUES ($1, $2, $3, $4) " +
"RETURNING id, first",
[capitalize(first), capitalize(last), email, password]
)
.then((result) => result.rows);
module.exports.deleteUser = (userId) =>
Promise.all(
db.query("DELETE FROM signatures WHERE user_id = $1", [userId]),
db.query("DELETE FROM user_profiles WHERE user_id = $1", [userId])
).then(db.query("DELETE FROM users WHERE user_id = $1", [userId]));
module.exports.updateUserData = (userId, { first, last, email }) =>
db.query(
"UPDATE users SET (first, last, email) = ($2, $3, $4) WHERE id = $1",
[userId, capitalize(first), capitalize(last), email]
);
module.exports.getUserBy = (parameter, value) => {
return db
.query(`SELECT * FROM users WHERE ${parameter} = $1`, [value])
.then((result) => result.rows);
};
module.exports.getPassword = (email) =>
db
.query("SELECT password FROM users WHERE email = $1", [email])
.then((result) => result.rows);
module.exports.addSignature = ({ userId, signature }) =>
db
.query(
"INSERT INTO signatures (user_id, signature) " +
"VALUES ($1, $2) " +
"RETURNING id",
[userId, signature]
)
.then((result) => result.rows);
module.exports.getSignatureId = (userId) =>
db
.query("SELECT id FROM signatures WHERE user_id = $1", [userId])
.then((result) => result.rows);
module.exports.getSignature = (userId) =>
db
.query("SELECT signature FROM signatures WHERE user_id = $1", [userId])
.then((result) => result.rows);
module.exports.deleteSignature = (userId) =>
db.query("DELETE FROM signatures WHERE user_id = $1", [userId]);
module.exports.addInfo = (userId, { age, city, url }) =>
db.query(
"INSERT INTO user_profiles (user_id, age, city, url) " +
"VALUES ($1, $2, $3, $4) " +
"ON CONFLICT (user_id) " +
"DO UPDATE SET (age, city, url) = ($2, $3, $4)",
[userId, age, capitalize(city), url]
);
module.exports.addToProfile = (userId, parameter, value) =>
db.query(
`INSERT INTO user_profiles (user_id, ${parameter}) ` +
"VALUES ($1, $2) " +
"ON CONFLICT (user_id) " +
`DO UPDATE SET ${parameter} = $2`,
[userId, value]
);
module.exports.getData = (userId) =>
db
.query(
"SELECT users.first, users.last, users.email, " +
"user_profiles.age, user_profiles.city, user_profiles.url " +
"FROM users " +
"LEFT OUTER JOIN user_profiles ON users.id = user_profiles.user_id " +
"WHERE users.id = $1",
[userId]
)
.then((result) => result.rows);
module.exports.updatePassword = (userId, password) =>
db.query("UPDATE users SET password = $2 WHERE id = $1", [
userId,
password,
]);