<commit_before>var express     = require('express');
var router      = express.Router();
var validator   = require('validator');
var Entry       = require('../models/entry');

// *** protect from CSRF *** ///
var csrf            = require('csurf');
var csrfProtection  = csrf({ cookie: true })

// =========================================================================
// API ROUTES =============================================================
// =========================================================================

// *** get ALL Entries plus FILTERED Entries ***
router.get('/api/entries', function(req, res) {
  // create new instance of Entry model
  var newEntry = Entry.find({})

  // checking filters
  if(req.query.category) {
    newEntry.where('category').equals(req.query.category);
  }

  // response generator
  newEntry.exec(function(err, entries) {
      if(err) {
        res.json({
            "STATUS": "ERROR",
            "ERROR": err,
            "ITEMS": []
        });
      } else {
        res.json({
            "STATUS": "SUCCESS",
            "ERROR": "",
            "ITEMS": entries
        });
      }
    });
});

// *** get SINGLE Entries *** //
router.get('/api/entry/:id', function(req, res) {
  Entry.findById(req.params.id, function(err, entry) {
    if(err) {
      res.json({
          "STATUS": "ERROR",<commit_msg>Added filters to api - date, dateFrom, dateTo and sort update #5<commit_after>var express     = require('express');
var router      = express.Router();
var Entry       = require('../models/entry');

// addition modules
var validator   = require('validator');
var moment      = require('moment');

// *** protect from CSRF *** ///
var csrf            = require('csurf');
var csrfProtection  = csrf({ cookie: true })

// =========================================================================
// HELP METHODS ============================================================
// =========================================================================
var dateFormat = function(date, format) {
    var validDate = validator.toDate(date);
    return moment(validDate).format(format);
}

// =========================================================================
// API ROUTES =============================================================
// =========================================================================

// *** get ALL Entries plus FILTERED Entries ***
router.get('/api/entries', function(req, res) {
    // create new instance of Entry model
    var newEntry = Entry.find({})

    // ********** FILTERS *************

    // category filter
    if(req.query.category) {
        newEntry.where('category').equals(req.query.category);
    }

    // date filter
    if(req.query.date) {
        newEntry.where('date').equals(dateFormat(req.query.date, 'L'));
    }

    // dateFrom filter
    if(req.query.dateFrom) {
        newEntry.where('date').gt(dateFormat(req.query.dateFrom, 'L'));
    }

    // dateTo filter
    if(req.query.dateTo) {
        newEntry.where('date').lt(dateFormat(req.query.dateTo, 'L'));
    }

    // sort
    newEntry.sort('-date');

    // ********** RESPONSE GENERATOR *************
    newEntry.exec(function(err, entries) {
        if(err) {
            res.json({
                "STATUS": "ERROR",
                "ERROR": err,
                "ITEMS": []
            });
        } else {
            res.json({
                "STATUS": "SUCCESS",
                "ERROR": "",
                "ITEMS": entries
            });
        }
    });
});

// *** get SINGLE Entries *** //
router.get('/api/entry/:id', function(req, res) {
  Entry.findById(req.params.id, function(err, entry) {
    if(err) {
      res.json({
          "STATUS": "ERROR",
{'fraction_non_alphanumeric': 0.3213429256594724, 'fraction_numerical': 0.0002664535038635758, 'mean_word_length': 2.7021696252465484, 'pattern_counts': {'":': 14, '<': 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': '3669379', 'n_tokens_mistral': 1032, 'n_tokens_neox': 987, 'n_words': 199}