Skip to content

Commit 1d7a23e

Browse files
Merge pull request #106 from lakshyabatman/development
Logic for upvotes and downvotes are updated
2 parents 4e879b0 + c43baf0 commit 1d7a23e

File tree

8 files changed

+99
-306
lines changed

8 files changed

+99
-306
lines changed

controller/profile.controller.js

Lines changed: 19 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -31,80 +31,25 @@ module.exports = {
3131
res.redirect('/profile/profileview/' + req.user.Eid);
3232
})
3333
},
34-
upvote: function (req, res) {
35-
36-
var p = 0;
37-
38-
var check = 0;
39-
var comment = " ";
40-
for (q = 0; q < proj.downvote.length; q++) {
41-
if (req.body.client == proj.downvote[q]) {
42-
comment = "Cannot upvote and downvote a post ";
43-
check = 1;
44-
break;
45-
}
46-
47-
}
48-
49-
if (check == 1) {
50-
res.send({ proj, comment });
51-
}
52-
else {
53-
for (v = 0; v < proj.upvote.length; v++) {
54-
if (req.body.client == proj.upvote[v]) {
55-
p = 1;
56-
proj.upvote.pop(req.body.client);
57-
proj.save();
58-
console.log("already present");
59-
break;
60-
}
61-
}
62-
63-
if (p == 0) {
64-
proj.upvote.push(req.body.client);
65-
proj.save();
66-
}
67-
68-
res.send({ comment, proj });
69-
}
70-
},
71-
downvote: function(req,res){
72-
proj.findOne({ proid: req.body.project }).then(function (proj) {
73-
74-
var p = 0;
75-
var check = 0;
76-
var comment = '';
77-
78-
for (q = 0; q < proj.upvote.length; q++) {
79-
if (req.body.client == proj.upvote[q]) {
80-
comment = "Cannot do upvote downvote at same post"
81-
check = 1;
82-
break;
83-
}
84-
}
85-
86-
if (check == 1) {
87-
res.send({ comment, proj });
88-
}
89-
else {
90-
for (v = 0; v < proj.downvote.length; v++) {
91-
if (req.body.client == proj.downvote[v]) {
92-
p = 1;
93-
proj.downvote.pop(req.body.client);
94-
proj.save();
95-
console.log("already present");
96-
break;
97-
}
98-
}
99-
100-
if (p == 0) {
101-
proj.downvote.push(req.body.client);
102-
proj.save();
103-
}
104-
105-
res.send({ comment, proj });
106-
}
107-
});
34+
upDownVote: function (req,res) {
35+
proj.findOne({proid: req.body.project}).then((pro)=>{
36+
if(pro.upDownVote.get(req.body.client)){
37+
if(pro.upDownVote.get(req.body.client)=="-1"){
38+
pro.upDownVote.set(req.body.client,"+1")
39+
return pro.save()
40+
41+
}else{
42+
pro.upDownVote.set(req.body.client,"-1")
43+
return pro.save()
44+
45+
}
46+
}else{
47+
pro.upDownVote.set(req.body.client,req.body.vote)
48+
return pro.save()
49+
}
50+
}).catch((err)=>{
51+
return err
52+
})
10853
},
10954
ch2 : function(req,res){
11055
res.render('main-landing', { sign: req.user });

db/projects.txt

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
{
22
"_id" : ObjectId("5b5201258bf7024f24e85126"),
3-
"upvote" : [
4-
""
5-
],
6-
"downvote" : [
7-
""
8-
],
3+
"upDownVote":{},
94
"pname" : "My first project",
105
"pid" : "104495411584749551646",
116
"github" : "xyz",
@@ -16,13 +11,7 @@
1611
},
1712
{
1813
"_id" : ObjectId("5b4f8beb7186aa40844df9b2"),
19-
"upvote" : [
20-
"",
21-
"104495411584749551646"
22-
],
23-
"downvote" : [
24-
""
25-
],
14+
"upDownVote" :{},
2615
"pname" : "My first project",
2716
"pid" : "104495411584749551646",
2817
"github" : "xyz",
@@ -33,13 +22,7 @@
3322
},
3423
{
3524
"_id" : ObjectId("5b5c2a66bb47f62b54a8ee40"),
36-
"upvote" : [
37-
""
38-
],
39-
"downvote" : [
40-
"",
41-
"104495411584749551646"
42-
],
25+
"upDownVote":{},
4326
"pname" : "My first project",
4427
"pid" : "115169838008905885474",
4528
"github" : "xyz",
@@ -50,13 +33,7 @@
5033
},
5134
{
5235
"_id" : ObjectId("5b5c83880380e426bce91822"),
53-
"upvote" : [
54-
""
55-
],
56-
"downvote" : [
57-
"",
58-
"104495411584749551646"
59-
],
36+
"upDownVote":{},
6037
"pname" : "My first ",
6138
"pid" : "104495411584749551646",
6239
"github" : "",

package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routes/profile.routes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ route.get('/profileview/:sd', auth, url, profileController.profileViewSd);
2525

2626
route.post('/publish',auth ,url, profileController.publish);
2727

28-
route.post('/upvote', auth,url, jsonParser, profileController.upvote);
2928

30-
route.post('/downvote', url, jsonParser, profileController.downvote);
29+
route.post('/upDownVote',auth,url,jsonParser,profileController.upDownVote);
30+
3131

3232
route.get('/ch2',auth, profileController.ch2);
3333

schema/project.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ Lang:{
2020
content:{
2121
type:String
2222
},
23-
upvote:{
24-
type:[String]
25-
},
26-
downvote:{
27-
type:[String]
23+
upDownVote:{
24+
type:Map,
25+
of: String,
2826
},
2927
proid:{
3028
type:Number

0 commit comments

Comments
 (0)