-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
175 lines (144 loc) · 5.74 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
const spicedPg = require("spiced-pg");
const bcrypt = require("./bcrypt");
const db = spicedPg(
process.env.DATABASE_URL ||
"postgres:postgres:postgres@localhost:5432/socialnetwork"
);
/////////////////////////////////////////////////////////////////////
////////////////CREATING A USER AFTER THE REGISTRATION///////////////
exports.createUser = function createUser(first, last, email, pass) {
return db.query(
`INSERT INTO users (first, last, email, pass)
VALUES ($1, $2, $3, $4)
RETURNING id`,
[first || null, last || null, email || null, pass || null]
);
};
////////////////////GETTING A USER BY E-MAIL AFTER LOG IN/////////////////
//we are using this to compare passwords and let user in or not///////
exports.getUserByMail = function getUserByMail(email) {
return db.query(
`SELECT *
FROM users
WHERE email=$1`, [email]
);
};
//////////////GETTING A USER BY ID AFTER THE LOG IN//////////////////
//////this we use after the log in to display users information//////
exports.getUserByID = function getUserByID(userID) {
return db.query (`
SELECT id, first, last, imgurl, bio
FROM users
WHERE id=$1`, [userID]);
};
//////////////////////////UPDATING PROFILE PIC////////////////////
exports.updatePic = function updatePic(userID, ProfilePicUrl) {
return db.query(`
UPDATE users
SET imgurl = $2
WHERE id = $1
RETURNING id, imgurl`, [userID, ProfilePicUrl]);
};
///////////////////////UPDATING YOUR BIO////////////////////////
exports.updateBio = function updateBio(userID, bioText) {
return db.query(`
UPDATE users
SET bio = $2
WHERE id = $1
RETURNING id, bio
`, [userID, bioText]);
};
/////////////////////CHECK FRIENDSHIP////////////////////////
exports.checkFriendship = function checkFriendship(senderID, receiverID) {
return db.query(`
SELECT * FROM friendships
WHERE (receiver_id = $1 AND sender_id = $2)
OR (receiver_id = $2 AND sender_id = $1);
`, [senderID, receiverID]);
};
/////////////////////SEND A FRIEND REQUEST////////////////////////
exports.sendRequest = function sendRequest(senderID, receiverID) {
return db.query(`
INSERT INTO friendships (sender_id, receiver_id)
VALUES ($1, $2)
RETURNING *`, [senderID, receiverID]);
};
//////////////////////////CANCEL REQUEST////////////////////////
exports.cancelRequest = function cancelRequest(senderID, receiverID) {
return db.query(`
DELETE FROM friendships
WHERE (receiver_id = $1 AND sender_id = $2)
OR (receiver_id = $2 AND sender_id = $1)
RETURNING *`, [senderID, receiverID]);
};
//////////////////////////ADDING A FRIEND/////////////////////////////
exports.acceptRequest = function acceptRequest(senderID, receiverID) {
return db.query(`
UPDATE friendships
SET accepted=true
WHERE (receiver_id = $1 AND sender_id = $2)
OR (receiver_id = $2 AND sender_id = $1)`, [senderID, receiverID]);
};
//////////////////////GETTING FRIENDS FOR MY FRIENDS AND WANNABEES PAGE/////////////
//we kinda select all the users.id which satisfy the condition
exports.getFriendsAndWannabes = function getFriendsAndWannabes(id) {
return db.query(`
SELECT users.id, first, last, imgurl, accepted
FROM friendships
JOIN users
ON (accepted = false AND receiver_id = $1 AND sender_id = users.id)
OR (accepted = true AND receiver_id = $1 AND sender_id = users.id)
OR (accepted = true AND sender_id = $1 AND receiver_id = users.id)`, [id]);
};
/////////////////GETTING ONLINE FRIENDS///////////////////////////
/////////////////////////////////////////////////////////////////
exports.getUsersByIds = function getUsersByIds(arrayOfIds) {
const query = `SELECT id, first, last, imgurl FROM users WHERE id = ANY($1)`;
return db.query(query, [arrayOfIds]);
};
///////////////GETTING 10 MOST RECENT MESSAGES////////////////////
/////////////////////////////////////////////////////////////////
exports.getTenMessages = function getTenMessages() {
return db.query(`
SELECT * FROM (
SELECT m.id as m_id, m.message, m.sender_id, m.created_at, u.first, u.last, u.imgurl, u.id as u_id
FROM messages AS m
LEFT JOIN users as u
ON m.sender_id = u.id
ORDER BY created_at DESC
LIMIT 10
) as test
ORDER BY test.created_at ASC
`);
};
//////////////////////INSERTING THE NEW CHAT MESSAGE IN///////////////////////
exports.insertChatMessage = function insertChatMessage(sender_id, message) {
return db.query(`
INSERT INTO messages (sender_id, message)
VALUES ($1, $2)`, [sender_id, message]);
};
/////////////////////////GETTING THE INSERTED MESSAGE BACK/////////////////////////
exports.getTheInsertedMessageBack = function getTheInsertedMessageBack() {
return db.query(`
SELECT m.id as m_id, m.message, m.sender_id, u.first, u.last, u.imgurl, u.id as u_id
FROM messages AS m
LEFT JOIN users as u
ON m.sender_id = u.id
ORDER BY created_at DESC
LIMIT 1`);
};
////////////////////////////SEARCHING DATABASE FOR A SEARCH REQUEST/////////////////
exports.search = function search(request) {
return db.query(`
SELECT *
FROM users
WHERE first ILIKE $1 OR last ILIKE $1`, [request + "%"]);
};
/////////////////////HASHING PASSWORDS////////////////////////
/////////////////////////////////////////////////////////////
exports.hashPassword = function hashPassword(textPass) {
return bcrypt.hash(textPass);
};
exports.comparePassword = function comparePassword(textPassword, hashPassword) {
return bcrypt.compare(textPassword, hashPassword);
};