<commit_before>'use strict';

const Boom = require('boom');
const Logger = require('@modulus/logger')('server/handlers/project');

const Project = require('../models/project');


exports.create = function (request, reply) {
  Project.find({ name: request.payload.name }, (err, foundProject) => {
    if (err) {
      Logger.error(`projects.find ${err.message}`);
      return reply(Boom.badRequest(err));
    }

    if (foundProject.length > 0) {
      Logger.error(
        `projects.find Project ${request.payload.name} already exists`
      );
      return reply(Boom.badRequest('A project by that name already exists.'));
    }

    const toCreate = {
      name: request.payload.name,
      maintainers: request.payload.maintainers || [],
      description: request.payload.description || '',
      hasLinter: request.payload.hasLinter || false,
      hasReadme: request.payload.hasReadme || false
    };

    new Project(toCreate).save((err, newProject) => {
      if (err) {
        Logger.error(`projects.create ${err.message}`);
        return reply(Boom.badRequest(err));
      }

      if (foundProject.length > 0) {
        Logger.error(
          `projects.find Project ${request.payload.name} already exists`
        );
        return reply(Boom.badRequest('A project by that name already exists.'));
      }

      const payload = request.payload;
      const toCreate = {
        name: payload.name,
        maintainers: payload.maintainers || [],
        description: payload.description || '',
        hasLicense: payload.hasLicense || false,
        hasLinter: payload.hasLinter || false,
        hasReadme: payload.hasReadme || false,
        engines: {
          npm: payload.engines ? payload.engines.npm : '0.0.0',
          node: payload.engines ? payload.engines.node : '0.0.0'
        }
      };

      new Project(toCreate).save((err, newProject) => {
        if (err) {
          Logger.error(`projects.create ${err.message}`);
          return reply(Boom.badRequest(err));
        }

        reply(newProject);
      });
    });
  });
};

exports.delete = function (request, reply) {
  Project.remove({ name: request.params.name }, (err, foundProject) => {
    if (err) {
      Logger.error(`projects.destroy ${err.message}`);
      return reply(Boom.badRequest(err));
    }

    if (foundProject.length === 0) {
      Logger.error('projects.destroy Project not found');
      return reply(Boom.notFound('Project not found.'));
    }

    reply();
  });
};

exports.getOne = function (request, reply) {
  Project.findOne({
    name: request.params.name
  })
  .populate('versions')
  .exec((err, foundProject) => {
    if (err) {
      Logger.error(`projects.findOne ${err.message}`);
      return reply(Boom.badRequest(err));
    }

    if (!foundProject) {
      Logger.error(
        `projects.findOne Project ${request.payload.name} not found`
      );
      return reply(Boom.badRequest('Project not found.'));
    }

    reply(foundProject);
  });
};

exports.getAll = function (request, reply) {
  Project.find({}).populate('versions').exec((err, foundProject) => {
    if (err) {
      Logger.error(`projects.find ${err.message}`);
      return reply(Boom.badRequest(err));
    }

    reply(foundProject);
  });
};

exports.update = function (request, reply) {
  Project.update({
    name: request.params.name
  }, request.payload, (err, updatedProject) => {<commit_msg>Add limit, offset, and sorting<commit_after>'use strict';

const Boom = require('boom');
const Logger = require('@modulus/logger')('server/handlers/project');

const Project = require('../models/project');


exports.create = function (request, reply) {
  Project.find({ name: request.payload.name }, (err, foundProject) => {
    if (err) {
      Logger.error(`projects.find ${err.message}`);
      return reply(Boom.badRequest(err));
    }

    if (foundProject.length > 0) {
      Logger.error(
        `projects.find Project ${request.payload.name} already exists`
      );
      return reply(Boom.badRequest('A project by that name already exists.'));
    }

    const toCreate = {
      name: request.payload.name,
      maintainers: request.payload.maintainers || [],
      description: request.payload.description || '',
      hasLinter: request.payload.hasLinter || false,
      hasReadme: request.payload.hasReadme || false
    };

    new Project(toCreate).save((err, newProject) => {
      if (err) {
        Logger.error(`projects.create ${err.message}`);
        return reply(Boom.badRequest(err));
      }

      if (foundProject.length > 0) {
        Logger.error(
          `projects.find Project ${request.payload.name} already exists`
        );
        return reply(Boom.badRequest('A project by that name already exists.'));
      }

      const payload = request.payload;
      const toCreate = {
        name: payload.name,
        maintainers: payload.maintainers || [],
        description: payload.description || '',
        hasLicense: payload.hasLicense || false,
        hasLinter: payload.hasLinter || false,
        hasReadme: payload.hasReadme || false,
        engines: {
          npm: payload.engines ? payload.engines.npm : '0.0.0',
          node: payload.engines ? payload.engines.node : '0.0.0'
        }
      };

      new Project(toCreate).save((err, newProject) => {
        if (err) {
          Logger.error(`projects.create ${err.message}`);
          return reply(Boom.badRequest(err));
        }

        reply(newProject);
      });
    });
  });
};

exports.delete = function (request, reply) {
  Project.remove({ name: request.params.name }, (err, foundProject) => {
    if (err) {
      Logger.error(`projects.destroy ${err.message}`);
      return reply(Boom.badRequest(err));
    }

    if (foundProject.length === 0) {
      Logger.error('projects.destroy Project not found');
      return reply(Boom.notFound('Project not found.'));
    }

    reply();
  });
};

exports.getOne = function (request, reply) {
  Project.findOne({
    name: request.params.name
  })
  .populate('versions')
  .exec((err, foundProject) => {
    if (err) {
      Logger.error(`projects.findOne ${err.message}`);
      return reply(Boom.badRequest(err));
    }

    if (!foundProject) {
      Logger.error(
        `projects.findOne Project ${request.payload.name} not found`
      );
      return reply(Boom.badRequest('Project not found.'));
    }

    reply(foundProject);
  });
};

exports.getAll = function (request, reply) {
  var query = Project.find({});

  query.sort({ name: request.query.sort || 'asc' });
  query.limit(parseInt(request.query.limit, 10) || 10);
  query.skip(parseInt(request.query.offset, 10) || 0);

  query.populate('versions').exec((err, foundProject) => {
    if (err) {
      Logger.error(`projects.find ${err.message}`);
      return reply(Boom.badRequest(err));
    }

    reply(foundProject);
  });
};

exports.update = function (request, reply) {
  Project.update({
    name: request.params.name
  }, request.payload, (err, updatedProject) => {
{'fraction_non_alphanumeric': 0.15802222855514392, 'fraction_numerical': 0.003562268452550584, 'mean_word_length': 2.757494646680942, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '1974507', 'n_tokens_mistral': 2130, 'n_tokens_neox': 2095, 'n_words': 419}