aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2012-12-06 20:23:34 -0800
committerAdam Soltys <[email protected]>2012-12-06 20:23:34 -0800
commit749787381c3fdcfb45852b2234b8099483ff0cf5 (patch)
tree776f0b44a839426bcdf1e628eadc1b4b57b6638d /routes
parent00a08d7c8b10b726830505b58a045a49bb2046fe (diff)
downloadcoinos-server-749787381c3fdcfb45852b2234b8099483ff0cf5.tar.gz
coinos-server-749787381c3fdcfb45852b2234b8099483ff0cf5.zip
Code re-org
Diffstat (limited to 'routes')
-rw-r--r--routes/calculator.coffee30
-rw-r--r--routes/sessions.coffee19
-rw-r--r--routes/transactions.coffee39
-rw-r--r--routes/users.coffee45
4 files changed, 133 insertions, 0 deletions
diff --git a/routes/calculator.coffee b/routes/calculator.coffee
new file mode 100644
index 00000000..0fe11a28
--- /dev/null
+++ b/routes/calculator.coffee
@@ -0,0 +1,30 @@
+exports.new = (req, res) ->
+ res.render('calculator/setup', js: (-> global.js), css: (-> global.css))
+
+exports.show = (req, res) ->
+ res.render('calculator/show',
+ js: (-> global.js),
+ css: (-> global.css),
+ )
+
+exports.ticker = (req, res) ->
+ options =
+ host: 'bitcoincharts.com',
+ path: "/t/depthcalc.json?symbol=#{req.query.symbol}&type=#{req.query.type}&amount=#{req.query.amount}&currency=true"
+
+ require('http').get(options, (r) ->
+ r.setEncoding('utf-8')
+ r.on('data', (chunk) ->
+ try
+ exchange = 1000 / JSON.parse(chunk).out
+ exchange = (Math.ceil(exchange * 100) / 100).toString()
+ catch e
+ exchange = ""
+
+ res.writeHead(200,
+ 'Content-Length': exchange.length,
+ 'Content-Type': 'text/plain')
+ res.write(exchange)
+ res.end()
+ )
+ )
diff --git a/routes/sessions.coffee b/routes/sessions.coffee
new file mode 100644
index 00000000..631004ca
--- /dev/null
+++ b/routes/sessions.coffee
@@ -0,0 +1,19 @@
+module.exports = (passport) ->
+ new: (req, res) ->
+ res.render('sessions/new',
+ js: (-> global.js),
+ css: (-> global.css)
+ )
+
+ create: (req, res, next) ->
+ passport.authenticate('local', (err, user, info) ->
+ if (err)
+ return next(err)
+ if (!user)
+ return res.redirect('/login')
+ req.login(user, (err) ->
+ if (err)
+ return next(err)
+ return res.redirect('/' + user.username)
+ )
+ )(req, res, next)
diff --git a/routes/transactions.coffee b/routes/transactions.coffee
new file mode 100644
index 00000000..92e566fa
--- /dev/null
+++ b/routes/transactions.coffee
@@ -0,0 +1,39 @@
+db = require("redis").createClient()
+
+module.exports =
+ json: (req, res) ->
+ user = req.params.user
+ r = 'transactions': []
+
+ db.lrange("#{user}:transactions", 0, -1, (err, transactions) ->
+ process = (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)
+
+ i = 0
+ db.hgetall("#{user}:transactions:#{transactions[i++]}", process)
+ )
+
+ 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()
+ )
+ )
+ )
+
+ index: (req, res) ->
+ res.render('transactions/index',
+ user: req.params.user,
+ js: (-> global.js),
+ css: (-> global.css)
+ )
+
diff --git a/routes/users.coffee b/routes/users.coffee
new file mode 100644
index 00000000..ea927583
--- /dev/null
+++ b/routes/users.coffee
@@ -0,0 +1,45 @@
+db = require("redis").createClient()
+bcrypt = require('bcrypt')
+
+module.exports =
+ json: (req, res) ->
+ db.hgetall(req.params.user, (err, obj) ->
+ delete obj['password']
+ res.write(JSON.stringify(obj))
+ res.end()
+ )
+
+ show: (req, res) ->
+ res.render('calculator/show',
+ user: req.params.user,
+ js: (-> global.js),
+ css: (-> global.css)
+ )
+
+ new: (req, res) ->
+ res.render('users/new',
+ js: (-> global.js),
+ css: (-> global.css),
+ )
+
+ create: (req, res) ->
+ bcrypt.hash(req.body.password, 12, (err, hash) ->
+ req.body.password = hash
+ db.hmset(req.body.username, req.body, ->
+ res.redirect(req.body.username)
+ )
+ )
+
+ edit: (req, res) ->
+ res.render('calculator/setup',
+ user: req.params.user,
+ js: (-> global.js),
+ css: (-> global.css)
+ )
+
+ update: (req, res) ->
+ return unless req.params.user is req.user.username or
+ req.user.username is 'admin'
+ db.hmset(req.params.user, req.body, ->
+ res.redirect("/#{req.params.user}")
+ )