<commit_before>var controller = exports;

var cliff  = require('cliff'),
    prompt = require('prompt');

controller.get = function (id, data, resource) {
  if (id) {
    resource.get(id, function(err, result){
      if(err) {
        logger.error(JSON.stringify(err))
        return;
      }
      logger.info('showing ' + id.magenta);
      cliff.putObject(result);
    });
  } else {
    logger.error('id is required to get');
  }
}

controller.all = function (id, options, resource) {
  logger.info('listing ' + resource.name.magenta)
  resource.all(function(err, rows){
    if(err) {
      logger.error(JSON.stringify(err))
      return;
    }
    if (rows.length === 0) {
      logger.info('no ' + resource.name.magenta + ' found')
    } else {
      logger.info('showing all ' + resource.name.magenta);
      var lines = cliff.stringifyObjectRows(rows, Object.keys(resource.schema.properties), ['underline'], { columnSpacing: 5 }).split('\n');
      lines.forEach(function(line){
        logger.info(line);
      })
    }
  })
};

controller.create = function (id, data, resource) {
  
  logger.warn('prompting user for data');
  if(typeof prompt.override.id !== 'undefined') {
    logger.info('define ' + prompt.override.id.magenta);
  } else {
    logger.info('define new ' + resource.name.magenta);
  }
  prompt.get(resource.schema, function (err, result) {
    if(err) {
      return;
    }
    logger.info('about to create ' + resource.name.magenta);
    cliff.putObject(result);
    resource.create(result, function(err, r){
      if(err) {
        logger.error(JSON.stringify(err))
        return;
      }
      logger.info('created new ' + resource.name.magenta + ' ' + r.id.grey)
    })
  });
};

controller.edit = function (id, resource) {
  resource.get(id, function(err, record) {
    if(err) {
      logger.error(JSON.stringify(err))
      return;
    }
    var schema = utile.clone(resource.schema);
    for(var p in record.properties) {
      if(typeof schema.properties[p] !== 'undefined') {
        schema.properties[p].default = record.properties[p];
      }
    }
    logger.info('editing ' + resource.name.magenta + ' ' + record.id);
    logger.warn('prompting user for data');
    prompt.get(schema, function (err, result) {
      if(err) {
        return;
      }
      logger.info('about to save ' + resource.name.magenta);
      cliff.putObject(result);
      result.id = record.id;
      confirm(function(err, promptResult){
        resource.update(record.id, result, function(err, r){
          if(err) {
            return logger.error(JSON.stringify(err))
          }
          logger.info('saved ' + resource.name.magenta + ' ' + r.id.grey)
        })
      });
    });
  });
};

controller.destroy = function (id, resource) {
  resource.get(id, function(err, record){
    if(err) {
      logger.error(JSON.stringify(err))
      return;
    }
    var schema = utile.clone(resource.schema);
    logger.info('about to destroy ' + resource.name.magenta + ' ' + record.id);
    cliff.putObject(record);
    confirm(function(err, promptResult){
      resource.destroy(record.id, function(err, r){
        if(err) {
          logger.error(JSON.stringify(err))
          return;
        }
        logger.info('destroyed' + ' ' + r.id.magenta)
      });
    });
  });
};

controller.resourceMethod = function (id, method, resource) {

  var _args = [];

  logger.info('executing resource method ' + method.magenta);

  //
  // If there is a supplied schema for this resource method,
  // then prompt the user to fill out the schema
  //
  if(typeof resource[method].schema === 'object') {
    var _schema = resource[method].schema;

    //
    // Check to see if the convention of an options / callback is used,
    // if not, just use the schema as is
    //
    _schema = _schema.properties.options || _schema;

    prompt.get(_schema, function(err, result){
      if(err) {
        logger.error(JSON.stringify(err));
        return;
      }
      logger.info('executed ' + method.magenta);
      cliff.putObject(result);
    });

  } else {

    logger.info('attempting to ' + method.magenta + ' ' + id.magenta)
    resource[method](id, {}, function(err, result){
      if(err) {
        logger.error(JSON.stringify(err));
        return;
      }
      logger.info('executed ' + method.magenta)
      logger.data(result);
    });

  }

};

//
// TODO: prompt actually has a built in prompt.confirm,
// its just not documented. Switch to using this and add usage docs to prompt library
//
function confirm (callback) {
  var property = {<commit_msg>[fix] Better checking for schema on resource methods <commit_after>var controller = exports;

var cliff  = require('cliff'),
    prompt = require('prompt');

controller.get = function (id, data, resource) {
  if (id) {
    resource.get(id, function(err, result){
      if(err) {
        logger.error(JSON.stringify(err))
        return;
      }
      logger.info('showing ' + id.magenta);
      cliff.putObject(result);
    });
  } else {
    logger.error('id is required to get');
  }
}

controller.all = function (id, options, resource) {
  logger.info('listing ' + resource.name.magenta)
  resource.all(function(err, rows){
    if(err) {
      logger.error(JSON.stringify(err))
      return;
    }
    if (rows.length === 0) {
      logger.info('no ' + resource.name.magenta + ' found')
    } else {
      logger.info('showing all ' + resource.name.magenta);
      var lines = cliff.stringifyObjectRows(rows, Object.keys(resource.schema.properties), ['underline'], { columnSpacing: 5 }).split('\n');
      lines.forEach(function(line){
        logger.info(line);
      })
    }
  })
};

controller.create = function (id, data, resource) {
  
  logger.warn('prompting user for data');
  if(typeof prompt.override.id !== 'undefined') {
    logger.info('define ' + prompt.override.id.magenta);
  } else {
    logger.info('define new ' + resource.name.magenta);
  }
  prompt.get(resource.schema, function (err, result) {
    if(err) {
      return;
    }
    logger.info('about to create ' + resource.name.magenta);
    cliff.putObject(result);
    resource.create(result, function(err, r){
      if(err) {
        logger.error(JSON.stringify(err))
        return;
      }
      logger.info('created new ' + resource.name.magenta + ' ' + r.id.grey)
    })
  });
};

controller.edit = function (id, resource) {
  resource.get(id, function(err, record) {
    if(err) {
      logger.error(JSON.stringify(err))
      return;
    }
    var schema = utile.clone(resource.schema);
    for(var p in record.properties) {
      if(typeof schema.properties[p] !== 'undefined') {
        schema.properties[p].default = record.properties[p];
      }
    }
    logger.info('editing ' + resource.name.magenta + ' ' + record.id);
    logger.warn('prompting user for data');
    prompt.get(schema, function (err, result) {
      if(err) {
        return;
      }
      logger.info('about to save ' + resource.name.magenta);
      cliff.putObject(result);
      result.id = record.id;
      confirm(function(err, promptResult){
        resource.update(record.id, result, function(err, r){
          if(err) {
            return logger.error(JSON.stringify(err))
          }
          logger.info('saved ' + resource.name.magenta + ' ' + r.id.grey)
        })
      });
    });
  });
};

controller.destroy = function (id, resource) {
  resource.get(id, function(err, record){
    if(err) {
      logger.error(JSON.stringify(err))
      return;
    }
    var schema = utile.clone(resource.schema);
    logger.info('about to destroy ' + resource.name.magenta + ' ' + record.id);
    cliff.putObject(record);
    confirm(function(err, promptResult){
      resource.destroy(record.id, function(err, r){
        if(err) {
          logger.error(JSON.stringify(err))
          return;
        }
        logger.info('destroyed' + ' ' + r.id.magenta)
      });
    });
  });
};

controller.resourceMethod = function (id, method, resource) {

  var _args = [];

  logger.info('executing resource method ' + method.magenta);

  //
  // If there is a supplied schema for this resource method,
  // then prompt the user to fill out the schema
  //
  if(typeof resource[method].schema === 'object' && typeof resource[method].properties === 'object') {
    var _schema = resource[method].schema;
    //
    // Check to see if the convention of an options / callback is used,
    // if not, just use the schema as is
    //
    _schema = _schema.properties.options || _schema;

    prompt.get(_schema, function(err, result){
      if(err) {
        logger.error(JSON.stringify(err));
        return;
      }
      logger.info('executed ' + method.magenta);
      cliff.putObject(result);
    });

  } else {

    logger.info('attempting to ' + method.magenta)
    resource[method](function(err, result){
      if(err) {
        logger.error(JSON.stringify(err));
        return;
      }
      logger.info('executed ' + method.magenta)
      logger.data(result);
    });

  }

};

//
// TODO: prompt actually has a built in prompt.confirm,
// its just not documented. Switch to using this and add usage docs to prompt library
//
function confirm (callback) {
  var property = {
{'fraction_non_alphanumeric': 0.15193579872031232, 'fraction_numerical': 0.0004337924303220909, 'mean_word_length': 2.5855365474339034, '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': '2135247', 'n_tokens_mistral': 2887, 'n_tokens_neox': 2883, 'n_words': 597}