<commit_before>const models = require('../models');
const express = require('express');
const basicAuthentication = require('../lib/authentication').basicAuthentication;

const router = express.Router();

function onGetIndex(req, res) {
  res.render('admin');
}

/* GET admin page */
router.get('/', basicAuthentication, onGetIndex);

/* GET admin/add-question page */

function onGetAddQuestion(req, res) {
  res.render('admin-add-question');
}

router.get('/add-question', basicAuthentication, onGetAddQuestion);

/* POST admin/add-question page */

function onPostQuestion(req, res) {
  const questionTitle = req.body.questionTitle;
  let choices = req.body['choices[]'];

  if (typeof choices === 'string') { // Occurs when only one choice is submitted
    choices = [choices];
  }

  function mapTextToJSON(text) {
    return { text };
  }

  function renderPage() {
    res.render('admin-add-question');
  }

  function onError(error) {
    res.render('error', { error });
  }

  models.Question.create({
    title: questionTitle,
    Choices: choices.map(mapTextToJSON),
  }, {
    include: models.Choice,
  })
  .then(renderPage)
  .catch(onError);
}

router.post('/add-question', basicAuthentication, onPostQuestion);

/* GET admin/stats page */

function mapChoiceStatsToJSON(choice) {
  return {
    id: choice.id,
    text: choice.text,
    count: choice.Responses.length,
  };
}

function mapQuestionStatsToJSON(question) {
  const choices = question.Choices.map(mapChoiceStatsToJSON);

  return {
    id: question.id,
    title: question.title,
    choices,
  };
}

function getStats(req, res) {
  function renderStats(questions) {
    const byQuestion = questions.map(mapQuestionStatsToJSON);

    res.render('admin-stats', { by_question: byQuestion });
  }

  models.Question.findAll({
    include: [{ model: models.Choice, include: models.Response }],
    order: 'Question.id ASC',
  })
  .then(renderStats);
}

router.get('/stats', basicAuthentication, getStats);

module.exports = router;
<commit_msg>Revert "Apply eslint feedback to routes/admin.js."<commit_after>var models = require('../models');
var express = require('express');
var router = express.Router();
var basicAuthentication = require('../lib/authentication').basicAuthentication;

/* GET admin page */
router.get('/', basicAuthentication, function(req, res, next) {
  res.render('admin');
});

router.get('/add-question', basicAuthentication, function(req, res, next) {
  res.render('admin-add-question');
});

router.post('/add-question', basicAuthentication, function(req, res, next) {
  var questionTitle = req.body.questionTitle;
  var choices = req.body['choices[]'];

  if (typeof(choices) == 'string') { // Occurs when only one choice is submitted
    choices = [choices];
  }

  models.Question.create({
    title: questionTitle,
    Choices: choices.map(function(text) { return { text: text } })
  }, {
    include: models.Choice
  })
  .then(function() {
    res.render('admin-add-question');
  })
  .catch(function(error) {
    res.render('error', {
      error: error
    });
  });
});

router.get('/stats', basicAuthentication, function(req, res, next) {
  models.Question.findAll({
    include: [
      { model: models.Choice, include: models.Response }
    ],
    order: 'Question.id ASC'
  })
  .then(function(questions) {
    var by_question = questions.map(function(question) {
      var choices = question.Choices.map(function(choice) {
        return {
          id: choice.id,
          text: choice.text,
          count: choice.Responses.length
        };
      });

      return {
        id: question.id,
        title: question.title,
        choices: choices
      };
    });

    res.render('admin-stats', { by_question: by_question });
  })
});

module.exports = router;

{'fraction_non_alphanumeric': 0.15512684989429176, 'fraction_numerical': 0.0, 'mean_word_length': 3.565741857659831, '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': '3776460', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1151, 'n_words': 240}