-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
180 lines (165 loc) · 4.53 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
const spicedPg = require("spiced-pg");
const db = spicedPg(
process.env.DATABASE_URL ||
`postgres:postgres:postgres@localhost:5432/tarantula`
);
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.getHashAndUserId = email => {
return db.query(
`SELECT password, id
FROM users
WHERE email = $1`,
[email]
);
};
module.exports.getUser = user => {
return db.query(
`SELECT *
FROM users
WHERE id = $1
`,
[user]
);
};
module.exports.getFile = file => {
return db.query(
`SELECT *
FROM plots
WHERE id = $1
`,
[file]
);
};
module.exports.addImage = (id, image) => {
return db.query(
`UPDATE users
SET image = $2
WHERE id = $1
RETURNING image`,
[id, image]
);
};
// module.exports.editBio = (id, bio) => {
// return db.query(
// `UPDATE users
// SET bio = $2
// WHERE id = $1
// RETURNING bio`,
// [id, bio]
// );
// };
module.exports.latestFiles = () => {
return db.query(
`SELECT id, ra, dec, name, file, description, url
FROM plots
ORDER BY id DESC
LIMIT 15`
);
};
module.exports.findFiles = input => {
return db.query(
`SELECT id, ra, dec, name, file, description FROM plots
WHERE name ILIKE $1`,
[input + "%"]
);
};
// module.exports.getFriendship = (sender_id, receiver_id) => {
// return db.query(
// `SELECT * FROM friendships
// WHERE (receiver_id = $1 AND sender_id = $2)
// OR (receiver_id = $2 AND sender_id = $1)
// `,
// [sender_id, receiver_id]
// );
// };
//
// module.exports.reqFriendship = (sender_id, receiver_id) => {
// return db.query(
// `INSERT INTO friendships (sender_id, receiver_id)
// VALUES ($1, $2)
// `,
// [sender_id, receiver_id]
// );
// };
//
// module.exports.acceptFriendship = (sender_id, receiver_id) => {
// return db.query(
// `UPDATE friendships
// SET accepted = true
// WHERE (sender_id=$1 AND receiver_id=$2)
// OR (sender_id=$2 AND receiver_id=$1)
// `,
// [sender_id, receiver_id]
// );
// };
// module.exports.unFriend = (sender_id, receiver_id) => {
// return db.query(
// `DELETE FROM friendships
// WHERE (sender_id=$1 AND receiver_id=$2)
// OR (sender_id=$2 AND receiver_id=$1)`,
// [sender_id, receiver_id]
// );
// };
//
// module.exports.getFriendsWannabes = id => {
// return db.query(
// ` SELECT users.id, first, last, image, 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]
// );
// };
module.exports.getLastTenChatMessages = () => {
return db.query(
`SELECT chats.id, users.first, users.last, users.image, chats.message, chats.created_at, chats.user_message_id
FROM chats
JOIN users
ON (users.id = chats.user_message_id)
ORDER BY chats.id DESC
LIMIT 10
`
);
};
module.exports.addNewMessage = (message, user_message_id) => {
return db.query(
`INSERT INTO chats (message, user_message_id)
VALUES ($1, $2)
RETURNING (message, id)
`,
[message, user_message_id]
);
};
module.exports.getMessageInfo = id => {
return db.query(
`SELECT chats.id AS chatsid, users.first, users.last, users.image, chats.message, chats.created_at, chats.user_message_id
FROM chats
JOIN users
ON (users.id = chats.user_message_id AND chats.user_message_id = $1)
ORDER BY chats.id DESC
LIMIT 1
`,
[id]
);
};
// module.exports.getOtherProfileFriends = id => {
// return db.query(
// `SELECT users.id, first, last, image, accepted
// FROM friendships
// JOIN users
// ON (accepted = true AND receiver_id = $1 AND sender_id = users.id)
// OR (accepted = true AND sender_id = $1 AND receiver_id = users.id)
// `,
// [id]
// );
// };