-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresourcerate.js
41 lines (35 loc) · 907 Bytes
/
resourcerate.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
const { MongoClient, ObjectId } = require("mongodb");
module.exports = async function (id, rate) {
const uri = process.env.DB_CREDENTIALS;
const client = new MongoClient(uri);
let successOrFail = false;
try {
await client.connect();
const database = client.db("4learn");
const collection = database.collection("Resource");
let upOrDown = 0;
if (rate === "upvote") {
upOrDown = 1;
} else if (rate === "downvote") {
upOrDown = -1;
}
const result = await collection.updateOne(
{
_id: ObjectId(id),
},
{
$inc: {
rate: upOrDown,
},
}
);
console.log(`${result.insertedCount} documents were inserted into db`);
console.log(result.insertedCount > 0);
if (result.insertedCount > 0) {
successOrFail = true;
return true;
}
} finally {
await client.close();
}
};