<commit_before>const express = require('express');
const validator = require('is-my-json-valid');

const generateErrors = require('../generate-errors');
const dbConnect = require('../db-connect');

const router = express.Router();

const TABLE_NAME = 'test_table';

// Retrieve a list of every `test` resource
router.get('/', (req, res) => {
  const query = {
    name: 'tests_get_all',
    text: `SELECT * FROM ${TABLE_NAME}`
  };

  dbConnect(res, (client, done) => {
    client.query(query, (err, result) => {
      done();
      if (err) {
        console.error(err);
        res.status(500).send({
          errors: [generateErrors.genericError()]
        });
      } else {
        res.send({
          data: result.rows
        });
      }
    });
  });
});

// Create a new `test` resource
router.post('/', (req, res) => {
  const id = req.body.id;
  const name = req.body.name;

  var validate = validator({
    type: 'object',
    properties: {
      id: {
        required: true,
        type: 'number'
      },
      name: {
        required: true,
        type: 'string'
      }
    }
  }, {
    greedy: true
  });

  if (!validate(req.body)) {
    console.log(validate.errors);
  }

  // Ensure that the user has submitted the required fields
  var errors = [];
  if (typeof id === 'undefined') {
    errors.push(generateErrors.missingAttribute('id'));
  }
  if (typeof name === 'undefined') {
    errors.push(generateErrors.missingAttribute('name'));
  }

  if (errors.length) {
    res.status(400).send({
      errors
    });
  } else {
    const query = {
      name: 'tests_create_one',
      text: `INSERT INTO ${TABLE_NAME} VALUES ($1, $2)`,
      values: [id, name]
    };

    dbConnect(res, (client, done) => {
      client.query(query, (err, result) => {
        done();
        if (err) {
          console.error(err);
          res.status(500).send({
            errors: [generateErrors.genericError()]
          });
        } else {
          res.end();
        }
      });
    });
  }
});

// Return a single `test` resource
router.get('/:id', (req, res) => {
  const query = {
    name: 'tests_get_one',
    text: `SELECT * FROM ${TABLE_NAME} WHERE id = $1`,
    values: [req.params.id]
  };

  dbConnect(res, (client, done) => {
    client.query(query, (err, result) => {
      done();
      if (err) {
        console.error(err);
        res.status(500).send({
          errors: [generateErrors.genericError()]
        });
      } else {
        if (!result.rows.length) {
          res.status(404).send({
            errors: [generateErrors.notFoundError()]
          });
        } else {
          res.send({
            data: result.rows[0]
          });
        }
      }
    });
  });
});

// Update a `test` resource
router.patch('/:id', (req, res) => {
  const id = req.params.id;
  const name = req.body.name;

  if (typeof name === 'undefined') {
    res.status(400).send({
      errors: [generateErrors.missingAttribute('name')]
    });
  } else {
    const query = {
      name: 'tests_update_one',
      text: `UPDATE ${TABLE_NAME} SET name = $1 WHERE id = $2`,
      values: [name, id]
    };

    dbConnect(res, (client, done) => {
      client.query(query, (err, result) => {
        done();
        if (err) {
          console.error(err);
          res.status(500).send({
            errors: [generateErrors.genericError()]<commit_msg>Refactor API endpoints to use request-error-map<commit_after>const express = require('express');
const validator = require('is-my-json-valid');

const generateErrors = require('../generate-errors');
const requestErrorMap = require('../request-error-map');
const dbConnect = require('../db-connect');

const router = express.Router();

const TABLE_NAME = 'test_table';

// Retrieve a list of every `test` resource
router.get('/', (req, res) => {
  const query = {
    name: 'tests_get_all',
    text: `SELECT * FROM ${TABLE_NAME}`
  };

  dbConnect(res, (client, done) => {
    client.query(query, (err, result) => {
      done();
      if (err) {
        console.error(err);
        res.status(500).send({
          errors: [generateErrors.genericError()]
        });
      } else {
        res.send({
          data: result.rows
        });
      }
    });
  });
});

// Create a new `test` resource
router.post('/', (req, res) => {
  const id = req.body.id;
  const name = req.body.name;

  var validate = validator({
    type: 'object',
    properties: {
      id: {
        required: true,
        type: 'number'
      },
      name: {
        required: true,
        type: 'string'
      }
    }
  }, {
    greedy: true
  });

  if (!validate(req.body)) {
    res.status(400).send({
      errors: requestErrorMap(validate.errors)
    });
  } else {
    const query = {
      name: 'tests_create_one',
      text: `INSERT INTO ${TABLE_NAME} VALUES ($1, $2)`,
      values: [id, name]
    };

    dbConnect(res, (client, done) => {
      client.query(query, (err, result) => {
        done();
        if (err) {
          console.error(err);
          res.status(500).send({
            errors: [generateErrors.genericError()]
          });
        } else {
          res.end();
        }
      });
    });
  }
});

// Return a single `test` resource
router.get('/:id', (req, res) => {
  const query = {
    name: 'tests_get_one',
    text: `SELECT * FROM ${TABLE_NAME} WHERE id = $1`,
    values: [req.params.id]
  };

  dbConnect(res, (client, done) => {
    client.query(query, (err, result) => {
      done();
      if (err) {
        console.error(err);
        res.status(500).send({
          errors: [generateErrors.genericError()]
        });
      } else {
        if (!result.rows.length) {
          res.status(404).send({
            errors: [generateErrors.notFoundError()]
          });
        } else {
          res.send({
            data: result.rows[0]
          });
        }
      }
    });
  });
});

// Update a `test` resource
router.patch('/:id', (req, res) => {
  const id = req.params.id;
  const name = req.body.name;

  var validate = validator({
    type: 'object',
    properties: {
      name: {
        required: true,
        type: 'string'
      }
    }
  }, {
    greedy: true
  });

  if (!validate(req.body)) {
    res.status(400).send({
      errors: requestErrorMap(validate.errors)
    });
  } else {
    const query = {
      name: 'tests_update_one',
      text: `UPDATE ${TABLE_NAME} SET name = $1 WHERE id = $2`,
      values: [name, id]
    };

    dbConnect(res, (client, done) => {
      client.query(query, (err, result) => {
        done();
        if (err) {
          console.error(err);
          res.status(500).send({
            errors: [generateErrors.genericError()]
{'fraction_non_alphanumeric': 0.17448964386827595, 'fraction_numerical': 0.008046490835940992, 'mean_word_length': 2.027514659449707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '2068468', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2147, 'n_words': 469}