Merge pull request #96 from cmgustavo/feature/01block-index-by-height

Feature/01block index by height
This commit is contained in:
Matias Alejo Garcia 2014-01-20 19:11:10 -08:00
commit 1a8307263a
7 changed files with 50 additions and 3 deletions

View File

@ -32,6 +32,20 @@ exports.show = function(req, res) {
}
};
/**
* Show block by Height
*/
exports.blockindex = function(req, res, next, height) {
Block.fromHeight(height, function(err, hash) {
if (err) {
console.log(err);
res.status(400).send('Bad Request'); // TODO
}
else {
res.jsonp(hash);
}
});
};
/**
* List of blocks by date

View File

@ -10,7 +10,7 @@ var Status = require('../models/Status'),
/**
* Status
*/
exports.show = function(req, res, next) {
exports.show = function(req, res) {
if (! req.query.q) {
res.status(400).send('Bad Request');

View File

@ -72,6 +72,15 @@ BlockSchema.statics.load = function(id, cb) {
}).exec(cb);
};
BlockSchema.statics.fromHeight = function(height, cb) {
var rpc = new RpcClient(config.bitcoind);
var hash = {};
rpc.getBlockHash(height, function(err, bh){
if (err) return cb(err);
hash.blockHash = bh.result;
cb(null, hash);
});
};
BlockSchema.statics.fromHash = function(hash, cb) {
this.findOne({

View File

@ -14,6 +14,9 @@ module.exports = function(app, historicSync) {
app.get('/api/block/:blockHash', blocks.show);
app.param('blockHash', blocks.block);
app.get('/api/block-index/:height', blocks.blockindex);
app.param('height', blocks.blockindex);
// Transaction routes
var transactions = require('../app/controllers/transactions');
app.get('/api/tx/:txid', transactions.show);

View File

@ -7,6 +7,10 @@ angular.module('insight').config(['$routeProvider',
when('/block/:blockHash', {
templateUrl: 'views/block.html'
}).
when('/block-index/:blockHeight', {
controller: 'BlocksController',
template: 'Redirecting...'
}).
when('/tx/:txId', {
templateUrl: 'views/transaction.html'
}).

View File

@ -1,9 +1,20 @@
'use strict';
angular.module('insight.blocks').controller('BlocksController',
function ($scope, $rootScope, $routeParams, $location, Global, Block, Blocks) {
function ($scope, $rootScope, $routeParams, $location, Global, Block, Blocks, BlockByHeight) {
$scope.global = Global;
if ($routeParams.blockHeight) {
BlockByHeight.get({
blockHeight: $routeParams.blockHeight
}, function(hash) {
$location.path('/block/' + hash.blockHash);
}, function() {
$rootScope.flashMessage = 'Bad Request';
$location.path('/');
});
}
$scope.list = function() {
Blocks.get({
blockDate: $routeParams.blockDate

View File

@ -23,5 +23,11 @@ angular.module('insight.blocks').factory('Block',
angular.module('insight.blocks').factory('Blocks',
function($resource) {
return $resource('/api/blocks');
return $resource('/api/blocks');
});
angular.module('insight.blocks').factory('BlockByHeight',
function($resource) {
return $resource('/api/block-index/:blockHeight');
});