-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
357 lines (325 loc) · 7.4 KB
/
app.js
File metadata and controls
357 lines (325 loc) · 7.4 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
const express = require('express')
const path = require('path')
const {open} = require('sqlite')
const sqlite3 = require('sqlite3')
const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken')
const app = express()
app.use(express.json())
const dbpath = path.join(__dirname, 'twitterClone.db')
let db = null
const init = async () => {
try {
db = await open({
filename: dbpath,
driver: sqlite3.Database,
})
app.listen(3000, () => {
console.log('Server hosted at 3000')
})
} catch (e) {
console.log(`Error is -> ${e}`)
process.exit(1)
}
}
init()
const authentication = (req, res, next) => {
let jwtToken
const headers = req.headers['authorization']
if (headers !== undefined) {
jwtToken = headers.split(' ')[1]
}
if (jwtToken === undefined) {
res.status(401)
res.send('Invalid JWT Token')
} else {
jwt.verify(jwtToken, 'MY_SECRET_TOKEN', async (error, payload) => {
if (error) {
res.status(401)
res.send('Invalid JWT Token')
} else {
req.userId = payload.user_id
console.log('auth end..')
next()
}
})
}
}
app.post('/register/', async (req, res) => {
const {username, password, name, gender} = req.body
if (password.length < 6) {
res.status(400)
res.send('Password is too short')
return
}
const checkuserquery = `
SELECT * FROM user
WHERE
username = '${username}' ;`
const checkuser = await db.get(checkuserquery)
if (checkuser !== undefined) {
res.status(400)
res.send('User already exists')
} else {
const epassword = await bcrypt.hash(password, 10)
const query = `
INSERT INTO
user (username,password,name,gender)
VALUES ('${username}','${epassword}','${name}','${gender}')`
await db.run(query)
res.status(200)
res.send('User created successfully')
}
})
app.post('/login/', async (req, res) => {
const {username, password} = req.body
const checkuserquery = `
SELECT * FROM user
WHERE
username = '${username}';`
const dbuser = await db.get(checkuserquery)
if (dbuser === undefined) {
res.status(400)
res.send('Invalid user')
} else {
const checkpassword = await bcrypt.compare(password, dbuser.password)
if (checkpassword) {
const user_id = dbuser.user_id
const payload = {
user_id: user_id,
}
const jwtToken = await jwt.sign(payload, 'MY_SECRET_TOKEN')
console.log(jwtToken)
res.send({jwtToken})
} else {
res.status(400)
res.send('Invalid password')
}
}
})
app.get('/user/tweets/feed/', authentication, async (req, res) => {
const {userId} = req
const query = `
SELECT
u.username AS username,
t.tweet AS tweet,
t.date_time AS dateTime
FROM
follower f
JOIN
tweet t ON f.following_user_id = t.user_id
JOIN
user u ON u.user_id = t.user_id
WHERE
f.follower_user_id = ${userId}
ORDER BY
t.date_time DESC
LIMIT 4;`
const dbuser = await db.all(query)
res.send(dbuser)
})
const uname = i => ({
name: i.name,
})
app.get('/user/following/', authentication, async (req, res) => {
const {userId} = req
const query = `
SELECT
u2.name
FROM
follower f
JOIN
user u1 ON u1.user_id = f.follower_user_id
JOIN
user u2 ON u2.user_id = f.following_user_id
WHERE
u1.user_id = ${userId};
`
const dbres = await db.all(query)
res.send(dbres.map(i => uname(i)))
})
app.get('/user/followers/', authentication, async (req, res) => {
const {userId} = req
const query = `
SELECT
u1.name
FROM
follower f
JOIN
user u1 ON u1.user_id = f.follower_user_id
JOIN
user u2 ON u2.user_id = f.following_user_id
WHERE
u2.user_id = ${userId} ;
`
const dbres = await db.all(query)
res.send(dbres.map(i => uname(i)))
})
//userfollowing verifiying function
const verifyfollowing = async (req, res, next) => {
const {tweetId} = req.params
const {userId} = req
const query = `
SELECT *
FROM
follower f
JOIN
tweet t ON f.following_user_id= t.user_id
WHERE
f.follower_user_id = ${userId} AND t.tweet_id = ${tweetId};
`
const tweeterId = await db.all(query)
console.log(tweeterId.length)
console.log('very...')
if (tweeterId.length === 0) {
console.log('very...')
res.status(401)
res.send('Invalid Request')
return
} else {
next()
}
}
// twitter and like
//reply and tweet
app.get(
'/tweets/:tweetId/',
authentication,
verifyfollowing,
async (req, res) => {
const {tweetId} = req.params
const likequery = `
SELECT COUNT(like_id) AS likes FROM
like
WHERE
tweet_id = ${tweetId}`
const likecount = await db.get(likequery)
const replyquery = `
SELECT COUNT(reply_id) AS replies FROM
reply
WHERE
tweet_id = ${tweetId}`
const replycount = await db.get(replyquery)
const tweetquery = `
SELECT tweet,date_time FROM
tweet
WHERE
tweet_id = ${tweetId}`
const dbrestweet = await db.get(tweetquery)
res.send({
tweet: dbrestweet.tweet,
likes: likecount.likes,
replies: replycount.replies,
dateTime: dbrestweet.date_time,
})
},
)
//API 7
app.get(
'/tweets/:tweetId/likes/',
authentication,
verifyfollowing,
async (req, res) => {
const {tweetId} = req.params
const query = `
SELECT u1.username FROM
user u1
JOIN
like l ON u1.user_id = l.user_id
WHERE
tweet_id = ${tweetId};
`
const dbres = await db.all(query)
res.send({
likes: dbres.map(i => i.username),
})
},
)
//API 8
app.get(
'/tweets/:tweetId/replies/',
authentication,
verifyfollowing,
async (req, res) => {
const {tweetId} = req.params
const query = `
SELECT u1.name,l.reply FROM
user u1
JOIN
reply l ON u1.user_id = l.user_id
WHERE
tweet_id = ${tweetId};
`
const dbres = await db.all(query)
res.send({
replies: dbres.map(i => ({
name: i.name,
reply: i.reply,
})),
})
},
)
//API 9
app.get('/user/tweets/', authentication, async (req, res) => {
const {userId} = req
const query = `
SELECT
t.tweet AS tweets,
t.date_time AS time,
(SELECT COUNT(like_id) FROM like WHERE tweet_id = t.tweet_id) AS likes,
(SELECT COUNT(reply_id) FROM reply WHERE tweet_id = t.tweet_id) AS replies
FROM
tweet t
WHERE
t.user_id = ${userId};
`
const dbres = await db.all(query)
res.send(
dbres.map(i => ({
tweet: i.tweets,
likes: i.likes,
replies: i.replies,
dateTime: i.time,
})),
)
})
app.get('/deek/', async (req, res) => {
const r = `
SELECT * FROM user;`
const dbres = await db.all(r)
res.send(dbres)
})
app.post('/user/tweets/', authentication, async (req, res) => {
const {tweet} = req.body
const {userId} = req
const query = `
INSERT INTO
tweet (tweet,user_id)
VALUES
('${tweet}',${userId})`
await db.run(query)
res.send('Created a Tweet')
})
app.delete('/tweets/:tweetId/', authentication, async (req, res) => {
const {userId} = req
const {tweetId} = req.params
const query = `
SELECT user_id FROM tweet
WHERE
tweet_id = ${tweetId};
`
const check = await db.get(query)
if (check.user_id !== userId) {
res.status(401)
res.send('Invalid Request')
} else {
const q1 = `
DELETE FROM tweet
WHERE
tweet_id = ${tweetId};
`
await db.run(q1)
res.send('Tweet Removed')
}
})
module.exports = app