<commit_before>var halson = require('halson');
var Player = require('../models/player');
var User = require('../models/user');
var mongoose = require('mongoose');
var Achievement = mongoose.model('Achievement');
var shortid = require('shortid');
var logger = require('../log');
var qr = require('qr-image');

module.exports = function(app, router, requireAuth) {
  'use strict';

  // This will handle call to /players/:player_id
  router.route('/:player_id')

  .get(function(req, res) {
    // Return player
    Player.findOne({id: req.params.player_id}, function(err, player) {
      if (err) {
        res.status(500);
        res.setHeader('Content-Type', 'application/vnd.error+json');
        res.json({ message: 'Failed to get player'});
      } else if (player === null) {
        res.status(404);
        res.setHeader('Content-Type', 'application/vnd.error+json');
        res.json({ message: 'Not found'});
      } else {
        res.status(200);
        res.setHeader('Content-Type', 'application/json');
        var resource = halson({
          id: player.id,
          url: player.url,
          name: player.name,
          org: player.org,
          achievements: player.achievements,
          active: player.active,
          disabled: player.disabled,
          created_at: player.created_at,
          updated_at: player.updated_at
        }).addLink(
          'self',
          '/players/' + player.id
        );
        res.send(JSON.stringify(resource));        
      }
    });
  })

  .put(requireAuth, function(req, res) {
    // Replace or create player
    if (!req.user.admin) {
      res.status(401);
      res.setHeader('Content-Type', 'application/vnd.error+json');
      res.json({ message: 'Not authorized'});
      return;
    }
    Player.update({
      id: req.params.player_id
    }, {
      id: req.body.id || req.params.player_id,
      url: req.body.url,
      name: req.body.name,
      org: req.body.org,
      contact: req.body.contact || {},
      achievements: req.body.achievements || [],
      active: typeof req.body.active !== 'undefined' ? req.body.active : true,
      disabled: typeof req.body.disabled !== 'undefined' ? req.body.disabled : false
    }, {
      upsert: true
    }, function(err, raw) {
      if (err) {
        res.status(500);
        res.setHeader('Content-Type', 'application/vnd.error+json');
        res.json({ message: 'Failed to update player'});
      } else {
        Player.findOne({id: req.params.player_id}, function(err, player) {
          if (err) {
            res.status(500);
            res.setHeader('Content-Type', 'application/vnd.error+json');
            res.json({ message: 'Failed to get player'});
          } else {
            res.status(200);
            res.setHeader('Content-Type', 'application/json');
            var resource = halson({
              id: player.id,
              url: player.url,
              name: player.name,
              org: player.org,
              contact: player.contact,
              achievements: player.achievements,
              active: player.active,
              disabled: player.disabled,
              created_at: player.created_at,
              updated_at: player.updated_at
            }).addLink(
              'self',
              '/players/' + player.id
            );
            res.send(JSON.stringify(resource));
          }
        });       
      }
    });
  })

  .patch(requireAuth, function(req, res) {
    // Patch player
    Player.findOne({
      id: req.params.player_id
    }, function(err, player) {
      if (err) {
        res.status(500);
        res.setHeader('Content-Type', 'application/vnd.error+json');
        res.json({ message: 'Unable to find player'});
      } else {
        if (req.body.url) {
          logger.debug('Player %s url changed to %s',player._id,req.body.name);
          player.url = req.body.url;
        }
        if (req.body.name) {
          logger.debug('Player %s name changed to %s',player._id,req.body.name);
          player.name = req.body.name;
        }
        if (req.body.org) {
          logger.debug('Player %s org changed to %s',player._id,req.body.org);
          player.org = req.body.org;
        }
        if (req.body.phone) {
          player.phone = req.body.phone;
        }
        if (req.body.email) {
          player.phone = req.body.email;
        }
        if (req.body.achievements) {
          player.achievements = req.body.achievements;
        }
        if (typeof req.body.active !== 'undefined') {
          player.active = req.body.active;
        } else {
          player.active = true;
        }
        if (typeof req.body.disabled !== 'undefined') {
          player.disabled = req.body.disabled;
        }
        Player.update({_id:player._id},player,function(err, raw) {
          if (err) {
            res.status(500);
            res.setHeader('Content-Type', 'application/vnd.error+json');
            res.json({ message: 'Failed to update player'});
          } else {
            logger.debug('Response from mongo: %s', JSON.stringify(raw));
            Player.findOne({id: req.params.player_id}, function(err, player) {
              if (err) {
                res.status(500);
                res.setHeader('Content-Type', 'application/vnd.error+json');
                res.json({ message: 'Failed to update player'});
              } else {
                res.status(200);
                res.setHeader('Content-Type', 'application/json');
                var resource = halson({
                  id: player.id,
                  url: player.url,<commit_msg>fixed bug in player patch<commit_after>var halson = require('halson');
var Player = require('../models/player');
var User = require('../models/user');
var mongoose = require('mongoose');
var Achievement = mongoose.model('Achievement');
var shortid = require('shortid');
var logger = require('../log');
var qr = require('qr-image');

module.exports = function(app, router, requireAuth) {
  'use strict';

  // This will handle call to /players/:player_id
  router.route('/:player_id')

  .get(function(req, res) {
    // Return player
    Player.findOne({id: req.params.player_id}, function(err, player) {
      if (err) {
        res.status(500);
        res.setHeader('Content-Type', 'application/vnd.error+json');
        res.json({ message: 'Failed to get player'});
      } else if (player === null) {
        res.status(404);
        res.setHeader('Content-Type', 'application/vnd.error+json');
        res.json({ message: 'Not found'});
      } else {
        res.status(200);
        res.setHeader('Content-Type', 'application/json');
        var resource = halson({
          id: player.id,
          url: player.url,
          name: player.name,
          org: player.org,
          achievements: player.achievements,
          active: player.active,
          disabled: player.disabled,
          created_at: player.created_at,
          updated_at: player.updated_at
        }).addLink(
          'self',
          '/players/' + player.id
        );
        res.send(JSON.stringify(resource));        
      }
    });
  })

  .put(requireAuth, function(req, res) {
    // Replace or create player
    if (!req.user.admin) {
      res.status(401);
      res.setHeader('Content-Type', 'application/vnd.error+json');
      res.json({ message: 'Not authorized'});
      return;
    }
    Player.update({
      id: req.params.player_id
    }, {
      id: req.body.id || req.params.player_id,
      url: req.body.url,
      name: req.body.name,
      org: req.body.org,
      contact: req.body.contact || {},
      achievements: req.body.achievements || [],
      active: typeof req.body.active !== 'undefined' ? req.body.active : true,
      disabled: typeof req.body.disabled !== 'undefined' ? req.body.disabled : false
    }, {
      upsert: true
    }, function(err, raw) {
      if (err) {
        res.status(500);
        res.setHeader('Content-Type', 'application/vnd.error+json');
        res.json({ message: 'Failed to update player'});
      } else {
        Player.findOne({id: req.params.player_id}, function(err, player) {
          if (err) {
            res.status(500);
            res.setHeader('Content-Type', 'application/vnd.error+json');
            res.json({ message: 'Failed to get player'});
          } else {
            res.status(200);
            res.setHeader('Content-Type', 'application/json');
            var resource = halson({
              id: player.id,
              url: player.url,
              name: player.name,
              org: player.org,
              contact: player.contact,
              achievements: player.achievements,
              active: player.active,
              disabled: player.disabled,
              created_at: player.created_at,
              updated_at: player.updated_at
            }).addLink(
              'self',
              '/players/' + player.id
            );
            res.send(JSON.stringify(resource));
          }
        });       
      }
    });
  })

  .patch(requireAuth, function(req, res) {
    // Patch player
    Player.findOne({
      id: req.params.player_id
    }, function(err, player) {
      if (err) {
        res.status(500);
        res.setHeader('Content-Type', 'application/vnd.error+json');
        res.json({ message: 'Unable to find player'});
      } else {
        if (req.body.url) {
          logger.debug('Player %s url changed to %s',player._id,req.body.name);
          player.url = req.body.url;
        }
        if (req.body.name) {
          logger.debug('Player %s name changed to %s',player._id,req.body.name);
          player.name = req.body.name;
        }
        if (req.body.org) {
          logger.debug('Player %s org changed to %s',player._id,req.body.org);
          player.org = req.body.org;
        }
        if (req.body.phone) {
          player.phone = req.body.phone;
        }
        if (req.body.email) {
          player.email = req.body.email;
        }
        if (req.body.achievements) {
          player.achievements = req.body.achievements;
        }
        if (typeof req.body.active !== 'undefined') {
          player.active = req.body.active;
        } else {
          player.active = true;
        }
        if (typeof req.body.disabled !== 'undefined') {
          player.disabled = req.body.disabled;
        }
        Player.update({_id:player._id},player,function(err, raw) {
          if (err) {
            res.status(500);
            res.setHeader('Content-Type', 'application/vnd.error+json');
            res.json({ message: 'Failed to update player'});
          } else {
            logger.debug('Response from mongo: %s', JSON.stringify(raw));
            Player.findOne({id: req.params.player_id}, function(err, player) {
              if (err) {
                res.status(500);
                res.setHeader('Content-Type', 'application/vnd.error+json');
                res.json({ message: 'Failed to update player'});
              } else {
                res.status(200);
                res.setHeader('Content-Type', 'application/json');
                var resource = halson({
                  id: player.id,
                  url: player.url,
{'fraction_non_alphanumeric': 0.14831940245420594, 'fraction_numerical': 0.00586875333451894, 'mean_word_length': 1.8523966522952067, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2185738', 'n_tokens_mistral': 3369, 'n_tokens_neox': 3296, 'n_words': 661}