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


// GET /quizzes

exports.load = function(req, res, next, quizId) {
	models.Quiz.findById(quizId)
  		.then(function(quiz) {
      		if (quiz) {
        		req.quiz = quiz;
        		next();
      		} else { 
      			next(new Error('No existe quizId=' + quizId));
      		}
        })
        .catch(function(error) { next(error); });
};


exports.index = function(req, res) {
  if (req.query.search===undefined) {
    models.Quiz.findAll().then(
      function(quizzes) {
        res.render('quizzes/index.ejs', {quizzes: quizzes, errors: []});
      }
    ).catch(function(error){next(error)});
  }
  else {
    models.Quiz.findAll({where:["question like ?", "%"+req.query.search.replace(/\s/g,"%")+"%"], order: 'question ASC'}).then(function(quizzes) {
      res.render('quizzes/index.ejs', { quizzes: quizzes, errors: []});
    }).catch(function(error) { next(error);})
  }
};



// GET /quizzes/:id
exports.show = function(req, res, next) {
	models.Quiz.findById(req.params.quizId)
		.then(function(quiz) {
			if (quiz) {
				var answer = req.query.answer || '';

				res.render('quizzes/show', {quiz: req.quiz,
											answer: answer});
			} else {
		    	throw new Error('No existe ese quiz en la BBDD.');
		    }
		})
		.catch(function(error) {
			next(error);
		});
};


// GET /quizzes/:id/check
exports.check = function(req, res) {
	models.Quiz.findById(req.params.quizId)
		.then(function(quiz) {
			if (quiz) {
				var answer = req.query.answer || "";

				var result = answer === req.quiz.answer ? 'Correcta' : 'Incorrecta';

				res.render('quizzes/result', { quiz: req.quiz, 
											   result: result, 
											   answer: answer });
			} else {
				throw new Error('No existe ese quiz en la BBDD.');
			}
		})
		.catch(function(error) {
			next(error);
		});	
};

// GET /quizzes/new

exports.new = function(req, res, next) {
  var quiz = models.Quiz.build({question: "", answer: ""});
  res.render('quizzes/new', {quiz: quiz});
};

// POST /quizzes/create
exports.create = function(req, res, next) {
  var quiz = models.Quiz.build({ question: req.body.quiz.question, 
  	                             answer:   req.body.quiz.answer} );<commit_msg>Validación de Entradas<commit_after>
var models = require('../models');


// GET /quizzes

exports.load = function(req, res, next, quizId) {
	models.Quiz.findById(quizId)
  		.then(function(quiz) {
      		if (quiz) {
        		req.quiz = quiz;
        		next();
      		} else { 
      			next(new Error('No existe quizId=' + quizId));
      		}
        })
        .catch(function(error) { next(error); });
};


exports.index = function(req, res) {
  if (req.query.search===undefined) {
    models.Quiz.findAll().then(
      function(quizzes) {
        res.render('quizzes/index.ejs', {quizzes: quizzes, errors: []});
      }
    ).catch(function(error){next(error)});
  }
  else {
    models.Quiz.findAll({where:["question like ?", "%"+req.query.search.replace(/\s/g,"%")+"%"], order: 'question ASC'}).then(function(quizzes) {
      res.render('quizzes/index.ejs', { quizzes: quizzes, errors: []});
    }).catch(function(error) { next(error);})
  }
};



// GET /quizzes/:id
exports.show = function(req, res, next) {
	models.Quiz.findById(req.params.quizId)
		.then(function(quiz) {
			if (quiz) {
				var answer = req.query.answer || '';

				res.render('quizzes/show', {quiz: req.quiz,
											answer: answer});
			} else {
		    	throw new Error('No existe ese quiz en la BBDD.');
		    }
		})
		.catch(function(error) {
			next(error);
		});
};


// GET /quizzes/:id/check
exports.check = function(req, res) {
	models.Quiz.findById(req.params.quizId)
		.then(function(quiz) {
			if (quiz) {
				var answer = req.query.answer || "";

				var result = answer === req.quiz.answer ? 'Correcta' : 'Incorrecta';

				res.render('quizzes/result', { quiz: req.quiz, 
											   result: result, 
											   answer: answer });
			} else {
				throw new Error('No existe ese quiz en la BBDD.');
			}
		})
		.catch(function(error) {
			next(error);
		});	
};

//GET /quizzes/new

exports.new = function(req, res, next) {
  var quiz = models.Quiz.build({question: "", answer: ""});
  res.render('quizzes/new', {quiz: quiz});
};

// POST /quizzes/create
exports.create = function(req, res, next) {
  var quiz = models.Quiz.build({ question: req.body.quiz.question, 
  	                             answer:   req.body.quiz.answer} );
{'fraction_non_alphanumeric': 0.18842530282637954, 'fraction_numerical': 0.0, 'mean_word_length': 2.6193181818181817, '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': '6319563', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1629, 'n_words': 290}