Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed delete functionality from removing the todo to completing it and made api access to access completed todos #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ Node provides the RESTful API. Angular provides the frontend and accesses the AP

1. Clone the repository: `git clone [email protected]:scotch-io/node-todo`
2. Install the application: `npm install`
3. Start the server: `node server.js`
4. View in browser at `http://localhost:8080`
3. Make sure mongodb is accessable at `http://localhost:27017`
4. Start the server: `node server.js`
5. View in browser at `http://localhost:2020`

## Tutorial Series

Expand Down
6 changes: 4 additions & 2 deletions app/models/todo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var mongoose = require('mongoose');

module.exports = mongoose.model('Todo', {
text : {type : String, default: ''}
});
text : {type : String, default: ''},
done : {type : Boolean, default:false},

});
51 changes: 39 additions & 12 deletions app/routes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var Todo = require('./models/todo');

function getTodos(res){
Todo.find(function(err, todos) {
Todo.find({done:false},function(err, todos) {

// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
Expand All @@ -11,6 +11,18 @@ function getTodos(res){
});
};

function getOldTodos(res){
Todo.find({done:true},function(err, todos) {

// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)

res.json(todos); // return all todos in JSON format
});
}


module.exports = function(app) {

// api ---------------------------------------------------------------------
Expand All @@ -27,7 +39,8 @@ module.exports = function(app) {
// create a todo, information comes from AJAX request from Angular
Todo.create({
text : req.body.text,
done : false
done : false,
//timeCompleted: null
}, function(err, todo) {
if (err)
res.send(err);
Expand All @@ -38,20 +51,34 @@ module.exports = function(app) {

});

// delete a todo

// Check off todo
app.delete('/api/todos/:todo_id', function(req, res) {
Todo.remove({
_id : req.params.todo_id
}, function(err, todo) {
if (err)
res.send(err);

getTodos(res);
});
Todo.findById(req.params.todo_id, function (err, todo) {
if (err)
res.send(err);
todo.done = true;
//todo.timeCompleted = Date.now();
todo.save(function (err) {
if (err)
res.send(err);
getTodos(res);
});
});

});


app.get('/api/completed', function(req, res) {
//res.sendfile('./public/old.html');
getOldTodos(res);
});


// application -------------------------------------------------------------
app.get('*', function(req, res) {
res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
res.sendfile('./public/index.html'); // load the view file (angular will handle the page changes on the front-end)
});
};

};
2 changes: 1 addition & 1 deletion config/database.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {

// the database url to connect
url : 'mongodb://node:[email protected]:27017/uwO3mypu'
url : 'mongodb://localhost:27017'
}
3 changes: 2 additions & 1 deletion public/js/controllers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ angular.module('todoController', [])
$scope.loading = false;
});


// CREATE ==================================================================
// when submitting the add form, send the text to the node API
$scope.createTodo = function() {
Expand Down Expand Up @@ -47,4 +48,4 @@ angular.module('todoController', [])
$scope.todos = data; // assign our new list of todos
});
};
}]);
}]);
3 changes: 2 additions & 1 deletion public/js/services/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ angular.module('todoService', [])
delete : function(id) {
return $http.delete('/api/todos/' + id);
}

}
}]);
}]);
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var express = require('express');
var app = express(); // create our app w/ express
var mongoose = require('mongoose'); // mongoose for mongodb
var port = process.env.PORT || 8080; // set the port
var port = process.env.PORT || 2020; // set the port
var database = require('./config/database'); // load the database config
var morgan = require('morgan');
var bodyParser = require('body-parser');
Expand Down