-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
152 lines (150 loc) · 5.07 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
// DB set up ===================================================================
var spicedPg = require('spiced-pg');
var db = spicedPg(process.env.DATABASE_URL || 'postgres:postgres:postgres@localhost:5432/SocialNetwork');
var path = require("path");
const s3 = require('./config.json')
//Function insertNewUser========================================================
function insertNewUser(first, last, email, password) {
return db.query('INSERT INTO users (first, last, email, password) VALUES ($1, $2, $3, $4) RETURNING id', [
first, last, email || null,
password || null
]).then((results) => {
return results.rows[0].id
})
};
exports.insertNewUser = insertNewUser;
//function getHashed============================================================
function getHashed(email) {
const query = `SELECT password FROM users WHERE email = $1`;
const params = [email];
return db.query(query, params).then((results) => {
return results.rows[0].password
})
};
exports.getHashed = getHashed;
// function searchInfos=========================================================
function searchInfos(email) {
const query = `SELECT id FROM users WHERE email = $1`
const params = [email]
return db.query(query, params).then((results) => {
return results.rows[0]
})
};
exports.searchInfos = searchInfos;
// get user infos===============================================================
function getInfos(id) {
const query = `SELECT * FROM users WHERE id = $1`
const params = [id]
return db.query(query, params).then((results) => {
console.log("results", results.rows[0])
return results.rows[0]
})
}
exports.getInfos = getInfos;
//POST upload Profilepic========================================================
function uploadPic(imgUrl, id) {
const query = `UPDATE users
SET imgUrl = $1
WHERE id = $2
RETURNING imgUrl`
const params = [imgUrl, id];
return db.query(query, params).then((results) => {
results.rows.forEach((elem) => {
elem.imgurl = s3.s3Url + elem.imgurl;
})
return results.rows[0];
}).catch((err) => {
console.log(err)
})
};
exports.uploadPic = uploadPic;
//Update users with BIO=========================================================
function updateUser(bio, id) {
const query = `UPDATE users
SET bio = $1
WHERE id = $2
`
const params = [bio, id];
return db.query(query, params)
};
exports.updateUser = updateUser;
//CHECK STATUS OF THE RELATIONSHIP BETWEEN USERS================================
function getStatus(sender_id, recipient_id) {
const query = `
SELECT status, recipient_id, sender_id FROM friends
WHERE (sender_id = $1 AND recipient_id = $2)
OR (sender_id = $2 AND recipient_id = $1)
`
const params = [sender_id, recipient_id];
return db.query(query, params).then((results) => {
return results.rows[0]
})
};
exports.getStatus = getStatus;
//UPDATE FRIENDREQUESTS=========================================================
//PENDING REQ
function pendingReq(status, recipient_id, sender_id) {
console.log("pendingReq", sender_id, recipient_id);
if (sender_id !== recipient_id) {
const query = `
INSERT INTO friends (status, recipient_id, sender_id) VALUES($1, $2, $3) RETURNING sender_id`
const params = [status, sender_id, recipient_id];
return db.query(query, params);
}
};
exports.pendingReq = pendingReq;
//CANCEL REQ
function cancelReq(recipient_id, sender_id) {
const query = `
DELETE FROM friends
WHERE recipient_id=$1 AND sender_id=$2
OR recipient_id=$2 AND sender_id=$1
`
const params = [recipient_id, sender_id];
return db.query(query, params);
}
exports.cancelReq = cancelReq;
//ACCEPT FRIEND REQ
function acceptReq(status, sender_id, recipient_id) {
console.log("inside acceptReq", status, sender_id, recipient_id)
const query = `
UPDATE friends
set status = $1
WHERE sender_id = $2
AND recipient_id = $3
`
const params = [status, sender_id, recipient_id];
return db.query(query, params);
};
exports.acceptReq = acceptReq;
//FUNCTION GET FRIENDS==========================================================
function getFriends(id) {
const query = `
SELECT users.id, first, last, imgurl, status
FROM friends
JOIN users
ON (status = 1 AND recipient_id = $1 AND sender_id = users.id)
OR (status = 3 AND recipient_id = $1 AND sender_id = users.id)
OR (status = 3 AND sender_id = $1 AND recipient_id = users.id)`
const params = [id];
return db.query(query, params).then((results) => {
return results.rows
});
};
exports.getFriends = getFriends;
//GET INFOS ABOUT ONLINE USERS==================================================
function getUsersByIds(arrayOfIds) {
const query = `SELECT * FROM users WHERE id = ANY($1)`;
return db.query(query, [arrayOfIds]).then((results) => {
return results.rows
});
};
exports.getUsersByIds = getUsersByIds;
//GET getSpecificUser===========================================================
function getSpecificUser(id) {
const query = `SELECT * FROM users WHERE id = $1`
return db.query(query, [id]).then((results) => {
return results.rows[0]
});
};
exports.getSpecificUser = getSpecificUser;