<commit_before>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
  Children = mongoose.model('Children'),
  path = require('path'),
  multer = require('multer'),
  config = require(path.resolve('./config/config')),
  errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')),
  _ = require('lodash');

/**
 * Create a child function
 */
exports.create = function (req, res) {
  var child = new Children(req.body);
  child.save(function(err) {
    if(err) {
      console.log(err);
      res.status(400).send(err);
    } else {
      res.json(child);
    }
  });
};

/**
 * Show the current child
 */
exports.read = function (req, res) {
  res.json(req.child);

};

/**
 * Update a child
 */
exports.update = function (req, res) {
  var child = req.child;
  /* Replace child properties */
  child.firstName = req.body.firstName;
  child.lastName = req.body.lastName;
  child.biography = req.body.biography;
  child.gender = req.body.gender;
  child.eligibleForSponsorship = req.body.eligibleForSponsorship;
  child.dob = req.body.dob;

  /* Save */
  child.save(function(err) {
    if(err) {
      console.log(err);
      res.status(400).send(err);
    }
    else {
      res.json(child);
    }
  });
};

exports.updateFunding = function (req, res) {
  var child = req.child;
  var sponsorshipType = req.body.sponsorshipType;
  var currentSponsorship = child.sponsorshipType;
  if (child.eligibleForSponsorship) {
    if(sponsorshipType === 'full'){
      currentSponsorship = currentSponsorship + 2;
    }
    if(sponsorshipType === 'half'){
      currentSponsorship = currentSponsorship + 1;
    }
    child.sponsorshipType = currentSponsorship;
    if (child.sponsorshipType <= 2) {
      if (child.sponsorshipType === 2) {
        child.eligibleForSponsorship = false;
      }
      child.save(function(err) {
        if(err) {
          console.log(err);
          res.status(400).send(err);
        }
        else {
          res.json(child);
        }
      });
    } else {
      res.status(400).send({
        message: 'Cannot fund this as child would be oversponsored'
      });
    }
  } else {
    res.status(400).send({
      message: 'child not eligible for sponsorships'
    });
  }
};

/**
 * Delete an child
 */
exports.delete = function (req, res) {
  var child = req.child;

  /* Remove Child */
  child.remove(function(err) {
    if(err){
      res.status(400).send(err);
    }
    else {
      res.end();
    }
  });
};

/**
 * List of children
 */
exports.list = function (req, res) {
  Children.find().exec(function(err, Children) {
    if(err) {
      return res.status(400).send(err);
    } else {
      res.json(Children);
    }
  });
};

/**
 * List of children for carousel (presently all for testing)
 */
exports.carouselList = function (req, res) {
  Children.find().limit(5).exec(function(err, Children) {
    if(err) {
      return res.status(400).send(err);
    } else {
      res.json(Children);
    }
  });
};

exports.listEligible = function (req, res) {
  Children.find({ eligibleForSponsorship: true }).exec(function(err, Children) {
    if(err) {
      return res.status(400).send(err);
    } else {
      res.json(Children);
    }
  });
};

exports.changePrimaryPhoto = function (req, res) {
  var child = req.child;
  var message = null;
  config.uploads.profileUpload.dest = 'modules/children/client/img/uploads/';
  var upload = multer(config.uploads.primaryUpload).single('newPrimaryPicture');
  var profileUploadFileFilter = require(path.resolve('./config/lib/multer')).profileUploadFileFilter;

  // Filtering to upload only images
  upload.fileFilter = profileUploadFileFilter;
  if (child) {
    upload(req, res, function (uploadError) {
      if(uploadError) {
        return res.status(400).send({
          message: 'Error occurred while uploading primary photo'
        });
      } else {
        if (child.primaryPhoto){
          child.additionalPhotos.push(child.primaryPhoto);
        }
        child.primaryPhoto =req.file.filename+','+req.body.dateTaken;
        child.save(function (saveError) {
          if (saveError) {
            return res.status(400).send({
              message: errorHandler.getErrorMessage(saveError)
            });
          } else {
            res.json(child);
          }
        });
      }
    });
  } else {
    res.status(400).send({
      message: 'child does not exist'
    });
  }
};

exports.removeAdditionalPhoto = function (req, res) {
  var child = req.child;
  var message = null;
  var index = req.body.photoindex;
  var image = req.body.photoimage;
  if (child) {
    child.additionalPhotos.splice(index, 1);
    child.save(function (saveError) {
      if (saveError) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(saveError)
        });
      } else {
        res.json(child);
      }
    });
  } else {
    res.status(400).send({
      message: 'child does not exist'
    });<commit_msg>unlinks files now<commit_after>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
  Children = mongoose.model('Children'),
  path = require('path'),
  multer = require('multer'),
  config = require(path.resolve('./config/config')),
  fs = require('fs'),
  errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')),
  _ = require('lodash');

/**
 * Create a child function
 */
exports.create = function (req, res) {
  var child = new Children(req.body);
  child.save(function(err) {
    if(err) {
      console.log(err);
      res.status(400).send(err);
    } else {
      res.json(child);
    }
  });
};

/**
 * Show the current child
 */
exports.read = function (req, res) {
  res.json(req.child);

};

/**
 * Update a child
 */
exports.update = function (req, res) {
  var child = req.child;
  /* Replace child properties */
  child.firstName = req.body.firstName;
  child.lastName = req.body.lastName;
  child.biography = req.body.biography;
  child.gender = req.body.gender;
  child.eligibleForSponsorship = req.body.eligibleForSponsorship;
  child.dob = req.body.dob;

  /* Save */
  child.save(function(err) {
    if(err) {
      console.log(err);
      res.status(400).send(err);
    }
    else {
      res.json(child);
    }
  });
};

exports.updateFunding = function (req, res) {
  var child = req.child;
  var sponsorshipType = req.body.sponsorshipType;
  var currentSponsorship = child.sponsorshipType;
  if (child.eligibleForSponsorship) {
    if(sponsorshipType === 'full'){
      currentSponsorship = currentSponsorship + 2;
    }
    if(sponsorshipType === 'half'){
      currentSponsorship = currentSponsorship + 1;
    }
    child.sponsorshipType = currentSponsorship;
    if (child.sponsorshipType <= 2) {
      if (child.sponsorshipType === 2) {
        child.eligibleForSponsorship = false;
      }
      child.save(function(err) {
        if(err) {
          console.log(err);
          res.status(400).send(err);
        }
        else {
          res.json(child);
        }
      });
    } else {
      res.status(400).send({
        message: 'Cannot fund this as child would be oversponsored'
      });
    }
  } else {
    res.status(400).send({
      message: 'child not eligible for sponsorships'
    });
  }
};

/**
 * Delete an child
 */
exports.delete = function (req, res) {
  var child = req.child;

  /* Remove Child */
  child.remove(function(err) {
    if(err){
      res.status(400).send(err);
    }
    else {
      res.end();
    }
  });
};

/**
 * List of children
 */
exports.list = function (req, res) {
  Children.find().exec(function(err, Children) {
    if(err) {
      return res.status(400).send(err);
    } else {
      res.json(Children);
    }
  });
};

/**
 * List of children for carousel (presently all for testing)
 */
exports.carouselList = function (req, res) {
  Children.find().limit(5).exec(function(err, Children) {
    if(err) {
      return res.status(400).send(err);
    } else {
      res.json(Children);
    }
  });
};

exports.listEligible = function (req, res) {
  Children.find({ eligibleForSponsorship: true }).exec(function(err, Children) {
    if(err) {
      return res.status(400).send(err);
    } else {
      res.json(Children);
    }
  });
};

exports.changePrimaryPhoto = function (req, res) {
  var child = req.child;
  var message = null;
  var upload = multer(config.uploads.primaryUpload).single('newPrimaryPicture');
  var profileUploadFileFilter = require(path.resolve('./config/lib/multer')).profileUploadFileFilter;

  // Filtering to upload only images
  upload.fileFilter = profileUploadFileFilter;
  if (child) {
    upload(req, res, function (uploadError) {
      if(uploadError) {
        return res.status(400).send({
          message: 'Error occurred while uploading primary photo'
        });
      } else {
        if (child.primaryPhoto){
          child.additionalPhotos.push(child.primaryPhoto);
        }
        child.primaryPhoto =req.file.filename+','+req.body.dateTaken;
        child.save(function (saveError) {
          if (saveError) {
            return res.status(400).send({
              message: errorHandler.getErrorMessage(saveError)
            });
          } else {
            res.json(child);
          }
        });
      }
    });
  } else {
    res.status(400).send({
      message: 'child does not exist'
    });
  }
};

exports.removeAdditionalPhoto = function (req, res) {
  var child = req.child;
  var message = null;
  var index = req.body.photoindex;
  var image = req.body.photoimage;
  var imagePath = config.uploads.primaryUpload.dest+image;
  if (child) {
    child.additionalPhotos.splice(index, 1);
    if(image){
      fs.stat(imagePath, function (err, stats) {
        if (err) {
          return console.error('Cannot find file so no deletion');
        }
        else {
          fs.unlink(imagePath,function(err){
            if(err) {
              return console.log('Attempted to delete nonexistant file');
            }
            else {
              console.log('file deleted successfully');
            }
          }); 
        } 
      });
    }
    child.save(function (saveError) {
      if (saveError) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(saveError)
        });
      } else {
        res.json(child);
      }
    });
  } else {
    res.status(400).send({
      message: 'child does not exist'
    });
{'fraction_non_alphanumeric': 0.14870278519648988, 'fraction_numerical': 0.009156810377718427, 'mean_word_length': 2.4880239520958085, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2653027', 'n_tokens_mistral': 3428, 'n_tokens_neox': 3303, 'n_words': 640}