-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
331 lines (293 loc) · 9.91 KB
/
app.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
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
const express = require('express')
const cors = require('cors')
require('dotenv').config()
const fs = require('fs')
const fileupload = require('express-fileupload')
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID
const app = express()
app.use(cors())
app.use(express.json())
app.use(express.static('serviceimg'))
app.use(fileupload())
const port = 2555;
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.xzynl.mongodb.net/${process.env.DATABASE}?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
console.log(err);
const userCollection = client.db(`${process.env.DATABASE}`).collection("users");
const blogCollection = client.db(`${process.env.DATABASE}`).collection("blogs");
const commentCollection = client.db(`${process.env.DATABASE}`).collection("comments");
const questionCollection = client.db(`${process.env.DATABASE}`).collection("questions");
const answerCollection = client.db(`${process.env.DATABASE}`).collection("answers");
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.post('/login', (req, res) => {
console.log('login req recieved')
userCollection.findOne({ email: req.body.email }, (err, doc) => {
if (doc) {
console.log('email address found')
console.log(req.body.password)
console.log(doc.password)
if (doc.password === req.body.password) {
console.log('password matched')
const userInfo = {
fullName: doc.fullName,
userName: doc._id,
role: doc.role,
photo: doc.photo,
spamcount: doc.spamcount
}
res.send(userInfo)
} else {
res.send(false)
}
} else {
res.send(false)
}
})
})
// Register User
app.post('/register', (req, res) => {
console.log(req.body.email)
const user = req.body
userCollection.findOne({ email: req.body.email }, (err, doc) => {
if (!doc) {
console.log('unique email')
userCollection.insertOne(user)
.then(result => {
console.log(result.insertedCount)
res.send(result.insertedCount > 0)
})
} else {
res.send(false)
}
})
})
// Check Existing User
app.post('/users', (req, res) => {
const email = req.body.email;
userCollection.findOne({ email: email }, (err, doc) => {
if (doc) {
res.send(doc)
} else {
res.send(false)
}
})
})
// Create New User
app.post('/createUser', (req, res) => {
userCollection.insertOne(req.body)
.then(result => {
res.send(result.insertedCount > 0);
})
})
// Get Public Profile Info
app.get('/profile/:userName', (req, res) => {
const userName = req.params.userName;
userCollection.findOne({ _id: userName }, (err, doc) => {
if (doc) {
const publicProfile = {
fullName: doc.fullName,
photo: doc.photo,
role: doc.role
}
res.send(publicProfile)
} else {
res.send(false)
}
})
})
// Add Blog Post
app.post('/addBlog', (req, res) => {
const blog = req.body
blogCollection.insertOne(blog)
.then(result => {
res.send(result.insertedCount > 0);
})
})
// Get Blog Post
app.get('/getBlogs', (req, res) => {
blogCollection.find()
.toArray((err, docs) => res.send(docs))
})
// Get Blog Post
app.get('/blog/:id', (req, res) => {
blogCollection.findOne({ _id: ObjectID(req.params.id) }, (err, doc) => {
if (doc) {
res.send(doc)
} else {
res.send(false)
}
})
})
// Get Comment
app.get('/getComment/:id', (req, res) => {
commentCollection.findOne(({ _id: req.params.id }), (err, doc) => {
if (doc) {
res.send(doc)
} else {
res.send(false)
}
})
})
// Add Comment Reply reference
app.patch('/updateParent/:id', (req, res) => {
const reply = req.body.reply
commentCollection.updateOne({ _id: req.params.id },
{
$set: { 'reply': reply }
}
).then(result => {
res.send(result.modifiedCount > 0)
})
})
// Add Comment Reply reference to blog
app.patch('/updateBlogParent/:id', (req, res) => {
const reply = req.body.reply
blogCollection.updateOne({ _id: ObjectID(req.params.id) },
{
$set: { 'reply': reply }
}
).then(result => {
res.send(result.modifiedCount > 0)
})
})
// Add Comment Reply
app.post('/addComment', (req, res) => {
const comment = req.body
commentCollection.insertOne(comment)
.then(result => {
res.send(result.insertedCount > 0);
})
res.send(true)
})
// Blog Up Vote
app.patch('/updateVote/:id', (req, res) => {
blogCollection.updateOne({ _id: ObjectID(req.params.id) }, {
$set: req.body
}).then(result => {
res.send(result.modifiedCount > 0)
})
})
// Update
app.patch('/updateBlog/:id', (req, res) => {
const title = req.body.title;
const tag = req.body.tag;
const content = req.body.content;
blogCollection.updateOne({ _id: ObjectID(req.params.id) }, {
$set: { title: title, tag: tag, content: content }
}).then(result => {
res.send(result.modifiedCount > 0)
})
})
// Update
// Delete Blog
app.delete('/deleteBlog/:id', (req, res) => {
blogCollection.deleteOne({ _id: ObjectID(req.params.id) })
.then(result => {
res.send(result.deletedCount > 0);
})
})
// Delete Blog
// Update Blog Reply Status
app.patch('/updateReplyStatus/:id', (req, res) => {
blogCollection.updateOne({ _id: ObjectID(req.params.id) }, {
$set: { replyStatus: req.body.replyStatus }
}).then(result => {
res.send(result.modifiedCount > 0)
})
})
// Update Blog Reply Status
// Delete Comment
app.delete('/deleteComment/:id', (req, res) => {
commentCollection.deleteOne({ _id: req.params.id })
.then(result => {
res.send(result.deletedCount > 0)
})
})
// Update Comment
app.patch('/updateComment/:id', (req, res) => {
commentCollection.updateOne({ _id: req.params.id }, {
$set: { comment: req.body.comment }
})
.then(result => {
res.send(result.modifiedCount > 0)
console.log(result.modifiedCount)
})
})
// toggle Feature Comment
app.patch('/toggleFeatureComment/:id', (req, res) => {
commentCollection.updateOne({ _id: req.params.id }, {
$set: req.body
})
.then(result => {
res.send(result.modifiedCount > 0)
console.log(result.modifiedCount)
})
})
// Update Spam Count
app.patch(`/spamCount/:id`, (req, res) => {
userCollection.findOne({ _id: req.params.id }, (err, doc) => {
if (doc) {
if (req.body.count) {
const newCount = { spamcount: doc.spamcount + 1 }
userCollection.updateOne({ _id: req.params.id }, {
$set: newCount
}).then(result => {
res.send(result.modifiedCount > 0)
})
}
if (!req.body.count) {
const newCount = { spamcount: doc.spamcount - 1 }
userCollection.updateOne({ _id: req.params.id }, {
$set: newCount
}).then(result => {
res.send(result.modifiedCount > 0)
})
}
} else {
console.log(err)
}
})
})
// AskAnything App Method
app.get('/allQuestions', (req, res) => {
questionCollection.find()
.toArray((err, docs) => res.send(docs))
})
app.get('/getAnswer/:id', (req, res) => {
answerCollection.find({ parent: req.params.id })
.toArray((err, docs) => res.send(docs))
})
app.post('/addAnswer', (req, res) => {
answerCollection.insertOne(req.body)
.then(result => {
res.send(result.insertedCount > 0)
})
})
app.post('/addQuestion', (req, res) => {
questionCollection.insertOne(req.body)
.then(result => {
res.send(result.insertedCount > 0)
})
})
app.patch('/updateQuetion/:id', (req, res) => {
questionCollection.updateOne({ _id: req.params.id }, {
$set: req.body
}).then(result => {
res.send(result.modifiedCount > 0)
})
})
app.patch('/updateAnswer/:id', (req, res) => {
answerCollection.updateOne({ _id: req.params.id }, {
$set: req.body
}).then(result => {
res.send(result.modifiedCount > 0)
})
})
})
app.listen(process.env.PORT || port, () => {
console.log('server running ..')
})