var express = require('express');
var router = express.Router();

var mongoose = require('mongoose');
var UserTransportationPreferences = require('../models/UserTransportationPreferences.js');

// Benchmark test
router.post('/', (req, res) => {
  var userId = req.headers['x-user-id'];
  UserTransportationPreferences.findOne({ 'userId': userId }, (err, userPreferences) => {
    if (err || !userPreferences) {
      var userTransportationPreferences = new UserTransportationPreferences(req.body);
      userTransportationPreferences.userId = req.headers['x-user-id'];
      userTransportationPreferences.save((error, userPrefs) => {
        console.log('done saving userTransportationPreferences :', userPrefs);
        res.status(200).json({
          msg: 'done'
        });
      });
    } else {
      userPrefs.bike = req.body.bike;
      userPrefs.bus = req.body.bus;
      userPrefs.walk = req.body.walk;
      userPrefs.subway = req.body.subway;
      userPrefs.car = req.body.car;
      // http://mongoosejs.com/docs/api.html#model_Model-save
      userPrefs.save((err, userPrefs) => {
        res.json(200, userPrefs);
      });
    }
  });

});

router.get('/', (req, res) => {
  var userId = req.headers['x-user-id'];
  UserTransportationPreferences.findOne({ 'userId': userId }, (err, userPreferences) => {
    if (err || !userPreferences) {
      res.status(418).json({
        "msg": "I am a teapot ! User preferences not found"
      });
    } else {
      res.status(200).json({
        userPreferences: userPreferences
      });
    }
  });
});

router.put('/', (req, res) => {
  UserTransportationPreferences.findOne({ 'userId': req.body.userId }, (err, userPrefs) => {
    userPrefs.bike = req.body.bike;
    userPrefs.bus = req.body.bus;
    userPrefs.walk = req.body.walk;
    userPrefs.subway = req.body.subway;
    userPrefs.car = req.body.car;
    // http://mongoosejs.com/docs/api.html#model_Model-save
    userPrefs.save((err, userPrefs) => {
      res.json(200, userPrefs);
    });
  });
});

module.exports = router;

@@ -800,32 +800,38 @@
 %7B%0A      userPref
+erence
 s.bike = req.bod
@@ -844,32 +844,38 @@
 ;%0A      userPref
+erence
 s.bus = req.body
@@ -886,32 +886,38 @@
 ;%0A      userPref
+erence
 s.walk = req.bod
@@ -930,32 +930,38 @@
 ;%0A      userPref
+erence
 s.subway = req.b
@@ -978,32 +978,38 @@
 ;%0A      userPref
+erence
 s.car = req.body
@@ -1082,32 +1082,38 @@
 e%0A      userPref
+erence
 s.save((err, use
@@ -1109,32 +1109,38 @@
 e((err, userPref
+erence
 s) =%3E %7B%0A        
@@ -1153,32 +1153,38 @@
 on(200, userPref
+erence
 s);%0A      %7D);%0A  

fix error on userprefs