
// Load required packages
var ToDo = require('../models/todoModel');

// Get list of todos
exports.getTodos = function (req, res) {
	listTodos(res);
}

// Create endpoint /api/recording for POSTS
exports.addTodo = function (req, res) {
	var coursecode, task, deadline, weight;
	
	coursecode = cleanInput(req.body.coursecode);
	task = cleanInput(req.body.task);
	deadline = cleanInput(req.body.deadline);
	weight = cleanInput(req.body.weight);

	// Create a new instance of the todo model
	var todo = new ToDo();

	// Set the todo properties that came from the POST data
	todo.coursecode = coursecode;
	todo.task = task;
	todo.deadline = deadline;
	todo.weight = weight;
	//todo.userid = req.user.id;

	// Save the todo and check for errors
	todo.save(function (err) {
		if (err) {
			res.send(err);
			console.log("Error adding to database was: " + err);
		} else {
			//console.log("Recording added from user " + req.user.displayName + ": ", post_text);
			console.log("Task added.");
		}
	});
	
	// Send the todos back to client
	listTodos(res);

	function cleanInput(rawinput) {
		var output;
		if (typeof rawinput === 'string' || rawinput instanceof String) {
			output = rawinput.replace(/[\${}\[\]&\0";\\]/gi, '');
			return output;
		} else {
			return "";
		}
	}

};

function listTodos(res) {
	ToDo.find({
		//'userid': req.user.id,
	})
	.sort({ weight: -1 })
	.exec(function (err, todos) {
		// Send any errors returned by the query
		if (err) {
			console.log("Analysis page query returned an error: ", err);
			res.send(err);
		} else {
			console.log("Length of todos was", todos.length);
			//console.log("Todos were", todos);
			res.json(todos);
		}
	});
}
@@ -140,16 +140,29 @@
 odos(res
+, req.user.id
 );%0A%7D%0A%0A//
@@ -678,18 +678,16 @@
 eight;%0A%09
-//
 todo.use
@@ -1007,42 +1007,8 @@
 %0A%09%0A%09
-// Send the todos back to client%0A%09
 list
@@ -1016,16 +1016,29 @@
 odos(res
+, req.user.id
 );%0A%0A%09fun
@@ -1282,19 +1282,74 @@
 odos(res
-) %7B
+, userid) %7B%0A%09console.log(%22Inside, the userid is%22, userid);
 %0A%09ToDo.f
@@ -1356,18 +1356,16 @@
 ind(%7B%0A%09%09
-//
 'userid'
@@ -1370,20 +1370,14 @@
 d': 
-req.
 user
-.
 id
-,
 %0A%09%7D)

Add user ID to each record, only retrieve user's todos