-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.js
50 lines (40 loc) · 1.34 KB
/
graph.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
42
43
44
45
46
47
48
49
50
var PooledConnection = require('cassandra-client').PooledConnection;
var hosts = [ 'localhost:9160' ];
var pool = new PooledConnection({ 'hosts' : hosts, 'keyspace' : 'Graphs', 'use_bigints' : true });
pool.on('log', function(level, message, obj) {
console.log('Log event: %s -- %j', level, message);
});
exports.following = function(req, res, next) {
pool.execute('SELECT * FROM Following WHERE KEY=?', [ req.params.id ],
function(err, rows) {
if (err) {
res.send(err);
} else {
var following = [];
for ( var i = 0; i < rows[0].colCount(); i++) {
var stringValue = rows[0].cols[i].value.toString();
following.push(parseInt(stringValue, 10));
}
res.send({ 'following' : following });
}
});
return next();
};
exports.postFollowing = function(req, res, next) {
var milliseconds = new Date().getTime();
var id = req.params.id;
var follow = req.params.follow;
pool.execute('UPDATE Following SET ?=? WHERE KEY=?', [ follow, milliseconds, id ], function(err, row) {
if (err) {
res.send(err);
} else {
res.send('%d is now following %d', req.params.id, req.params.follow);
}
});
return next();
};
exports.shutdown = function() {
pool.shutdown(function() {
console.log("Connection pool shutting down...");
});
};