From 5a4e3ff9bf1db2db4fc5ae8d3bd7dcefc8c58e25 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 7 Nov 2012 11:50:43 -0800 Subject: Store and use commission rate, move DB connection info into separate file --- .gitignore | 1 + app.js | 22 ++++++++++++++++++++-- config.js | 10 ++++++++++ db/migrations/002_add_commission.rb | 5 +++++ public/js/bitcoin_pos.js | 26 ++++++++++++++------------ views/calculator.html | 1 + views/index.html | 2 +- 7 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 config.js create mode 100644 db/migrations/002_add_commission.rb diff --git a/.gitignore b/.gitignore index 655c6073..30ab7dfa 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ transactions.json .rvmrc db/migrate.rb node_modules +config.js.sample diff --git a/app.js b/app.js index 10219d1c..dd932ce1 100644 --- a/app.js +++ b/app.js @@ -2,6 +2,7 @@ var express = require('express') , http = require('http') , path = require('path') , engines = require('consolidate') + , config = require('./config') , app = express(); app.engine('html', engines.hogan); @@ -17,10 +18,27 @@ app.get('/report', function(req, res) { res.render('report'); }); +app.get('/calculator', function(req, res) { + res.render('calculator'); +}); + +app.get('/client/:login', function(req, res) { + var connection = require('mysql').createConnection(config.database); + + connection.connect(); + connection.query("SELECT name, address, commission, logo FROM users WHERE login = ?", + [req.params.login], function(err, rows) { + res.write(JSON.stringify(rows)); + connection.end(); + res.end(); + } + ); +}); + app.get('/ticker', function(req, res) { var options = { host: 'bitcoincharts.com', - path: '/t/depthcalc.json?symbol=virtexCAD&type=bid&amount=1001¤cy=true' + path: '/t/depthcalc.json?symbol=virtexCAD&type=bid&amount=1000¤cy=true' } require('http').get(options, function(r) { @@ -32,7 +50,7 @@ app.get('/ticker', function(req, res) { }); app.get('/:client', function(req, res) { - res.render('calculator', { title: req.param('client') }); + res.render('calculator', { client: req.params.client }); }); app.listen(3000); diff --git a/config.js b/config.js new file mode 100644 index 00000000..7e9bc53b --- /dev/null +++ b/config.js @@ -0,0 +1,10 @@ +var config = {}; + +config.database = { + host : 'localhost', + user : 'root', + password : 'MPJzfq97', + database: 'vanbtc' +} + +module.exports = config; diff --git a/db/migrations/002_add_commission.rb b/db/migrations/002_add_commission.rb new file mode 100644 index 00000000..f8b5add8 --- /dev/null +++ b/db/migrations/002_add_commission.rb @@ -0,0 +1,5 @@ +class AddCommission < ActiveRecord::Migration + def change + add_column :users, :commission, :decimal, :precision => 10, :scale => 2 + end +end diff --git a/public/js/bitcoin_pos.js b/public/js/bitcoin_pos.js index 82d5f879..869e72e2 100644 --- a/public/js/bitcoin_pos.js +++ b/public/js/bitcoin_pos.js @@ -1,10 +1,10 @@ $(function() { var websocket = null; - var client = getParameterByName('client'); - var title = getParameterByName('title'); - var address = getParameterByName('address'); - var commission = getParameterByName('commission'); - var logo = getParameterByName('logo'); + var client = $('#client').val(); + var title = get('title'); + var address = get('address'); + var commission = get('commission'); + var logo = get('logo'); var exchange = 0; setupPage(); @@ -12,11 +12,12 @@ $(function() { setupSocket(); if (client != "") { - $.getJSON('client.json', { name: client }, function(data) { - title = data.title; - address = data.address; - commission = data.commission; - logo = data.logo; + $.getJSON('client/' + client, function(data) { + var user = data[0]; + title = user.title; + address = user.address; + commission = user.commission; + logo = user.logo; setupPage(); }); } @@ -88,6 +89,8 @@ $(function() { $('#address').html(address); $('#received').hide(); + + fetchExchangeRate(); } function setupSocket() { @@ -98,7 +101,6 @@ $(function() { websocket.onopen = function() { websocket.send('{"op":"addr_sub", "addr":"' + address + '"}'); - fetchExchangeRate(); } websocket.onerror = websocket.onclose = function() { @@ -144,7 +146,7 @@ $(function() { } }); -function getParameterByName(name) { +function get(name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); diff --git a/views/calculator.html b/views/calculator.html index bdc9fde8..88a7b47b 100644 --- a/views/calculator.html +++ b/views/calculator.html @@ -17,6 +17,7 @@