<commit_before>import { db } from '../main';
import { addCompetition, getRunningCompetitions } from '../competition-store';
import { loadUserObject } from './user';

// Loads a competition entry.
// If there's a query parameter "loadResults" with a value of "true", then
// in addition to the normal competition attributes, all competition results
// stored so far will be returned within the object in an array with the key
// 'results'.
export function getCompetition(req, res) {
  let competition = {};
  let results = [];
  const loadResults = req.query.hasOwnProperty('loadResults') && req.query.loadResults.toLowerCase() === "true";

  db.query('SELECT id, created_at, language FROM competitions WHERE id=$1', [req.params.id])
    .then(result => {
      if (result.rows.length === 0)
        return res.status(404).json({ error: 'Competition not found' });

      competition = result.rows[0];
      if (loadResults)
        return loadCompetitionResults(competition.id);
      return Promise.resolve([]);
    })
    .then(result => {
      competition.results = result;
      res.json(competition);
    })
    .catch(err => {
      res.status(500).json({ error: err.message });
    });
}

// Creates a new competition.
export function createCompetition(req, res) {
  const { language, content } = req.body;

  db.query('INSERT INTO competitions(language, created_at) ' +
    'VALUES ($1, CURRENT_TIMESTAMP) RETURNING id, language, created_at', [language])
    .then(result => {
      if (result.rows.length !== 1)
        return res.status(500).end();
      else {
        const competitionId = result.rows[0].id;
        // Add to competition store
        addCompetition({
          id: competitionId,
          createdAt: result.rows[0].created_at,
          language,
          finished: false,
          content,
          createdBy: req.user.name,
        });<commit_msg>Don't store competition creator name<commit_after>import { db } from '../main';
import { addCompetition, getRunningCompetitions } from '../competition-store';
import { loadUserObject } from './user';

// Loads a competition entry.
// If there's a query parameter "loadResults" with a value of "true", then
// in addition to the normal competition attributes, all competition results
// stored so far will be returned within the object in an array with the key
// 'results'.
export function getCompetition(req, res) {
  let competition = {};
  let results = [];
  const loadResults = req.query.hasOwnProperty('loadResults') && req.query.loadResults.toLowerCase() === "true";

  db.query('SELECT id, created_at, language FROM competitions WHERE id=$1', [req.params.id])
    .then(result => {
      if (result.rows.length === 0)
        return res.status(404).json({ error: 'Competition not found' });

      competition = result.rows[0];
      if (loadResults)
        return loadCompetitionResults(competition.id);
      return Promise.resolve([]);
    })
    .then(result => {
      competition.results = result;
      res.json(competition);
    })
    .catch(err => {
      res.status(500).json({ error: err.message });
    });
}

// Creates a new competition.
export function createCompetition(req, res) {
  const { language, content } = req.body;

  db.query('INSERT INTO competitions(language, created_at) ' +
    'VALUES ($1, CURRENT_TIMESTAMP) RETURNING id, language, created_at', [language])
    .then(result => {
      if (result.rows.length !== 1)
        return res.status(500).end();
      else {
        const competitionId = result.rows[0].id;
        // Add to competition store
        addCompetition({
          id: competitionId,
          createdAt: result.rows[0].created_at,
          language,
          finished: false,
          content,
        });
{'fraction_non_alphanumeric': 0.1344157572531275, 'fraction_numerical': 0.008517434122970456, 'mean_word_length': 2.9516298633017874, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '1167610', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1050, 'n_words': 306}