<commit_before>var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;

var CONTACTS_COLLECTION = "contacts";

var app = express();
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());

// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
var db;

// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI, function (err, database) {
  if (err) {
    console.log(err);
    process.exit(1);
  }

  // Save database object from the callback for reuse.
  db = database;
  console.log("Database connection ready");

  // Initialize the app.
  var server = app.listen(process.env.PORT || 8080, function () {
    var port = server.address().port;
    console.log("App now running on port", port);
  });
});

// CONTACTS API ROUTES BELOW

// Generic error handler used by all endpoints.
function handleError(res, reason, message, code) {
  console.log("ERROR: " + reason);
  res.status(code || 500).json({"error": message});
}

/*  "/contacts"
 *    GET: finds all contacts
 *    POST: creates a new contact
 */

app.get("/contacts", function(req, res) {
  db.collection(CONTACTS_COLLECTION).find({}).toArray(function(err, docs) {
    if (err) {
      handleError(res, err.message, "Failed to get contacts.");
    } else {
      res.status(200).json(docs);
    }
  });
});

app.post("/contacts", function(req, res) {
  var newContact = req.body;
    newContact.createDate = new Date().toLocaleDateString();

//toLocaleString().substring(0,9);

//  if (!(req.body.firstName || req.body.lastName)) {
//    handleError(res, "Invalid user input", "Must provide a first or last name.", 400);
//  }

  db.collection(CONTACTS_COLLECTION).insertOne(newContact, function(err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to create new contact.");
    } else {
      res.status(201).json(doc.ops[0]);
    }
  });
});

/*  "/contacts/:id"
 *    GET: find contact by id
 *    PUT: update contact by id
 *    DELETE: deletes contact by id
 */

app.get("/contacts/:id", function(req, res) {
  db.collection(CONTACTS_COLLECTION).findOne({ _id: new ObjectID(req.params.id) }, function(err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to get contact");
    } else {
      res.status(200).json(doc);
    }
  });
});

app.get("/api/v1/test/:name", function(req, res) {
    console.log("Param: " + req.params.name);
    db.collection(CONTACTS_COLLECTION).find({ name: req.params.name }).toArray( function(err, doc) {
        if (err) {
            handleError(res, err.message, "Failed to list name");
        } else {
            res.status(200).json(doc);
        }
    });
});
//-----------------------------------------------
// API V1 -> SEARCH
//
app.get("/api/v1/search", function(req, res) {
    if (req.query.source == "*" || ( typeof req.query.source == 'undefined')) { source = { $regex: /\w|/, $options:"i"} } else { source = req.query.source }
    if (req.query.name == "*" || (typeof req.query.name == 'undefined')) { name = { $regex: /\w|/, $options:"i"} } else { name = req.query.name }
    if (typeof req.query.before == 'undefined') { before = ObjectID(Math.floor((new Date('2100/12/31'))/1000).toString(16) + "0000000000000000") } else { before = ObjectID(Math.floor((new Date(req.query.before))/1000).toString(16) + "0000000000000000") }
    if (typeof req.query.after == 'undefined') { after = ObjectID(Math.floor((new Date('2016/01/01'))/1000).toString(16) + "0000000000000000") } else { after = ObjectID(Math.floor((new Date(req.query.after))/1000).toString(16) + "0000000000000000") }
    if (typeof req.query.filtre == 'undefined') { filtre = {} } else { if (req.query.filtre == "name") { filtre = {name: 1, value:  1, _id: 0} } else {filtre = { value: 1 , _id: 0 } } }
    
	selection = { name: name, source: source , _id: {$lt: before, $gt: after}} ;

	console.log("before = "+before+"  after : "+after);
	
    db.collection(CONTACTS_COLLECTION).find( selection , filtre ).toArray( function(err, doc) {
        if (err) {
            handleError(res, err.message, "Failed to find contact");
        } else {
            res.status(200).json(doc);
        }
    });
});

app.get("/api/v1/name", function(req, res) {
        db.collection(CONTACTS_COLLECTION).distinct( "name" , function(err, doc) {
            if (err) {
                handleError(res, err.message, "Failed to find contact");
            } else {
                res.status(200).json(doc);
            }
    });
});


app.get("/api/v1/source", function(req, res) {
    db.collection(CONTACTS_COLLECTION).distinct( "source" , function(err, doc) {
        if (err) {
            handleError(res, err.message, "Failed to find contact");
        } else {
            res.status(200).json(doc);
        }
    });
});

app.put("/contacts/:id", function(req, res) {
  var updateDoc = req.body;
  delete updateDoc._id;

  db.collection(CONTACTS_COLLECTION).updateOne({_id: new ObjectID(req.params.id)}, updateDoc, function(err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to update contact");
    } else {
      res.status(204).end();
    }
  });
});

app.delete("/api/v1/:id", function(req, res) {
  db.collection(CONTACTS_COLLECTION).deleteOne({_id: new ObjectID(req.params.id)}, function(err, result) {
    if (err) {
      handleError(res, err.message, "Failed to delete contact");
    } else {
      res.status(204).end();
    }
  });
});

app.delete("/api/v1/name/:name", function(req, res) {
    db.collection(CONTACTS_COLLECTION).deleteMany({ name: req.params.name}, function(err, result) {
        if (err) {
            handleError(res, err.message, "Failed to delete contact");
        } else {
            res.status(204).end();
        }
    });<commit_msg>api delete<commit_after>var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;

var CONTACTS_COLLECTION = "contacts";

var app = express();
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());

// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
var db;

// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI, function (err, database) {
  if (err) {
    console.log(err);
    process.exit(1);
  }

  // Save database object from the callback for reuse.
  db = database;
  console.log("Database connection ready");

  // Initialize the app.
  var server = app.listen(process.env.PORT || 8080, function () {
    var port = server.address().port;
    console.log("App now running on port", port);
  });
});

// CONTACTS API ROUTES BELOW

// Generic error handler used by all endpoints.
function handleError(res, reason, message, code) {
  console.log("ERROR: " + reason);
  res.status(code || 500).json({"error": message});
}

/*  "/contacts"
 *    GET: finds all contacts
 *    POST: creates a new contact
 */

app.get("/contacts", function(req, res) {
  db.collection(CONTACTS_COLLECTION).find({}).toArray(function(err, docs) {
    if (err) {
      handleError(res, err.message, "Failed to get contacts.");
    } else {
      res.status(200).json(docs);
    }
  });
});

app.post("/contacts", function(req, res) {
  var newContact = req.body;
  var d = new Date();
    newContact.Date = d.toLocaleDateString();
    newContact.Heure = d.toLocaleTimeString();

//toLocaleString().substring(0,9);

//  if (!(req.body.firstName || req.body.lastName)) {
//    handleError(res, "Invalid user input", "Must provide a first or last name.", 400);
//  }

  db.collection(CONTACTS_COLLECTION).insertOne(newContact, function(err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to create new contact.");
    } else {
      res.status(201).json(doc.ops[0]);
    }
  });
});

/*  "/contacts/:id"
 *    GET: find contact by id
 *    PUT: update contact by id
 *    DELETE: deletes contact by id
 */

app.get("/contacts/:id", function(req, res) {
  db.collection(CONTACTS_COLLECTION).findOne({ _id: new ObjectID(req.params.id) }, function(err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to get contact");
    } else {
      res.status(200).json(doc);
    }
  });
});

app.get("/api/v1/test/:name", function(req, res) {
    console.log("Param: " + req.params.name);
    db.collection(CONTACTS_COLLECTION).find({ name: req.params.name }).toArray( function(err, doc) {
        if (err) {
            handleError(res, err.message, "Failed to list name");
        } else {
            res.status(200).json(doc);
        }
    });
});
//-----------------------------------------------
// API V1 -> SEARCH
//
app.get("/api/v1/search", function(req, res) {
    if (req.query.source == "*" || ( typeof req.query.source == 'undefined')) { source = { $regex: /\w|/, $options:"i"} } else { source = req.query.source }
    if (req.query.name == "*" || (typeof req.query.name == 'undefined')) { name = { $regex: /\w|/, $options:"i"} } else { name = req.query.name }
    if (typeof req.query.before == 'undefined') { before = ObjectID(Math.floor((new Date('2100/12/31'))/1000).toString(16) + "0000000000000000") } else { before = ObjectID(Math.floor((new Date(req.query.before))/1000).toString(16) + "0000000000000000") }
    if (typeof req.query.after == 'undefined') { after = ObjectID(Math.floor((new Date('2016/01/01'))/1000).toString(16) + "0000000000000000") } else { after = ObjectID(Math.floor((new Date(req.query.after))/1000).toString(16) + "0000000000000000") }
    if (typeof req.query.filtre == 'undefined') { filtre = {} } else { if (req.query.filtre == "name") { filtre = {name: 1, value:  1, _id: 0} } else {filtre = { value: 1 , _id: 0 } } }
    
	selection = { name: name, source: source , _id: {$lt: before, $gt: after}} ;

	console.log("before = "+before+"  after : "+after);
	
    db.collection(CONTACTS_COLLECTION).find( selection , filtre ).toArray( function(err, doc) {
        if (err) {
            handleError(res, err.message, "Failed to find contact");
        } else {
            res.status(200).json(doc);
        }
    });
});

app.get("/api/v1/name", function(req, res) {
        db.collection(CONTACTS_COLLECTION).distinct( "name" , function(err, doc) {
            if (err) {
                handleError(res, err.message, "Failed to find contact");
            } else {
                res.status(200).json(doc);
            }
    });
});


app.get("/api/v1/source", function(req, res) {
    db.collection(CONTACTS_COLLECTION).distinct( "source" , function(err, doc) {
        if (err) {
            handleError(res, err.message, "Failed to find contact");
        } else {
            res.status(200).json(doc);
        }
    });
});

app.get("/api/v1/date", function(req, res) {
    db.collection(CONTACTS_COLLECTION).distinct( "date" , function(err, doc) {
        if (err) {
            handleError(res, err.message, "Failed to find contact");
        } else {
            res.status(200).json(doc);
        }
    });
});

app.put("/contacts/:id", function(req, res) {
  var updateDoc = req.body;
  delete updateDoc._id;

  db.collection(CONTACTS_COLLECTION).updateOne({_id: new ObjectID(req.params.id)}, updateDoc, function(err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to update contact");
    } else {
      res.status(204).end();
    }
  });
});

app.delete("/api/v1/:id", function(req, res) {
  db.collection(CONTACTS_COLLECTION).deleteOne({_id: new ObjectID(req.params.id)}, function(err, result) {
    if (err) {
      handleError(res, err.message, "Failed to delete contact");
    } else {
      res.status(204).end();
    }
  });
});

app.delete("/api/v1/name/:name", function(req, res) {
    db.collection(CONTACTS_COLLECTION).deleteMany({ name: req.params.name}, function(err, result) {
        if (err) {
            handleError(res, err.message, "Failed to delete contact");
        } else {
            res.status(204).end();
        }
    });
{'fraction_non_alphanumeric': 0.1846743920352538, 'fraction_numerical': 0.026440346009466296, 'mean_word_length': 2.9827754306142347, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, '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': '553131', 'n_tokens_mistral': 4279, 'n_tokens_neox': 3981, 'n_words': 891}