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

var PAIRS_COLLECTION = "pairs";
var MEMBER_COLLECTION = "members";

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
 */

var baseUrl = "/members";
app.get(baseUrl, function (req, res) {
});

app.post(baseUrl, function (req, res) {
  db.collection(MEMBER_COLLECTION).insertOne(getNewMember(req.body || {}), function (err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to create new member.");
    } else {
      res.status(201).json(doc.ops[0]);
    }
  });
});

/**
 *  "/members/:id"
 *    GET: find member by id
 *    PUT: update member by id
 *    DELETE: deletes member by id
 */
app.get(baseUrl + "/:id", function (req, res) {
});

app.put(baseUrl + "/:id", function (req, res) {
});

app.delete(baseUrl + "/:id", function (req, res) {
});

/**
 * Validates and creates a new member
 * @param input {{userId: String, name: String, startDate: Date}}<commit_msg>Implemented more of the API.<commit_after>var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;

var PAIRS_COLLECTION = "pairs";
var MEMBER_COLLECTION = "members";

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("/status", function (req, res) {
  res.status(200).json({"up": true})
});

var baseUrl = "/members";

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

app.post(baseUrl, function (req, res) {
  db.collection(MEMBER_COLLECTION).insertOne(getNewMember(req.body || {}), function (err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to create new member.");
    } else {
      res.status(201).json(doc.ops[0]);
    }
  });
});

/**
 *  "/members/:id"
 *    GET: find member by id
 *    PUT: update member by id
 *    DELETE: deletes member by id
 */
app.get(baseUrl + "/:id", function (req, res) {
  db.collection(MEMBER_COLLECTION).findOne({_id: new ObjectID(req.params.id)}, function (err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to get member");
    } else {
      res.status(200).json(doc);
    }
  });
});

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

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

app.delete(baseUrl + "/:id", function (req, res) {
  db.collection(MEMBER_COLLECTION).deleteOne({_id: new ObjectID(req.params.id)}, function (err) {
    if (err) {
      handleError(res, err.message, "Failed to delete member.");
    } else {
      res.status(204).end();
    }
  });
});

/**
 * Validates and creates a new member
 * @param input {{userId: String, name: String, startDate: Date}}
{'fraction_non_alphanumeric': 0.1626877165960724, 'fraction_numerical': 0.007508663842895649, 'mean_word_length': 3.617777777777778, 'pattern_counts': {'":': 3, '<': 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': '553110', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1681, 'n_words': 466}