<commit_before>const { body, param, query, validationResult } = require('express-validator/check')
const { matchedData, sanitize } = require('express-validator/filter')
//
// API BASE CLASS
//
exports.BaseController = class {
  constructor (Model) {
    // table model
    this.Model = Model.orm
    this.modelId = Model.id
    // check method
    this.check = {body, param, query}

    this.ValidateIdParams = [
      param('id', 'id should be a int').custom(id => {
        if (!Number.isInteger(Number(id))) {
          throw new Error('id format illegal')
        }
        return id
      }),
      sanitize('id').toInt()
    ]

    this.ValidateCreateKeys = []
    // Bind Main Function
    this.getAll = this.getAll.bind(this)
    this.getById = this.getById.bind(this)
    this.create = this.create.bind(this)
    this.update = this.update.bind(this)
    this.delete = this.delete.bind(this)
  }

  _notEmpty (value) {
    if (value && Array.isArray(value)) {
      return !value.includes('')
    } else if (value) {
      return true
    } else {
      return false
    }
  }

  _validateRequest (req, res) {
    return new Promise((resolve, reject) => {
      const errors = validationResult(req)
      // check id param valid
      if (errors.mapped().id) {
        throw new Error('404')
      } else if (!errors.isEmpty()) {
        const badRequest = new Error('400')
        badRequest.payload = errors.mapped()
        throw badRequest
      }
      resolve()
    })
  }

  // use in update & delete request
  // generate different {'item_id': req.params.id}
  _setIdFilter (req) {
    const filterRules = {}
    filterRules[this.modelId] = req.params.id
    return filterRules
  }

  _returnItemOrNotFound (Item, res) {
    if (Item) {
      res.json(Item)
    } else {
      throw new Error('404')
    }
  }

  _sendErrorResponse (err, res) {
    (err.name === 'SequelizeForeignKeyConstraintError') ? res.status(400).json({error: 'This foreign id is not exist'})
    : (err.message === '400') ? res.status(400).json({error: err.payload})
    : (err.message === '404') ? res.status(404).json({error: 'not found'})
                              : res.status(500).json({error: err.message})
  }

  async getAll (req, res) {
    try {
      const Items = await this.Model.findAll({where: req.query, oreder: [['created_at', 'DESC']]})
      if (Items.length) {
        res.json(Items)
      } else {<commit_msg>[Update] foreign key not exist error msg<commit_after>const { body, param, query, validationResult } = require('express-validator/check')
const { matchedData, sanitize } = require('express-validator/filter')
//
// API BASE CLASS
//
exports.BaseController = class {
  constructor (Model) {
    // table model
    this.Model = Model.orm
    this.modelId = Model.id
    // check method
    this.check = {body, param, query}

    this.ValidateIdParams = [
      param('id', 'id should be a int').custom(id => {
        if (!Number.isInteger(Number(id))) {
          throw new Error('id format illegal')
        }
        return id
      }),
      sanitize('id').toInt()
    ]

    this.ValidateCreateKeys = []
    // Bind Main Function
    this.getAll = this.getAll.bind(this)
    this.getById = this.getById.bind(this)
    this.create = this.create.bind(this)
    this.update = this.update.bind(this)
    this.delete = this.delete.bind(this)
  }

  _notEmpty (value) {
    if (value && Array.isArray(value)) {
      return !value.includes('')
    } else if (value) {
      return true
    } else {
      return false
    }
  }

  _validateRequest (req, res) {
    return new Promise((resolve, reject) => {
      const errors = validationResult(req)
      // check id param valid
      if (errors.mapped().id) {
        throw new Error('404')
      } else if (!errors.isEmpty()) {
        const badRequest = new Error('400')
        badRequest.payload = errors.mapped()
        throw badRequest
      }
      resolve()
    })
  }

  // use in update & delete request
  // generate different {'item_id': req.params.id}
  _setIdFilter (req) {
    const filterRules = {}
    filterRules[this.modelId] = req.params.id
    return filterRules
  }

  _returnItemOrNotFound (Item, res) {
    if (Item) {
      res.json(Item)
    } else {
      throw new Error('404')
    }
  }

  _sendErrorResponse (err, res) {
    (err.name === 'SequelizeForeignKeyConstraintError') ? res.status(400).json({error: 'The foreign id is not exist'})
    : (err.message === '400') ? res.status(400).json({error: err.payload})
    : (err.message === '404') ? res.status(404).json({error: 'not found'})
                              : res.status(500).json({error: err.message})
  }

  async getAll (req, res) {
    try {
      const Items = await this.Model.findAll({where: req.query, oreder: [['created_at', 'DESC']]})
      if (Items.length) {
        res.json(Items)
      } else {
{'fraction_non_alphanumeric': 0.14813295034878948, 'fraction_numerical': 0.01107919573245794, 'mean_word_length': 2.4549964564138906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5357134', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1533, 'n_words': 352}