Skip to content

Commit c8bb76b

Browse files
committed
added snippit vote capabilities (route, api, client-side)
1 parent a19531f commit c8bb76b

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

app/scripts/controllers/main.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
angular.module('codeSearchApp')
4-
.controller('MainCtrl', function ($scope, $http, $timeout, $modal, $log) {
4+
.controller('MainCtrl', function ($rootScope, $scope, $http, $timeout, $modal, $log) {
55
$scope.isCollapsed = {collapse:false};
66
$scope.codeSnippits = [];
77
$scope.fileUrl = {url: ''};
@@ -55,6 +55,29 @@ angular.module('codeSearchApp')
5555
});
5656
};
5757

58+
$scope.tempId = 1234;
59+
60+
$scope.snippitVote = function(votePreference, snippitObj) {
61+
// if (!$rootScope.currentUser) {
62+
// $scope.notSignedIn = true;
63+
// $timeout(function(){
64+
// $scope.notSignedIn = false;
65+
// }, 3000);
66+
// }
67+
68+
var snippit = snippitObj.snippit;
69+
70+
var snippitData = {
71+
snippit: snippit,
72+
votePreference: votePreference
73+
};
74+
console.log(snippitData);
75+
$http.post('/api/snippitVote', snippitData)
76+
.success(function(data){
77+
console.log(data);
78+
});
79+
};
80+
5881
$scope.openModal = function(size, snippitObj) {
5982

6083
var modalInstance = $modal.open({

app/views/partials/main.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ <h3>Results:</h3>
9191
<div class="snippit" ng-repeat="snippitObj in codeSnippits |
9292
startPageFrom : page.currPage*page.resultsPerPage |
9393
limitTo : page.resultsPerPage track by $index">
94-
<pre ng-click="openModal('lg', snippitObj)">{{snippitObj.snippit}}</pre>
94+
<div class="row">
95+
<pre class="col-md-11" ng-click="openModal('lg', snippitObj)" >{{snippitObj.snippit}}</pre>
96+
<div class="col-md-1">
97+
<i class="glyphicon glyphicon-thumbs-up" ng-click="snippitVote(1, snippitObj)"></i>
98+
<i class="glyphicon glyphicon-thumbs-down" ng-click="snippitVote(-1, snippitObj)"></i>
99+
<p ng-show="notSignedIn">Please sign in to vote</p>
100+
</div>
101+
</div>
95102
</div>
96103
<div>
97104
<button ng-disabled="page.currPage === 0"

lib/controllers/voting.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var mongoose = require('mongoose');
2+
var Snippit = mongoose.model('Snippit');
3+
4+
exports.snippitVote = function(req, res, next) {
5+
// var githubId = req.user.github_id // fix later
6+
var githubId = 5555;
7+
var votePref = req.body.votePreference;
8+
var snippit = req.body.snippit;
9+
10+
var setObj = {
11+
snippit: snippit
12+
};
13+
14+
setObj["github_id." + githubId] = votePref;
15+
16+
17+
Snippit.findOneAndUpdate ( { snippit: snippit }, { $set: setObj }, { upsert: true }, function (err, data) {
18+
if (err) console.log(err);
19+
20+
var score = 0;
21+
for (var user in data.github_id) {
22+
score += data.github_id[user];
23+
}
24+
25+
Snippit.findOneAndUpdate ( { snippit: req.body.snippit }, { $set: {score: score } }, function (err, data) {
26+
if (err) console.log(err);
27+
res.send("success");
28+
});
29+
});
30+
31+
};

lib/routes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var api = require('./controllers/api'),
66
session = require('./controllers/session'),
77
middleware = require('./middleware'),
88
files = require('./controllers/files'),
9+
voting = require('./controllers/voting'),
910
passport = require('passport');
1011

1112
/**
@@ -38,6 +39,9 @@ module.exports = function(app) {
3839
app.route('/api/testDirectorySearch')
3940
.post(files.pushFileToDirectory);
4041

42+
app.route('/api/snippitVote')
43+
.post(voting.snippitVote);
44+
4145
// All undefined api routes should return a 404
4246
app.route('/api/*')
4347
.get(function(req, res) {

0 commit comments

Comments
 (0)