-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.js
116 lines (107 loc) · 2.71 KB
/
post.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
const express = require('express');
const mongo = require('mongodb').MongoClient;
const router = express.Router()
const dotenv = require('dotenv');
const auth = require('./auth');
dotenv.config();
const dburl = process.env.DB_URL;
const con = (dburl, callback) => {
mongo.connect(dburl, {
useNewUrlParser: true,
useUnifiedTopology: true
}, (err, client) => {
if (err) {
console.error(err)
return
}
const db = client.db('likileaks')
callback(db)
})
}
const updateCred = (author, increase) => {
con(dburl, (db) => {
const userCol = db.collection('user')
userCol.update({username: author}, {$inc: {"credibility": increase ? 1 : -1}})
})
}
router.get('/posts', (req, res) => {
const username = req.query.username
const p = req.query.page
// const page = p ? p : 0
const option = username ? {author: username} : {}
con(dburl, (db) => {
const postCol = db.collection('post')
postCol.find(option, {projection: {_id: 0}}).toArray((err, result) => {
if (err) {
console.log(err)
res.send(404)
return
}
res.json(result)
})
})
})
router.get('/downVote', auth, (req, res) => {
// update table
const id = req.query.id;
con(dburl, (db) => {
const postCol = db.collection('post')
postCol.findOneAndUpdate({id : id}, {$inc : {downVote : 1}}, (err, result) => {
if (err) {
console.log(err);
res.send(404);
} else {
if (result) {
updateCred(result.value.author, false)
res.json({message : "success"})
return
}
res.send(200)
}
})
})
})
router.get('/upVote', auth, (req, res) => {
const id = req.query.id;
con(dburl, (db) => {
const postCol = db.collection('post')
postCol.findOneAndUpdate({id : id}, {$inc : {upVote : 1}}, (err, result) => {
if (err) {
console.log(err);
res.send(404);
} else {
if (result) {
updateCred(result.value.author, true)
res.json({message : "success"})
return
}
res.send(200)
}
})
})
})
router.post('/newPost', auth, (req, res) => {
const obj = req.body
obj.upVote = 0.0;
obj.downVote = 0.0;
obj.tags = obj.description.split(/([.,!?:;'\"-]|\s)+/)
.filter(str => str.startsWith("#"))
.map(str => str.substring(1))
con(dburl, (db) => {
const userCol = db.collection('post')
userCol.insertOne(obj, (err, result) => {
if (err) {
console.log(err)
res.send(404)
return
}
if (result) {
res.send(200)
return
}
res.send(404)
})
})
// res.send(200)
});
module.exports = router