var pg = require('pg');

var engine = (function(){
  var min_id = undefined;
  var max_id = undefined;
  var min_max_query = 'SELECT min(id), max(id) FROM kanji';

  function getRandomID(){
    return Math.floor(Math.random() * (max_id - min_id + 1)) + min_id;
  }

  function getRandomKanji(callback){
    var id = getRandomID();
    var query = 'SELECT * FROM kanji WHERE id = '+id;
    pg.connect(process.env.DATABASE_URL, function(err, client, done) {
      client.query(query, function(err, result) {
        done();
        if (err){
          console.error(err); throw "Error " + err;
        }
        else{
          callback(result.rows[0]);
        }
      });
    });
  };

  function getMinMax(callback){
    pg.connect(process.env.DATABASE_URL, function(err, client, done) {
      client.query(min_max_query, function(err, result) {
        done();
        if (err){
          console.error(err); throw "Error " + err;
        }
        else{
          min_id = result.rows[0].min;
          max_id = result.rows[0].max;
        }
        if (null !== callback){
          callback();
        }
      });
    });
  };

  function getRandomKanjiWrapper(callback){
    if (typeof min_id == 'undefined' 
     || typeof max_id == 'undefined'){
      getMinMax(function(){getRandomKanji(callback);});
    }
    else{
      getRandomKanji(callback);
    }
  };


  return {
    "getRandomKanji":getRandomKanjiWrapper
  };
})();

exports.getRandomKanji = engine.getRandomKanji;

exports.route = function(request, response){
  engine.getRandomKanji(function(query_result){
    response.render('kanji', query_result);
  });
};
@@ -16,16 +16,154 @@
 e('pg');
+%0A  %0Afunction isEmpty(object)%7B%0A  for(var key in object)%7B%0A    if(object.hasOwnProperty(key))%7B%0A      return false;%0A    %7D%0A  %7D%0A  return true;%0A%7D
 %0A%0Avar en
@@ -295,16 +295,18 @@
 anji';%0A%0A
+%0A%0A
   functi
@@ -417,58 +417,34 @@
  get
-RandomKanji(callback)%7B%0A    var id = getRandomID();
+KanjiFromID(id, callback)%7B
 %0A   
@@ -781,32 +781,136 @@
     %7D);%0A    %7D);%0A
+  %7D%0A%0A  function getRandomKanji(callback)%7B%0A    var id = getRandomID();%0A    getKanjiFromID(id, callback);%0A
   %7D;%0A%0A  function
@@ -1357,37 +1357,33 @@
 unction 
-getRandomKanjiWrapp
+wrapDatabaseQu
 er
+y
 (callbac
@@ -1483,34 +1483,8 @@
 Max(
-function()%7BgetRandomKanji(
 call
@@ -1489,19 +1489,16 @@
 llback);
-%7D);
 %0A    %7D%0A 
@@ -1512,39 +1512,24 @@
 %7B%0A      
-getRandomKanji(
 callback
 );%0A    %7D
@@ -1512,32 +1512,33 @@
 %7B%0A      callback
+(
 );%0A    %7D%0A  %7D;%0A%0A%0A
@@ -1536,17 +1536,16 @@
 %7D%0A  %7D;%0A%0A
-%0A
   return
@@ -1572,88 +1572,226 @@
 ji%22:
-getRandomKanjiWrapper%0A  %7D;%0A%7D)();%0A%0Aexports.getRandomKanji = engine.getRandomKanji
+function(callback)%7BwrapDatabaseQuery(function()%7BgetRandomKanji(callback);%7D);%7D,%0A    %22getKanjiFromID%22:function(id, callback)%7BwrapDatabaseQuery(function()%7BgetKanjiFromID(id, callback);%7D);%7D,%0A    %22isEmpty%22:isEmpty%0A  %7D;%0A%7D)()
 ;%0A%0Ae
@@ -1840,30 +1840,33 @@
 %7B%0A  
-engine.getRandomKanji(
+var rendering_callback = 
 func
@@ -1932,13 +1932,219 @@
 lt);%0A  %7D
-);
+%0A  var query = request.query;%0A  if(query.hasOwnProperty(%22id%22))%7B%0A    console.log(query.id)%0A    engine.getKanjiFromID(query.id, rendering_callback);%0A  %7D else %7B%0A    engine.getRandomKanji(rendering_callback);%0A  %7D
 %0A%7D;

refactor + add possibility to query by id