-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
263 lines (241 loc) · 7.58 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
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
console.log("LISTENING TO QUERIES");
const spicedPg = require("spiced-pg");
const bcrypt = require("bcryptjs");
var db;
const S3config = require("./config.json");
if (process.env.DATABASE_URL) {
db = spicedPg(process.env.DATABASE_URL);
} else {
db = spicedPg(`postgres:postgres:postgres@localhost:5432/soacialnetwork`);
}
// Register
module.exports.register = function(first, last, email, password) {
const params = [first, last, email, password];
const q = `INSERT INTO USERS (first, last, email, password) VALUES ($1, $2, $3, $4) RETURNING id`;
return db
.query(q, params)
.then(function(results) {
console.log("db reg results", results.rows);
return results.rows[0].id;
})
.catch(err => {
console.log("error in sending registration data to db", err);
});
};
// Hash password
module.exports.hashPassword = function(plainTextPassword) {
return new Promise((resolve, reject) => {
bcrypt.genSalt((err, salt) => {
if (err) {
console.log("error in bcrypt.genSalt", err);
}
bcrypt.hash(plainTextPassword, salt, (err, hash) => {
if (err) {
return reject(err);
}
resolve(hash);
});
});
});
};
// check password on login
module.exports.checkPassword = function(plainTextPassword, storedHash) {
return new Promise((resolve, reject) => {
bcrypt.compare(plainTextPassword, storedHash, (err, match) => {
if (err) {
reject(err);
} else {
resolve(match);
}
});
});
};
// match password up with user info for logged In Router
module.exports.getUserInfo = function(email) {
return db
.query(
`SELECT *
FROM users
WHERE email = $1`,
[email]
)
.then(results => {
console.log("results from getUserInfo: ", results.rows);
return results.rows[0];
})
.catch(function(err) {
console.log("error in getUserInfo", err);
throw err;
});
};
// Get profile info
module.exports.getProfileInfo = function(id) {
console.log("id:", id);
const q = `SELECT * FROM users WHERE id = $1`;
const params = [id];
return db.query(q, params).then(results => {
console.log("results from db.getProfileInfo: ", results.rows[0]);
return results.rows[0];
});
};
// send photo to database
module.exports.updateProfilePic = function(img, id) {
// const imgUrl = S3config.s3Url + img;
const params = [img, id];
console.log(params);
const q = "UPDATE users SET imageUrl = $1 WHERE id = $2 RETURNING imageUrl";
return db
.query(q, params)
.then(results => {
console.log("results from db updateprofilepic: ", results.rows[0]);
return results.rows[0];
})
.catch(err => {
console.log("err in updateProfilePic", err);
});
};
//Update / set bio
module.exports.updateUserBio = function(bio, id) {
const q = `UPDATE users
SET bio = $1 WHERE id = $2`;
const params = [bio, id];
return db.query(q, params);
};
module.exports.getUserInfoById = function(id) {
const q = `
SELECT * FROM users
WHERE id = $1`;
const params = [id];
return db
.query(q, params)
.then(results => {
return results.rows[0];
})
.catch(err => {
console.log("Theres an error on db getUserInfoById", err);
});
};
//MODULE FRIEND REQUEST
module.exports.sendRequestStatus = function(user1_id, user2_id) {
const q = `SELECT curr_status, user2_id FROM friends
WHERE (user1_id = $1 AND user2_id = $2)
OR (user1_id = $2 AND user2_id = $1)`;
const params = [user1_id, user2_id];
return db
.query(q, params)
.then(results => {
return results.rows[0];
})
.catch(err => {
console.log("Here was the error", err);
});
};
module.exports.addFriendReq = function(user1_id, user2_id, curr_status) {
const q = `
INSERT INTO friends (user1_id, user2_id, curr_status)
VALUES ($1, $2, $3)
RETURNING id`;
const params = [user1_id, user2_id, curr_status];
return db
.query(q, params)
.then(results => {
console.log(
"RESULTS || ADDFRIENDREQ.results.rows[0]",
results.rows[0]
);
return results.rows[0];
})
.catch(err => {
console.log("ERR IN ADDFRIENDREQ || DB.JS", err);
});
};
module.exports.cancelFriendReq = function(user1_id, user2_id) {
const q = `
DELETE FROM friends WHERE user1_id = $1 AND user2_id = $2`;
const params = [user1_id, user2_id];
return db
.query(q, params)
.then(results => {
console.log("RESULTS || CANCELED FRIENDREQ", results);
console.log("RESULTS || CANCELED.results.rows[0]", results.rows[0]);
return results.rows[0];
})
.catch(err => {
console.log("ERR IN CANCELFRIENDREQ || DB.JS", err);
});
};
module.exports.acceptFriendReq = function(user1_id, user2_id, curr_status) {
console.log("accept func");
const q = `
UPDATE friends SET curr_status = 2
WHERE (user1_id = $2
AND user2_id = $1) OR (user1_id = $1
AND user2_id = $2) `;
const params = [user1_id, user2_id];
return db
.query(q, params)
.then(results => {
return results;
})
.catch(err => {
console.log("err | db.js | accept friend", err);
});
};
module.exports.deleteFriend = function(user1_id, user2_id) {
const q = `
DELETE FROM friends
WHERE user1_id = $1 AND user2_id = $2
OR user1_id = $2 AND user2_id = $1`;
const params = [user1_id, user2_id];
return db
.query(q, params)
.then(results => {
return results;
})
.catch(err => {
console.log("err | db.js | delete friend", err);
});
};
module.exports.getFriends = function(userId) {
const q = `SELECT users.first, users.last, users.imageUrl
FROM users
JOIN friends
ON (user1_id = usersId AND user1_id = $1)
OR (user2_id = user.id AND user2_id = $1 AND user2_id = $1)
WHERE curr_status = 2
AND (user2_id = $1 OR user1_id = $1)`;
params = [userId];
return db.query(q, params).then(results => {
console.log("getFriends FROM DB:", RESULTS);
result = result.rows.map(row => {
//console.log('db.getFriends', results);
if (row.imageUrl) {
row.imageUrl = config.s3Url + row.imageUrl;
}
return row;
console.log("getFriends FROM DB:", row);
});
});
};
/*
module.exports.getFriendRequests = function() {
const params = [userId, 1];
const q = `SELECT users.id, users.first, users.last, users.imageUrl
FROM users
JOIN friends
ON (user1_id = users.id)
WHERE curr_status = $2
AND (user2_id = $1)`;
return new Promise(function(resolve, reject) {
db.query(q, params).then((result) => {
result = result.rows.map((row) => {
if(row.imageUrl) {
row.imageUrl = config.s3Url + row.imageUrl;
}
return row;
})
resolve(result);
})
})
}
*/