Skip to content

Commit

Permalink
added resource share with rating
Browse files Browse the repository at this point in the history
  • Loading branch information
effy971 committed Nov 12, 2020
1 parent 30eb540 commit 47a3a94
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
37 changes: 37 additions & 0 deletions frontend/src/Components/ResourceShare/DisplayLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,59 @@ import React from "react";
import upDownVote from "./upDownvote.png";
import "./DisplayLink.css";
const DisplayLink = (props) => {
const handleClickUpVote = () => {
fetch("/resourcerate", {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
rate: "upvote",
userName: document.cookie
.split("; ")
.find((row) => row.startsWith("access-token"))
.split("=")[1],
id: props.id,
}),
});
};

const handleClickDownVote = () => {
fetch("/resourcerate", {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
rate: "downvote",
userName: document.cookie
.split("; ")
.find((row) => row.startsWith("access-token"))
.split("=")[1],
id: props.id,
}),
});
};

return (
<div className="container-display-link">
<img
onClick={handleClickUpVote}
src={upDownVote}
alt="iconForUpvoteDownvote"
className="icon-upvote"
></img>

<img
onClick={handleClickDownVote}
src={upDownVote}
alt="iconForUpvoteDownvote"
className="icon-downvote"
></img>
<p>{props.link}</p>
<p>{props.linkName}</p>
<p>{props.userName}</p>
<p>{props.rating}</p>
<hr />
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/Components/ResourceShare/ShareMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const ShareMain = () => {
linkName={e.linkName}
userName={e.userName}
key={e._id}
id={e._id}
rating={e.rate}
/>
))
)}
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const adminDeletequestion = require("./admindeletequestion");
const adminViewquestion = require("./adminviewquestion");
const uploadResource = require("./uploadresource");
const getResource = require("./getresource");
const resourceRate = require("./resourcerate");

const auth = require("./auth");
const jwt = require("jsonwebtoken");
Expand Down Expand Up @@ -274,6 +275,15 @@ app.get("/getresource", async (req, res) => {
res.json(data);
});

app.put("/resourcerate", async (req, res) => {
const rate = req.body.rate;
const userName = req.body.userName;
const id = req.body.id;
console.log(id);
const result = await resourceRate(id, rate);
res.send("asd");
});

http.listen(PORT, (req, res) => {
console.log(`listening on port ${PORT}`);
});
41 changes: 41 additions & 0 deletions resourcerate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}
};

0 comments on commit 47a3a94

Please sign in to comment.