<commit_before>var db = require('../../../models');

module.exports = {
  list: async (req, res) => {
    let perPage = 2;
    let offset = (!req.query.page || isNaN(req.query.page)) ? 0 : req.query.page;
    var users = await db.user.findAndCountAll({
      limit: perPage,
      offset: (offset - 1) * perPage
    });

    let resultSet = {
      total: users.count,
      per_page: perPage,
      current_page: offset,
      last_page: 'x',
      next_page_url: 'x',
      prev_page_url: 'x',
      'from': offset,
      'to': 'x',
      data: users.rows
    }

    res.json(resultSet);
  }
};
<commit_msg>Working prototype of API endpoint. Need to port to a module<commit_after>var db = require('../../../models');

const perPage = 2;

function calculateNextPage(req, resultSet, currentPage, lastPage) {
  if (currentPage >= lastPage) {
    return null;
  }
  else if (currentPage < 1) {
    return 1;
  }
  else {
    return currentPage+1;
  }
}

function calculatePreviousPage(req, resultSet, currentPage, lastPage) {
  if (currentPage <= 1) {
    return null;
  }
  else if (currentPage > lastPage) {
    return lastPage;
  }
  else {
    return (currentPage-1);
  }
}

module.exports = {
  list: async (req, res) => {

    let currentPage = (!req.query.page || isNaN(req.query.page)) ? 1 : parseInt(req.query.page);

    var resultSet = await db.user.findAndCountAll({
      limit: perPage,
      offset: (currentPage - 1) * perPage
    });

    let lastPage = Math.ceil(resultSet.count/perPage);
    let baseUrl = `${req.protocol}://${req.get('host')}${req.path}`;

    let nextPageNumber = calculateNextPage(req, resultSet, currentPage, lastPage);
    let previousPageNumber = calculatePreviousPage(req, resultSet, currentPage, lastPage);

    let retval = {
      total: resultSet.count,
      per_page: perPage,
      current_page: currentPage,
      last_page: lastPage,
      next_page_url: nextPageNumber !== null ? `${baseUrl}?page=${nextPageNumber}` : null,
      prev_page_url: previousPageNumber !== null ? `${baseUrl}?page=${previousPageNumber}` : null,
      'from': currentPage,
      'to': (currentPage-1)+resultSet.rows.length,
      data: resultSet.rows
    }

    res.json(retval);
  }
};

{'fraction_non_alphanumeric': 0.14943566591422122, 'fraction_numerical': 0.005417607223476298, 'mean_word_length': 2.936056838365897, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3211426', 'n_tokens_mistral': 747, 'n_tokens_neox': 734, 'n_words': 168}