<commit_before>var models = require('../models');

exports.list = function(User){
  return function(req, res) {

    User.find().lean().exec(function (err, users) {
      return res.render('userlist', {
        "userlist" : users
      });
    });

  };
}

exports.save = function(User) {
	return function(req, res) {
    	var form_data = req.body;

	    var username = req.session.username;
	    if (username) {
	    	User.find({ name: username }, function(error, result) {
		      if (error) res.redirect('/');

		      if (result.length == 0) {
		      } else {
		        var usr = result[0];

            var newBreathingsession = new models.BreathingSession({
              "date": new Date(),
              "data": form_data.data,
              "_user": usr.id,
              "username": usr.name, 
              "guidance": form_data.guidance
            });
            newBreathingsession.save(function(err) {if (err) {console.log(err)}});

            res.send(200);
		      }
		    });
	    } else {
        var newBreathingsession = new models.BreathingSession({
                "date": new Date(),
                "data": form_data.data,
                "username": "TESTING",
                "guidance": form_data.guidance
              });
        
              newBreathingsession.save(function(err) {if (err) {console.log(err)}});
        res.json({message:'You have to be logged in to save your results.</br><a href="./">Register here!</a>'});
      }
	};
}

exports.history = function(User) {
  return function(req, res) {
      var username = req.session.username;
      if (username != null) {
        models.BreathingSession.find({username: username.toString()}).sort([['date','ascending']]).exec(function(error, result) {
          if (error) res.redirect('/');
          res.json(result);<commit_msg>Ensures data gets saved EVERY time, regardless of whether user is logged in.<commit_after>var models = require('../models');

exports.list = function(User){
  return function(req, res) {

    User.find().lean().exec(function (err, users) {
      return res.render('userlist', {
        "userlist" : users
      });
    });

  };
}

exports.save = function(User) {
	return function(req, res) {
    	var form_data = req.body;

      var newBreathingsession = new models.BreathingSession({
              "date": new Date(),
              "data": form_data.data,
              "username": "TESTING",
              "guidance": form_data.guidance
            });
      newBreathingsession.save(function(err) {if (err) {console.log(err)}});

	    var username = req.session.username;
	    if (username) {
	    	User.find({ name: username }, function(error, result) {
		      if (error) res.redirect('/');

		      if (result.length == 0) {
		      } else {
		        var usr = result[0];

            var newBreathingsession = new models.BreathingSession({
              "date": new Date(),
              "data": form_data.data,
              "_user": usr.id,
              "username": usr.name
            });
            newBreathingsession.save(function(err) {if (err) {console.log(err)}});

            res.send(200);
		      }
		    });
	    } else {
        res.json({message:'You have to be logged in to save your results.</br><a href="./">Register here!</a>'});
      }
	};
}

exports.history = function(User) {
  return function(req, res) {
      var username = req.session.username;
      if (username != null) {
        models.BreathingSession.find({username: username.toString()}).sort([['date','ascending']]).exec(function(error, result) {
          if (error) res.redirect('/');
          res.json(result);
{'fraction_non_alphanumeric': 0.15750412314458492, 'fraction_numerical': 0.002748763056624519, 'mean_word_length': 2.1262886597938144, 'pattern_counts': {'":': 17, '<': 9, '<?xml version=': 0, '>': 9, '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': '7289737', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1060, 'n_words': 231}