-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
146 lines (128 loc) · 3.61 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
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
const spicedPg = require("spiced-pg");
const db = spicedPg(
process.env.DATABASE_URL ||
`postgres:postgres:postgres@localhost:5432/petition`
);
module.exports.addSignature = (signature, user_id) => {
return db.query(
`INSERT INTO signatures (signature, user_id)
VALUES ($1, $2)
RETURNING id`,
[signature, user_id]
);
};
module.exports.getNumber = () => {
return db.query("SELECT COUNT(*) FROM signatures");
};
module.exports.addUser = (first, last, email, password) => {
return db.query(
`INSERT INTO users (first, last, email, password)
VALUES ($1, $2, $3, $4)
RETURNING id`,
[first, last, email, password]
);
};
module.exports.getHashPassword = email => {
return db.query(`SELECT password FROM users WHERE email = $1`, [email]);
};
module.exports.getLoginUserId = loginId => {
return db.query(
`SELECT id
FROM users
WHERE email = $1`,
[loginId]
);
};
module.exports.addProfile = (age, city, url, user_id) => {
return db.query(
`INSERT INTO user_profiles (age, city, url, user_id)
VALUES ($1, $2, $3, $4)
RETURNING id`,
[age || null, city, url, user_id]
);
};
module.exports.getSignature = signId => {
return db.query(
`SELECT signature, first
FROM signatures
LEFT JOIN users
ON users.id = signatures.user_id
WHERE user_id = $1
`,
[signId]
);
};
module.exports.getSigners = () => {
return db.query(
`SELECT first, last, age, city, url
FROM signatures
LEFT JOIN users
ON users.id = signatures.user_id
LEFT JOIN user_profiles
ON users.id = user_profiles.user_id
`
);
};
module.exports.sigCheck = id => {
return db.query("SELECT user_id FROM signatures WHERE user_id = $1", [id]);
};
module.exports.getSignersCity = city => {
return db.query(
`SELECT first, last, age, city, url
FROM signatures
LEFT JOIN users
ON users.id = signatures.user_id
LEFT JOIN user_profiles
ON users.id = user_profiles.user_id
WHERE LOWER(city) = LOWER($1)
`,
[city]
);
};
module.exports.showProfile = user_id => {
return db.query(
`SELECT first, last, email, password, age, city, url
FROM users
LEFT JOIN user_profiles
ON users.id = user_profiles.user_id
WHERE users.id = $1
`,
[user_id]
);
};
module.exports.editProfile = (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 || null, city, url, user_id]
);
};
module.exports.editPassword = (first, last, email, password, user_id) => {
return db.query(
`UPDATE users SET first = $1, last = $2, email = $3, password = $4
WHERE id =$5
`,
[first, last, email, password, user_id]
);
};
module.exports.editUsers = (first, last, email, user_id) => {
return db.query(
`
UPDATE users SET first = $1, last = $2, email = $3
WHERE id = $4
`,
[first, last, email, user_id]
);
};
module.exports.delSignature = id => {
return db.query("DELETE FROM signatures WHERE user_id = $1", [id]);
};
module.exports.delUserProfile = id => {
return db.query("DELETE FROM user_profiles WHERE user_id = $1", [id]);
};
module.exports.delUser = id => {
return db.query("DELETE FROM users CASCADE WHERE id = $1", [id]);
};