<commit_before>const safeExtend = require('../lib/safe-extend')
const Systems = require('../models/system');

const imageHelper = require('../lib/image-helper')
const errorHelper = require('../lib/error-helper')
const middleware = require('../lib/middleware')

const putSystem = imageHelper.putModel(Systems)
const dbErrorHandler = errorHelper.makeDbHandler('system')

exports = module.exports = function applySystemRoutes (server) {
  server.get('/systems', showAllSystems);
  function showAllSystems(req, res, next) {
    const query = {}
    const options = {relationships: true};
    Systems.get(query, options, function foundRows(error, rows) {
      if (error)
        return dbErrorHandler(error, null, res, next)

      return res.send({systems: rows.map(Systems.toResponse)});
    });
  }

  server.post('/systems', saveSystem);
  function saveSystem(req, res, next) {
    const row = fromPostToRow(req.body);
    const image = imageHelper.getFromPost(req)

    putSystem(row, image, function savedRow(err, system) {
      if (err) {
        if (!Array.isArray(err))
          return dbErrorHandler(err, row, res, next);
        return res.send(400, errorHelper.validation(err));
      }

      res.send(201, {
        status: 'created',
        system: Systems.toResponse(system)
      });
    });
  }

  server.get('/systems/:systemSlug', [
    middleware.findSystem({relationships: true}),
    showOneSystem,
  ]);
  function showOneSystem(req, res, next) {
    res.send({system: req.system.toResponse()})
    return res.next()
  }

  server.del('/systems/:systemSlug', [
    middleware.findSystem(),
    deleteSystem,
  ]);
  function deleteSystem(req, res, next) {
    const system = req.system
    const query = {id: system.id, slug: system.slug}
    Systems.del(query, function deletedRow(error, result) {
      if (error)
        return dbErrorHandler(error, system, req, next)<commit_msg>Fix `toResponse` call, use method on object.<commit_after>const safeExtend = require('../lib/safe-extend')
const Systems = require('../models/system');

const imageHelper = require('../lib/image-helper')
const errorHelper = require('../lib/error-helper')
const middleware = require('../lib/middleware')

const putSystem = imageHelper.putModel(Systems)
const dbErrorHandler = errorHelper.makeDbHandler('system')

exports = module.exports = function applySystemRoutes (server) {
  server.get('/systems', showAllSystems);
  function showAllSystems(req, res, next) {
    const query = {}
    const options = {relationships: true};
    Systems.get(query, options, function foundRows(error, rows) {
      if (error)
        return dbErrorHandler(error, null, res, next)

      return res.send({systems: rows.map(Systems.toResponse)});
    });
  }

  server.post('/systems', saveSystem);
  function saveSystem(req, res, next) {
    const row = fromPostToRow(req.body);
    const image = imageHelper.getFromPost(req)

    putSystem(row, image, function savedRow(err, system) {
      if (err) {
        if (!Array.isArray(err))
          return dbErrorHandler(err, row, res, next);
        return res.send(400, errorHelper.validation(err));
      }

      res.send(201, {
        status: 'created',
        system: system.toResponse(),
      });
    });
  }

  server.get('/systems/:systemSlug', [
    middleware.findSystem({relationships: true}),
    showOneSystem,
  ]);
  function showOneSystem(req, res, next) {
    res.send({system: req.system.toResponse()})
    return res.next()
  }

  server.del('/systems/:systemSlug', [
    middleware.findSystem(),
    deleteSystem,
  ]);
  function deleteSystem(req, res, next) {
    const system = req.system
    const query = {id: system.id, slug: system.slug}
    Systems.del(query, function deletedRow(error, result) {
      if (error)
        return dbErrorHandler(error, system, req, next)
{'fraction_non_alphanumeric': 0.14516549387542352, 'fraction_numerical': 0.003127443315089914, 'mean_word_length': 3.504694835680751, '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': '5152668', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1161, 'n_words': 257}