From ff804ba2a5fb9d27fdec0287f20d164ee4d7629d Mon Sep 17 00:00:00 2001 From: slightlycyborg Date: Thu, 15 Oct 2015 08:57:10 -0400 Subject: [PATCH 1/3] added done functionality --- app/models/todo.js | 6 ++- app/routes.js | 55 ++++++++++++++++++----- config/database.js | 2 +- public/js/controllers/main.js | 3 +- public/js/services/todos.js | 3 +- public/old.html | 83 +++++++++++++++++++++++++++++++++++ server.js | 2 +- 7 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 public/old.html diff --git a/app/models/todo.js b/app/models/todo.js index 082a2ee37..cae388efa 100644 --- a/app/models/todo.js +++ b/app/models/todo.js @@ -1,5 +1,7 @@ var mongoose = require('mongoose'); module.exports = mongoose.model('Todo', { - text : {type : String, default: ''} -}); \ No newline at end of file + text : {type : String, default: ''}, + done : {type : Boolean, default:false}, + +}); diff --git a/app/routes.js b/app/routes.js index 17a201567..f149b38bd 100644 --- a/app/routes.js +++ b/app/routes.js @@ -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) @@ -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 --------------------------------------------------------------------- @@ -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); @@ -38,20 +51,38 @@ 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/old', function(req, res) { + //res.sendfile('./public/old.html'); + getOldTodos(res); + }); + + + app.get('/old', function(req, res) { + res.sendfile('./public/old.html'); // load the view file (angular will handle the page changes on the front-end) }); // 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) }); -}; \ No newline at end of file + +}; diff --git a/config/database.js b/config/database.js index cd3586632..e4788fe5c 100644 --- a/config/database.js +++ b/config/database.js @@ -1,5 +1,5 @@ module.exports = { // the database url to connect - url : 'mongodb://node:nodeuser@mongo.onmodulus.net:27017/uwO3mypu' + url : 'mongodb://localhost:27017' } diff --git a/public/js/controllers/main.js b/public/js/controllers/main.js index 7cb2ce277..2d6ecf657 100644 --- a/public/js/controllers/main.js +++ b/public/js/controllers/main.js @@ -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() { @@ -47,4 +48,4 @@ angular.module('todoController', []) $scope.todos = data; // assign our new list of todos }); }; - }]); \ No newline at end of file + }]); diff --git a/public/js/services/todos.js b/public/js/services/todos.js index 49761e727..bd34e264d 100644 --- a/public/js/services/todos.js +++ b/public/js/services/todos.js @@ -13,5 +13,6 @@ angular.module('todoService', []) delete : function(id) { return $http.delete('/api/todos/' + id); } + } - }]); \ No newline at end of file + }]); diff --git a/public/old.html b/public/old.html new file mode 100644 index 000000000..4f0caccc3 --- /dev/null +++ b/public/old.html @@ -0,0 +1,83 @@ + + + + + + + + + + Node/Angular Todo App + + + + + + + + + + + + + + + + +
+ + +
+

I'm a Todo-aholic {{ todos.length }}

+
+ + +
+
+ + + + +
+ +
+ +

+ +

+ +
+
+ + +
+
+
+
+ + + +
+ + + +
+
+
+ +
+

A demo by Scotch.

+

Read the tutorial.

+
+ +
+ + + diff --git a/server.js b/server.js index 2bb0a9bc1..9f969c853 100644 --- a/server.js +++ b/server.js @@ -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'); From cc1a29d21db16ba9a3e07942ccdb0b88327e5970 Mon Sep 17 00:00:00 2001 From: slightlycyborg Date: Thu, 15 Oct 2015 09:18:38 -0400 Subject: [PATCH 2/3] ready for pull --- public/old.html | 83 ------------------------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 public/old.html diff --git a/public/old.html b/public/old.html deleted file mode 100644 index 4f0caccc3..000000000 --- a/public/old.html +++ /dev/null @@ -1,83 +0,0 @@ - - - - - - - - - - Node/Angular Todo App - - - - - - - - - - - - - - - - -
- - -
-

I'm a Todo-aholic {{ todos.length }}

-
- - -
-
- - - - -
- -
- -

- -

- -
-
- - -
-
-
-
- - - -
- - - -
-
-
- -
-

A demo by Scotch.

-

Read the tutorial.

-
- -
- - - From ebcb12ec68ae96e6c7ae150d50f19b451f9ba7be Mon Sep 17 00:00:00 2001 From: slightlycyborg Date: Thu, 15 Oct 2015 09:27:22 -0400 Subject: [PATCH 3/3] readme --- README.md | 5 +++-- app/routes.js | 6 +----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 301c768ba..1f1dba2ce 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,9 @@ Node provides the RESTful API. Angular provides the frontend and accesses the AP 1. Clone the repository: `git clone git@github.com: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 diff --git a/app/routes.js b/app/routes.js index f149b38bd..fc4396cdd 100644 --- a/app/routes.js +++ b/app/routes.js @@ -70,16 +70,12 @@ module.exports = function(app) { }); - app.get('/api/old', function(req, res) { + app.get('/api/completed', function(req, res) { //res.sendfile('./public/old.html'); getOldTodos(res); }); - app.get('/old', function(req, res) { - res.sendfile('./public/old.html'); // load the view file (angular will handle the page changes on the front-end) - }); - // application ------------------------------------------------------------- app.get('*', function(req, res) { res.sendfile('./public/index.html'); // load the view file (angular will handle the page changes on the front-end)