<commit_before>module.exports = function(sio, mongoObject){
	return {
		vote: function(req, res){
			castVote(req.params.id, function(){
				sio.sockets.emit('vote cast', req.params.id);
				res.send('ok');
			});
		},
		results: function(req, res) {
      countVotes(function(results){
				res.send(results);
      });
		}
	}

  function countVotes(success) {
    
    var MongoClient = mongoObject.client;
    var format = mongoObject.format;
    var database = mongoObject.database;
    var collection_name = mongoObject.collection_name;

    MongoClient.connect(database, function(err, db) {
      if(err){
	      console.log('there was an error!', err, db);
	      success('foo');
      } else {
				var collection = db.collection(collection_name);

				collection.aggregate( [ {$group: { _id: '$vote', count: { $sum: 1 } } }], function(err, rsl) {
					console.log(rsl);
					success(rsl);
				});
      }
    });

  }

  function castVote(id, success) {

    var MongoClient = mongoObject.client;
    var format = mongoObject.format;
    var database = mongoObject.database;
    var collection_name = mongoObject.collection_name;

    MongoClient.connect(database, function(err, db) {

      if(err) throw err;

      var collection = db.collection(collection_name);

      var data = {
        vote: id
      };
      
      collection.insert(data, function(err, docs) {

	      if(!err){
	        success();
	      }
      });

	    db.close();

    });
  }

}<commit_msg>update cast vote & count vote<commit_after>module.exports = function(sio, mongoObject) {

	return {
		vote: function(req, res){
			castVote(req.params.id, function(){
				sio.sockets.emit('vote cast', req.params.id);
				res.send('ok');
			});
		},
		results: function(req, res) {
      countVotes(function(results){
				res.send(results);
      });
		}
	}

  // var collectionName = 'test_insert';

  function countVotes(success) {

    DB(function(db) {
      var collection = db.collection('test_insert');
      collection.aggregate( [ {$group: { _id: '$vote', count: { $sum: 1 } } }], function(err, rsl) {
        console.log(rsl);
        success(rsl);
      });
    });
  }

  function castVote(id, success) {

    DB(function(db) {
      var collection = db.collection('test_insert');
      var data = {
        vote: id
      };
      collection.insert(data, function(err, docs) {
        if(!err) {
          success();
        }
      });
    });

  }

}
{'fraction_non_alphanumeric': 0.15296523517382413, 'fraction_numerical': 0.0008179959100204499, 'mean_word_length': 2.5295815295815296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7503503', 'n_tokens_mistral': 878, 'n_tokens_neox': 791, 'n_words': 150}