-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
218 lines (176 loc) · 5.67 KB
/
Copy pathserver.js
File metadata and controls
218 lines (176 loc) · 5.67 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
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://nandit123:<password>@cluster0.nj3nz.mongodb.net/db1?retryWrites=true&w=majority";
const app = express();
app.use(express.json())
const port = process.env.PORT || 3001;
app.listen(port, function (err) {
if (err) console.log(err);
console.log('server listening on PORT', port);
});
app.get('/', (req, res) => {
res.send('GET request called');
})
app.post('/addRating', (req, res) => {
console.log('entered addRating with parameters: ', req.body);
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
async function run() {
try {
console.log('entered try block');
await client.connect();
console.log('connected correctly to the server');
const db = client.db("db1");
const col = db.collection("cl1");
let ratingDocument = {
userId: req.body.userId,
url: req.body.url,
rating: req.body.rating,
type: 'rating'
};
// insert a single document, wait for promise so we can read it back
const p = await col.insertOne(ratingDocument);
// find one document
const myDoc = await col.findOne();
// print to the console
console.log('mydoc:', myDoc);
} catch (e) {
console.log('got an error:', e);
} finally {
await client.close();
}
}
run();
res.send('done')
})
app.post('/addReport', (req, res) => {
console.log('entered addReport with parameters: ', req.body);
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
async function run() {
try {
console.log('entered try block');
await client.connect();
console.log('connected correctly to the server');
const db = client.db("db1");
const col = db.collection("cl1");
let reportDocument = {
userId: req.body.userId,
url: req.body.url,
reportReason: req.body.reportReason,
reasonType: req.body.reasonType, // 1-5 for checkbox reason, 6 for manually written by the user
type: "report"
};
// insert a single document, wait for promise so we can read it back
const p = await col.insertOne(reportDocument);
// find one document
const myDoc = await col.findOne();
// print to the console
console.log('mydoc:', myDoc);
} catch (e) {
console.log('got an error:', e);
} finally {
await client.close();
}
}
run();
res.send('done')
})
app.get('/getRating', (req, res) => {
console.log('entered getRating with parameters: ', req.body);
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
async function run() {
try {
console.log('entered try block');
await client.connect();
console.log('connected correctly to the server');
const db = client.db("db1");
const col = db.collection("cl1");
const query = { url: req.body.url };
const options = {
projection: { rating: 1 }
}
const cursor = await col.find(query, options);
// print a message if no documents were found!
if ((await cursor.count) === 0) {
console.log('no documents found!');
res.send({});
}
const ratingsArray = [];
await cursor.forEach(function (item) {
if (item.rating)
ratingsArray.push(item.rating);
});
const totalRatings = ratingsArray.length;
const averageRating = (ratingsArray.reduce((a, b) => a + b, 0)) / totalRatings;
res.send({
totalRatings: totalRatings,
averageRating: averageRating
})
} catch (e) {
console.log('got an error:', e);
res.send('error:', e)
} finally {
await client.close();
}
}
run();
})
app.post('/addUser', (req, res) => {
console.log('entered addUser with parameters: ', req.body);
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
async function run() {
try {
console.log('entered try block');
await client.connect();
console.log('connected correctly to the server');
const db = client.db("db1");
const col = db.collection("user");
let userDocument = {
userId: req.body.userId,
balance: 0,
walletAddress: '',
timeCreated: Date.now()
};
// insert a single document, wait for promise so we can read it back
const p = await col.insertOne(userDocument);
// find one document
const myDoc = await col.findOne();
// print to the console
console.log('mydoc:', myDoc);
} catch (e) {
console.log('got an error:', e);
} finally {
await client.close();
}
}
run();
res.send('done')
})
app.get('/getUser', (req, res) => {
console.log('entered getRating with parameters: ', req.body);
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
async function run() {
try {
console.log('entered try block');
await client.connect();
console.log('connected correctly to the server');
const db = client.db("db1");
const col = db.collection("user");
const query = { userId: req.body.userId };
const user = await col.findOne(query);
console.log('user recieved:', user);
res.send({
userId: user.userId,
balance: user.balance,
walletAddress: user.walletAddress,
timeCreated: user.timeCreated
})
} catch (e) {
console.log('got an error:', e);
res.send('error:', e)
} finally {
await client.close();
}
}
run();
})
console.log('restfulAPI started at port', port);