var express = require('express');
var app = express();
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var bodyParser = require('body-parser');
//var methodOverride = require('method-override');

mongoose.connect('mongodb://127.0.0.1:27017/TodoAppDB');

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
//app.use(methodOverride());

// define DB
var schema = new Schema({
	taskname: String,
	Done: Boolean,
	details: String
});

var Todo = mongoose.model('tasks', schema);

app.get('/todolist', function(req, res) {
	console.log("I recived a get request.");

	Todo.find(function(err, tasks) {
		res.json(tasks);
	});

});

app.post('/todolist', function(req, res) {
	Todo.create(req.body, function(err, tasks) {
		res.json(tasks);
	});
});

app.put('/todolist', function(req, res){
	var id = req.params.id;
	console.log(req.body.Done);
	db.contactlist.findAndModify({query: {_id: mongojs.ObjectId(id)},
		update: {$set: {name: req.body.name, email: req.body.email, number: req.body.number}},
		new: true}, function(err, doc) {
			res.json(doc);
});


app.listen(8080);
console.log("Server started on port 8080...");