Browse Source

Promises turned to async/await

stuff
xcps 7 years ago
parent
commit
8fc53880a2
  1. 13
      routes/address.js
  2. 7
      routes/block.js
  3. 10
      routes/index.js
  4. 25
      routes/search.js
  5. 9
      routes/transaction.js

13
routes/address.js

@ -3,13 +3,13 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/:address', function(req, res, next) { router.get('/:address', async function(req, res, next) {
const address = encodeURI(req.params.address); const addrss = encodeURI(req.params.address);
models.Address.findOne({ const address = await models.Address.findOne({
where: { where: {
address, address: addrss,
}, },
include: { include: {
model: models.Vout, model: models.Vout,
@ -17,8 +17,8 @@ router.get('/:address', function(req, res, next) {
model: models.Transaction, model: models.Transaction,
}, },
}, },
}) });
.then((address) => {
if (address === null) { if (address === null) {
res.status(404).render('404'); res.status(404).render('404');
return; return;
@ -30,6 +30,5 @@ router.get('/:address', function(req, res, next) {
txes, txes,
}); });
}); });
});
module.exports = router; module.exports = router;

7
routes/block.js

@ -3,9 +3,9 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/:hash', function(req, res, next) { router.get('/:hash', async function(req, res, next) {
const hash = encodeURI(req.params.hash); const hash = encodeURI(req.params.hash);
models.Block.findOne({ const block = await models.Block.findOne({
where: { where: {
hash, hash,
}, },
@ -13,7 +13,6 @@ router.get('/:hash', function(req, res, next) {
model: models.Transaction, model: models.Transaction,
}, },
}) })
.then((block) => {
if (block === null) { if (block === null) {
res.status(404).render('404'); res.status(404).render('404');
return; return;
@ -22,7 +21,7 @@ router.get('/:hash', function(req, res, next) {
res.render('block', { res.render('block', {
block, block,
}); });
});
}); });
module.exports = router; module.exports = router;

10
routes/index.js

@ -3,17 +3,15 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { router.get('/', async function(req, res, next) {
models.Block.findAll({
const blocks = await models.Block.findAll({
order: [['height', 'DESC']], order: [['height', 'DESC']],
limit: 30, limit: 30,
}) });
.then((blocks) => {
res.render('index', { res.render('index', {
blocks, blocks,
}); });
}); });
});
module.exports = router; module.exports = router;

25
routes/search.js

@ -3,44 +3,43 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
/* GET home page. */ /* GET home page. */
router.post('/', function(req, res, next) { router.post('/', async function(req, res, next) {
const search = encodeURI(req.body.search); const search = encodeURI(req.body.search);
models.Address.findOne({ // looking for address
const address = await models.Address.findOne({
where: { where: {
address: search, address: search,
}, },
}) });
.then((address) => {
if (address) { if (address) {
res.redirect(`/address/${address.address}`); res.redirect(`/address/${address.address}`);
return; return;
} }
models.Transaction.findOne({
// looking for transaction
const transaction = await models.Transaction.findOne({
where: { where: {
txid: search, txid: search,
}, },
}) });
.then((transaction) => {
if (transaction) { if (transaction) {
res.redirect(`/transaction/${transaction.txid}`); res.redirect(`/transaction/${transaction.txid}`);
return; return;
} }
models.Block.findOne({
// looking for block
const block = await models.Block.findOne({
where: { where: {
hash: search, hash: search,
}, },
}) });
.then((block) => {
if (block) { if (block) {
res.redirect(`/block/${block.hash}`); res.redirect(`/block/${block.hash}`);
return; return;
} }
res.status(404).render('404'); res.status(404).render('404');
}); });
});
});
});
module.exports = router; module.exports = router;

9
routes/transaction.js

@ -3,10 +3,10 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/:txid', function(req, res, next) { router.get('/:txid', async function(req, res, next) {
const txid = encodeURI(req.params.txid); const txid = encodeURI(req.params.txid);
models.Transaction.findOne({ const transaction = await models.Transaction.findOne({
where: { where: {
txid, txid,
}, },
@ -22,8 +22,7 @@ router.get('/:txid', function(req, res, next) {
model: models.Transaction, model: models.Transaction,
as: 'txtx', as: 'txtx',
}], }],
}) });
.then((transaction) => {
if (transaction === null) { if (transaction === null) {
res.status(404).render('404'); res.status(404).render('404');
return; return;
@ -43,6 +42,4 @@ router.get('/:txid', function(req, res, next) {
}); });
}); });
});
module.exports = router; module.exports = router;

Loading…
Cancel
Save