<commit_before>var db = require('./db.js');
var Promise = require('bluebird');
var ctrl = require('./controller');

module.exports = function(app){

  // Get kanji
  app.get('/api/kanji', function(req, res){

    var c = req.cookies;
    var id = c.session.id;

    db.getNextFromQueue_(id)
      .then(function(kanji){

        if (kanji) {
          res.status(200).send(kanji);
        } else {
          res.status(404).send("Nothing more to study.");
        }

      });

  });

  // Get facts
  app.get('/api/facts', function(req, res){

    var c = req.cookies;
    var id = c.session.id;

    db.getFacts_(id)
      .then(function(rows){

        res.status(200).send(rows);

      });

  });

  // Add a new fact to a user's collection
  app.post('/api/facts', function(req, res){

    if (!req.body.fact){

      res.status(400).send("Error: POST must include a fact.");

    } else {

      var c = req.cookies;
      var id = c.session.id;
      var name = c.session.name;
      var fact = req.body.fact;

      db.addWord_2(id, fact)
        .then(function(added){
          res.status(201).send(["Success! Fact added to ", name, "'s collection: ", fact].join(''));
        });

    }

  });

  // Add new user & password to database
  app.post('/api/signup', function(req,res){

    if (!req.body.username || !req.body.password){

      res.status(400).send("Error: POST must include a username and password.");

    } else {

      var username = req.body.username;
      var password = req.body.password;

      db.checkUser_(username, password)
        .then(function(user){

          if (user.exists){

            res.status(409).send("User already in database.");

          } else {

            db.addNewUser_(username, password)
              .then(function(user){

                res.status(201).send(user);
                
              });

          }

        });

    }

  });

  // Authenticate username/password
  app.post('/api/login', function(req,res){

    var c = req.cookies;
    if (c && c.session && c.session.id) {
      res.status(200).send('Already logged in as ' + c.session.name);
      return;
    }

    if (!req.body.username || !req.body.password){

      res.status(400).send("Error: POST must include a name and password.");

    } else {

      var name = req.body.username;
      var password = req.body.password;

      db.checkUser_(name, password)
      .then(function(session){

        if (session.data){

          res.cookie('session', session.data, {httpOnly: true});
          res.status(200).send(session.data);

        } else {

          res.status(403).send("Error: username / password incorrect.");

        }

      });

    }

  });
  // Logout & clear cookies
  app.post('/api/logout', function(req,res){
    res.clearCookie('session');
    res.status(200).send('User signed out.');
  });

  app.get('/', function(req,res){

    res.sendFile('public/index.html');

  });
};<commit_msg>Refactor routes.js to user Express router<commit_after>var db = require('./db.js');
var router = require('express').Router();
var Promise = require('bluebird');
var ctrl = require('./controller');

// Get kanji
router.get('/kanji', function(req, res){

  var c = req.cookies;
  var id = c.session.id;

  db.getNextFromQueue_(id)
    .then(function(kanji){

      if (kanji) {
        res.status(200).send(kanji);
      } else {
        res.status(404).send("Nothing more to study.");
      }

    });

});

// Get facts
router.get('/facts', function(req, res){

  var c = req.cookies;
  var id = c.session.id;

  db.getFacts_(id)
    .then(function(rows){

      res.status(200).send(rows);

    });

});

// Add a new fact to a user's collection
router.post('/facts', function(req, res){

  if (!req.body.fact){

    res.status(400).send("Error: POST must include a fact.");

  } else {

    var c = req.cookies;
    var id = c.session.id;
    var name = c.session.name;
    var fact = req.body.fact;

    db.addWord_2(id, fact)
      .then(function(added){
        res.status(201).send(["Success! Fact added to ", name, "'s collection: ", fact].join(''));
      });

  }

});

// Add new user & password to database
router.post('/signup', function(req,res){

  if (!req.body.username || !req.body.password){

    res.status(400).send("Error: POST must include a username and password.");

  } else {

    var username = req.body.username;
    var password = <PASSWORD>;

    db.checkUser_(username, password)
      .then(function(user){

        if (user.exists){

          res.status(409).send("User already in database.");

        } else {

          db.addNewUser_(username, password)
            .then(function(user){

              res.status(201).send(user);
              
            });

        }

      });

  }

});

// Authenticate username/password
router.post('/login', function(req,res){

  var c = req.cookies;
  if (c && c.session && c.session.id) {
    res.status(200).send('Already logged in as ' + c.session.name);
    return;
  }

  if (!req.body.username || !req.body.password){

    res.status(400).send("Error: POST must include a name and password.");

  } else {

    var name = req.body.username;
    var password = <PASSWORD>;

    db.checkUser_(name, password)
    .then(function(session){

      if (session.data){

        res.cookie('session', session.data, {httpOnly: true});
        res.status(200).send(session.data);

      } else {

        res.status(403).send("Error: username / password incorrect.");

      }

    });

  }

});
// Logout & clear cookies
router.post('/logout', function(req,res){
  res.clearCookie('session');
  res.status(200).send('User signed out.');
});

router.get('/', function(req,res){

  res.sendFile('public/index.html');

});

module.exports = function(app){
  app.use('/api', router);
};
{'fraction_non_alphanumeric': 0.17353951890034364, 'fraction_numerical': 0.013745704467353952, 'mean_word_length': 2.3111490329920366, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2080441', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1830, 'n_words': 392}