var express = require('express');
var router = express.Router();
var models = require('../models/index');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index');
});

router.post('/', function(req, res, next) {
  var auth = req.header("Authorization")
  var username_apikey = auth.split(" ");

  if(username_apikey.length !== 2)
  {
    res.status(400).send('Bad request!');
    return;
  }

  models.users.findOne({
    where: {username: username_apikey[0]}
  })
  .then(function(user) {
    user.getApi_key().then(function(apiKey) {
      if(apiKey.api_key !== username_apikey[1])
      {
        res.status(400).send('Bad request!');
        return;
      }

      var email = "";
      var response = "{\"hello\": \"Hello " + user.username + " your principal email is " + email + "\"}";
      res.json(response);
    });
  });
});

module.exports = router;

@@ -137,32 +137,33 @@
 et('/', function
+ 
 (req, res, next)
@@ -165,16 +165,18 @@
 next) %7B%0A
+  
   res.re
@@ -220,16 +220,17 @@
 function
+ 
 (req, re
@@ -242,16 +242,18 @@
 xt) %7B%0A  
+  
 var auth
@@ -283,16 +283,18 @@
 ation%22)%0A
+  
   var us
@@ -295,26 +295,25 @@
 var username
-_a
+A
 pikey = auth
@@ -328,18 +328,71 @@
  %22);%0A%0A  
-if
+  /* Authorization format: %7BUSERNAME API_KEY%7D*/%0A    if 
 (usernam
@@ -388,26 +388,25 @@
 if (username
-_a
+A
 pikey.length
@@ -416,13 +416,15 @@
 = 2)
-%0A 
  %7B%0A
+    
     
@@ -442,39 +442,42 @@
 400).send('B
-ad request!');%0A
+AD REQUEST');%0A    
     return;%0A
@@ -480,101 +480,317 @@
 rn;%0A
+  
   %7D%0A%0A  
-models.users.findOne(%7B%0A    where: %7Busername: username_apikey%5B0%5D%7D%0A  %7D)%0A  .then(
+  findUser(usernameApikey, function (user) %7B%0A        var email = user.emails%5B0%5D; // the principal email object%0A        res.json(%22%7B%5C%22hello%5C%22: %5C%22Hi %22 + user.username + %22 your principal email is %22 + email.email + %22%5C%22%7D%22);%0A    %7D, function () %7B%0A        res.status(401).send('UNAUTHORIZED');%0A    %7D, 
 function
 (use
@@ -785,29 +785,26 @@
 function
+ 
 (
-user
 ) %7B%0A    
 user.get
@@ -799,74 +799,207 @@
     
-user.getApi_key().then(function(apiKey) %7B%0A      if(apiKey.
+    res.status(429).send('TOO MANY REQUESTS');%0A    %7D);%0A%7D);%0A%0Afunction findUser(usernameApikey, fUserOk, fUserBad, fManyUserBad) %7B%0A    /*%0A     * find user with username = usernameApikey%5B0%5D and 
 api_key 
 !== 
@@ -994,18 +994,16 @@
 api_key 
-!=
 = userna
@@ -1008,18 +1008,17 @@
 name
-_a
+A
 pikey%5B1%5D
 )%0A  
@@ -1017,260 +1017,600 @@
 y%5B1%5D
-)
 %0A     
- %7B%0A        res.status(400).send('Bad request!');%0A        return;%0A      %7D%0A%0A      var email = %22%22;%0A      var response = %22%7B%5C%22hello%5C%22: %5C%22Hello %22 + user.username + %22 your principal email is %22 + email + %22%5C%22%7D%22;%0A      res.json(response);%0A    %7D);%0A
+* and with an email like primary%0A     */%0A    models.users.findOne(%7B%0A            include: %5B%0A                %7B%0A                    model: models.api_keys,%0A                    where: %7Bapi_key: usernameApikey%5B1%5D%7D%0A                %7D,%0A                %7B%0A                    model: models.emails,%0A                    where: %7Bprimary: true%7D%0A                %7D%5D,%0A            where: %7Busername: usernameApikey%5B0%5D%7D%0A        %7D)%0A        .then(function (user) %7B%0A            if (user) %7B%0A                fUserOk(user);%0A            %7D%0A            else %7B%0A                fUserBad();%0A            %7D%0A      
   %7D);%0A%7D
-)
 ;%0A%0Am

Refactor hello request