-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
217 lines (194 loc) · 4.77 KB
/
database.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
const db = require("./server");
const getUserByUsername = (username) => {
return db
.query(
`
SELECT id, username, first_name AS "firstName", last_name AS "lastName", email, password, profile_picture_url AS "profilePictureUrl", date_joined AS "dateJoined" FROM users
WHERE username = $1;
`,
[`${username}`]
)
.then((res) => res.rows[0])
.catch((error) => {
console.log("error ", error);
throw new Error(error);
});
};
exports.getUserByUsername = getUserByUsername;
const getUserByEmail = (email) => {
return db
.query(
`
SELECT * FROM users
WHERE email = $1
`,
[email]
)
.then((res) => res.rows[0])
.catch((error) => {
console.log("No user found with that email", error);
throw new Error(error);
});
};
exports.getUserByEmail = getUserByEmail;
const getUserById = (id) => {
return db
.query(
`
SELECT id, username, first_name AS "firstName", last_name AS "lastName", email, password, profile_picture_url AS "profilePictureUrl", date_joined AS "dateJoined" FROM users
WHERE id = $1
`,
[id]
)
.then((res) => res.rows[0])
.catch((error) => {
console.log("Error: ", error);
throw new Error(error);
});
};
exports.getUserById = getUserById;
const registerUser = (newUser) => {
console.log("new user:", newUser);
const {
firstName,
lastName,
username,
email,
password,
profilePictureUrl,
} = newUser;
return db
.query(
`
INSERT INTO users (username, first_name, last_name, email, password, profile_picture_url)
VALUES
($1, $2, $3, $4, $5, $6)
RETURNING *;
`,
[username, firstName, lastName, email, password, profilePictureUrl]
)
.then((res) => res.rows[0])
.catch((error) => {
console.log("Error: ", error);
throw new Error(error);
});
};
exports.registerUser = registerUser;
const getAllTweets = () => {
console.log("getAllTweets");
return db
.query(
`
SELECT tweets.id, tweets.user_id AS "userId", tweets.content, tweets.tweet_date AS "tweetDate", users.first_name AS "firstName", users.last_name AS "lastName", users.username, users.profile_picture_url AS "profilePictureURL",
(SELECT COUNT(*) FROM favourites
WHERE tweet_id = tweets.id
) AS "tweetFavourites"
FROM tweets
JOIN users ON user_id = users.id
ORDER BY tweet_date DESC;
`
)
.then((res) => {
return res.rows;
})
.catch((error) => {
console.log("error: ", error);
throw new Error(error);
});
};
exports.getAllTweets = getAllTweets;
const getTweetsForUser = (userId) => {
return db
.query(
`
SELECT tweets.id, tweets.user_id AS "userId", tweets.content, tweets.tweet_date AS "tweetDate", users.first_name AS "firstName", users.last_name AS "lastName", users.username, users.profile_picture_url AS "profilePictureURL",
(SELECT COUNT(*) FROM favourites
WHERE tweet_id = tweets.id
) AS "tweetFavourites"
FROM tweets
JOIN users ON user_id = users.id
WHERE tweets.user_id = $1
ORDER BY tweet_date DESC;
`,
[userId]
)
.then((res) => res.rows)
.catch((error) => {
console.log("Error getting tweets for user: ", error);
throw new Error(error);
});
};
exports.getTweetsForUser = getTweetsForUser;
const newTweet = (tweetContent, userId) => {
return db
.query(
`
INSERT INTO tweets (user_id, content)
VALUES ($1, $2)
RETURNING *;
`,
[userId, tweetContent]
)
.then((res) => res.rows[0]);
};
exports.newTweet = newTweet;
const doesUserLikeTweet = (tweetId, userId) => {
return db
.query(
`
SELECT * FROM favourites
WHERE user_id = $1
AND tweet_id = $2
`,
[userId, tweetId]
)
.then((res) => {
console.log("doesUserLikeTweet res", res.rows);
return res.rows;
})
.catch((error) => {
console.log("Error: ", error);
throw new Error(error);
});
};
exports.doesUserLikeTweet = doesUserLikeTweet;
const favouriteTweet = (tweetId, userId) => {
return db
.query(
`
INSERT INTO favourites (user_id, tweet_id)
VALUES ($1, $2)
RETURNING *;
`,
[userId, tweetId]
)
.then((res) => res.rows[0])
.catch((error) => {
console.log("Error: ", error);
throw new Error(error);
});
};
exports.favouriteTweet = favouriteTweet;
const unFavouriteTweet = (tweetId, userId) => {
return db.query(
`
DELETE FROM favourites
WHERE user_id = $1
AND tweet_id = $2
`,
[userId, tweetId]
);
};
exports.unFavouriteTweet = unFavouriteTweet;
const getTweetFavouriteCount = (tweetId) => {
return db
.query(
`
SELECT COUNT(*) FROM favourites
WHERE tweet_id = $1
`,
[tweetId]
)
.then((res) => res.rows[0]);
};
exports.getTweetFavouriteCount = getTweetFavouriteCount;