diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.coffee | 4 | ||||
| -rw-r--r-- | src/routes/transactions.coffee | 36 |
2 files changed, 29 insertions, 11 deletions
diff --git a/src/app.coffee b/src/app.coffee index 361a9a56..66ce32a5 100644 --- a/src/app.coffee +++ b/src/app.coffee @@ -73,11 +73,11 @@ app.get('/:user/edit', cache, authorize, users.edit) app.post('/:user', authorize, users.update) app.get('/:user/transactions.json', authorize, transactions.json) -app.post('/:user/transactions', authorize, transactions.create) +app.post('/:user/transactions', transactions.create) app.get('/:user/report', cache, authorize, transactions.index) app.get('/:user.json', authorize, users.json) -app.get('/:user', cache, authorize, users.show) +app.get('/:user', cache, users.show) app.use(require('connect-assets')(src: 'public')) diff --git a/src/routes/transactions.coffee b/src/routes/transactions.coffee index b4653362..06636a15 100644 --- a/src/routes/transactions.coffee +++ b/src/routes/transactions.coffee @@ -6,27 +6,45 @@ module.exports = r = 'transactions': [] db.lrange("#{user}:transactions", 0, -1, (err, transactions) -> - process = (err, t) -> + cb = (err, t) -> r.transactions.push t if i >= transactions.length res.write(JSON.stringify(r)) res.end() else - db.hgetall("#{user}:transactions:#{transactions[i++]}", process) + txid = transactions[i++] + if parseInt(txid) + txid = user + ":transactions:" + txid + + db.hgetall(txid, cb) i = 0 - db.hgetall("#{user}:transactions:#{transactions[i++]}", process) + + txid = transactions[i++] + if parseInt(txid) + txid = user + ":transactions:" + txid + + db.hgetall(txid, cb) ) create: (req, res) -> user = req.params.user - db.incr('transactions', (err, id) -> - db.hmset("#{user}:transactions:#{id}", req.body, -> - db.rpush("#{user}:transactions", id, -> - res.write(JSON.stringify(req.body)) - res.end() - ) + finish = -> + res.write(JSON.stringify(req.body)) + res.end() + + db.watch(req.body.txid) + db.exists(req.body.txid, (err, result) -> + if result + finish() + return + + multi = db.multi() + multi.hmset(req.body.txid, req.body) + multi.rpush("#{user}:transactions", req.body.txid) + multi.exec((err, replies) -> + finish() ) ) |
