diff options
| author | Adam Soltys <[email protected]> | 2016-10-14 20:48:01 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2016-10-14 20:48:01 -0700 |
| commit | 6f906c289422c9d655e194390bb871b2e276f326 (patch) | |
| tree | 0973e01f99a1ac187d1431804b3768774ef1a02e /src | |
| parent | 60b9265ca0ea5f880dea5c6d2cdef177e9d8dc86 (diff) | |
| download | coinos-server-6f906c289422c9d655e194390bb871b2e276f326.tar.gz coinos-server-6f906c289422c9d655e194390bb871b2e276f326.zip | |
Reimplement transactions index function with promises
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.coffee | 1 | ||||
| -rw-r--r-- | src/redis.coffee | 4 | ||||
| -rw-r--r-- | src/routes/transactions.coffee | 50 |
3 files changed, 30 insertions, 25 deletions
diff --git a/src/app.coffee b/src/app.coffee index fc1eee37..903fa879 100644 --- a/src/app.coffee +++ b/src/app.coffee @@ -75,7 +75,6 @@ do fetchRates = -> ) setTimeout(fetchRates, 120000) - app.get('/', cache, sessions.new) app.get('/address', cache, (req, res) -> diff --git a/src/redis.coffee b/src/redis.coffee index 38524595..7f28fae0 100644 --- a/src/redis.coffee +++ b/src/redis.coffee @@ -1,5 +1,7 @@ +Promise = require('bluebird') + settings = port: 6379 host: '127.0.0.1' -module.exports = require('redis').createClient(settings.port, settings.host) +module.exports = Promise.promisifyAll(require('redis')).createClient(settings.port, settings.host) diff --git a/src/routes/transactions.coffee b/src/routes/transactions.coffee index 445bbc83..70447901 100644 --- a/src/routes/transactions.coffee +++ b/src/routes/transactions.coffee @@ -1,7 +1,12 @@ +Promise = require('bluebird') db = require('../redis') config = require("../config") moment = require('moment') +txid = (transaction, user) -> + return transaction if transaction.match(/[a-z]/i) + return user + ":transactions:" + transaction + module.exports = index: (req, res) -> res.render( @@ -15,32 +20,31 @@ module.exports = ) json: (req, res) -> - user = req.params.user - r = 'transactions': [] - - db.lrange("#{user}:transactions", 0, -1, (err, transactions) -> - if err or not transactions.length - res.write(JSON.stringify(r)) - return res.end() - - txid = -> - x = transactions[i++] - return x if x.match(/[a-z]/i) - return user + ":transactions:" + x + result = 'transactions': [] + promises = [] + pattern = req.params.user + if req.session.user.username is 'admin' + x = '*' - cb = (err, t) -> - r.transactions.push t - - if i >= transactions.length - res.write(JSON.stringify(r)) - res.end() - else - db.hgetall(txid(), cb) - - i = 0 - db.hgetall(txid(), cb) + db.keysAsync("#{pattern}:transactions").then((keys) -> + Promise.all(keys.map((key) -> + user = key.substr(0, key.indexOf(':')) + db.lrangeAsync(key, 0, -1).then((transactions) -> + Promise.all(transactions.map((transaction) -> + db.hgetallAsync(txid(transaction, user)).then((transaction) -> + if transaction + transaction.user = user + result.transactions.push(transaction) + ) + )) + ) + )) + ).then(-> + res.write(JSON.stringify(result)) + res.end() ) + create: (req, res) -> finish = -> db.hgetall("user:"+req.params.user.toLowerCase(), (err, user) -> |
