<commit_before>require('dotenv').load()
var api = require('../db/api')
var express = require('express')
var router = express.Router()
var mongodb = require('mongodb').MongoClient
var ObjectID = require('mongodb').ObjectID
var url = process.env.DATABASE_URL || 'mongodb://localhost/didgeridone'

router.get('/:userID', function(req, res) {
  mongodb.connect(url, function(err, db) {
    api.tasks.getTasks(db, req.params.userID).then(function(results){
      res.json(results)
      db.close();
    })
  })
})

router.post('/:userID', function(req, res) {
  mongodb.connect(url, function(err, db){
    req.body.taskID = ObjectID();
    api.tasks.createTask(db, req.params.userID, req.body).then(function(results){
      res.json(results)
      db.close();
    })
  })
})

router.put('/:userID/:taskID', function(req, res) {
  mongodb.connect(url, function(err, db){
    api.tasks.updateTask(db, req.params.userID, req.params.taskID, req.body).then(function(results){
      res.json(results)
      db.close();
    })
  })
})

router.delete('/:userID/:taskID', function(req, res) {
  mongodb.connect(url, function(err, db){
    api.tasks.deleteTask(db, req.params.userID, req.body).then(function(results){
      res.json(results)
      db.close();
    })
  })
})

module.exports = router;<commit_msg>adding error handling to task routes<commit_after>require('dotenv').load()
var api = require('../db/api')
var express = require('express')
var router = express.Router()
var mongodb = require('mongodb').MongoClient
var ObjectID = require('mongodb').ObjectID
var url = process.env.DATABASE_URL || 'mongodb://localhost/didgeridone'

/* Route to retrieve information for a user's task */
router.get('/:userID', function(req, res) {
  var results = testUserID(res, req.params.userID)
  var userID = results.userID
  if (results.error) {
    return
  }

  mongodb.connect(url, function(err, db) {
    api.tasks.getTasks(db, userID).then(function(results){
      if (results.length < 1) {
        res.json({ "error": "Could not find user" })
      } else {
        res.json({ "user": {
          "_id": results[0]._id,
          "tasks": results[0].tasks
        }})
      }
      db.close();
    }).catch(function(error) {
      res.json({
        "error": "Error locating task.",
        "message": error
      })
      db.close()
    })
  })
})

/* Route to create a task for a user */
router.post('/:userID', function(req, res) {
  var results = testUserID(res, req.params.userID)
  var userID = results.userID
  if (results.error) {
    return
  }

  if (checkErrorTaskData(res, req.body)) {
    return
  }

  mongodb.connect(url, function(err, db){
    req.body.radius = JSON.parse(req.body.radius)
    req.body.done = JSON.parse(req.body.done)
    req.body.enter = JSON.parse(req.body.enter)
    api.tasks.createTask(db, userID, req.body).then(function(results){
      if (results.result.nModified === 1) {
        res.json({ "success": "Successfully created task: " + req.body.name})
      } else if (results.result.n === 0) {
        res.json({ "error": "User not found. Task could not be created. "})
      } else {
        res.json( { "error": "Task was not created correctly."})
      }
      db.close();
    }).catch(function(error) {
      res.json({
        "error": "Error creating task.",
        "message": error
      })
      db.close()
    })
  })
})

/* Route to update a task for a user */
router.put('/:userID/:taskID', function(req, res) {
  var results = testUserID(res, req.params.userID)
  var userID = results.userID
  if (results.error) {
    return
  }

  if (checkErrorTaskData(res, req.body)) {
    return
  }

  mongodb.connect(url, function(err, db){
    req.body.radius = JSON.parse(req.body.radius)
    req.body.done = JSON.parse(req.body.done)
    req.body.enter = JSON.parse(req.body.enter)
    api.tasks.updateTask(db, userID, req.params.taskID, req.body).then(function(results){
      if (results.result.nModified === 1) {
        res.json({ "success": "Task updated correctly."})
      } else if (results.result.n === 1) {
        res.json({ "error": "Task not updated."})
      } else {
        res.json({ "error": "User not found."})
      }
      db.close();
    }).catch(function(error) {
      res.json({
        "error": "Error updating task.",
        "message": error
      })
      db.close()
    })
  })
})

/* Route to delete a task for a user */
router.delete('/:userID/:taskID', function(req, res) {
  var results = testUserID(res, req.params.userID)
  var userID = results.userID
  if (results.error) {
    return
  }

  mongodb.connect(url, function(err, db){
    api.tasks.deleteTask(db, req.params.userID, req.body).then(function(results){
      if (results.result.nModified === 1) {
        res.json({ "success": "Task deleted correctly."})
      } else if (results.result.n === 1) {
        res.json({ "error": "Task not deleted."})
      } else {
        res.json({ "error": "User not found."})
      }
      db.close();
    }).catch(function(error) {
      res.json({
        "error": "Error deleting task.",
        "message": error
      })
      db.close()
    })
  })
})

module.exports = router;

function testUserID(res, id) {
  var results = {}
  try {
    results.userID = ObjectID(id)
  }
  catch(error) {
    results.error = true
    res.json({
      "error": "Invalid user ID.",
      "message": error.toString()
    })
  }
  return results
}

function checkErrorTaskData(res, data) {
  if (!data.name || !data.lat || !data.long || !data.radius ||
          !data.done || !data.enter) {
    res.json({
      "error": "Invalid PUT or POST format. See below for correct format.",
      "message": {
        "format": "Format must contain all fields as a JSON object.",
        "correct_format": {
          "name": "TASK NAME",
          "lat": "TASK LATITUDE",
          "long": "TASK LONGITUDE",
          "radius": 10,
          "done": true,
          "enter": false
        }
      }
    })
    return true
  }
  return false
}
{'fraction_non_alphanumeric': 0.17200533867200535, 'fraction_numerical': 0.0018351685018351686, 'mean_word_length': 2.6869618696186963, 'pattern_counts': {'":': 33, '<': 4, '<?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': '3165506', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1987, 'n_words': 428}