Skip to content

Commit

Permalink
added done functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
collinalexbell committed Oct 15, 2015
1 parent 5f31fd7 commit ff804ba
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 18 deletions.
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},

});
55 changes: 43 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,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)
});
};

};
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);
}

}
}]);
}]);
83 changes: 83 additions & 0 deletions public/old.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!doctype html>

<!-- ASSIGN OUR ANGULAR MODULE -->
<html ng-app="scotchTodo">
<head>
<!-- META -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"><!-- Optimize mobile viewport -->

<title>Node/Angular Todo App</title>

<!-- SCROLLS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"><!-- load bootstrap -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css">
<style>
html { overflow-y:scroll; }
body { padding-top:50px; }
#todo-list { margin-bottom:30px; }
#todo-form { margin-bottom:50px; }
</style>

<!-- SPELLS -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script><!-- load angular -->

<script src="js/controllers/main.js"></script> <!-- load up our controller -->
<script src="js/services/todos.js"></script> <!-- load our todo service -->
<script src="js/core.js"></script> <!-- load our main application -->

</head>
<!-- SET THE CONTROLLER -->
<body ng-controller="mainController">
<div class="container">

<!-- HEADER AND TODO COUNT -->
<div class="jumbotron text-center">
<h1>I'm a Todo-aholic <span class="label label-info">{{ todos.length }}</span></h1>
</div>

<!-- TODO LIST -->
<div id="old-todo-list" class="row">
<div class="col-sm-4 col-sm-offset-4">



<!-- LOOP OVER THE TODOS IN $scope.todos -->
<div class="checkbox" ng-repeat="todo in todos">
<label>
<input type="checkbox" ng-click="deleteTodo(todo._id)"> {{ todo.text }}
</label>
</div>

<p class="text-center" ng-show="loading">
<span class="fa fa-spinner fa-spin fa-3x"></span>
</p>

</div>
</div>

<!-- FORM TO CREATE TODOS -->
<div id="todo-form" class="row">
<div class="col-sm-8 col-sm-offset-2 text-center">
<form>
<div class="form-group">

<!-- BIND THIS VALUE TO formData.text IN ANGULAR -->
<input type="text" class="form-control input-lg text-center" placeholder="I want to buy a puppy that will love me forever" ng-model="formData.text">
</div>

<!-- createToDo() WILL CREATE NEW TODOS -->
<button type="submit" class="btn btn-primary btn-lg" ng-click="createTodo()">Add</button>
</form>
</div>
</div>

<div class="text-center text-muted">
<p>A demo by <a href="http://scotch.io">Scotch</a>.</p>
<p>Read the <a href="http://scotch.io/tutorials/javascript/creating-a-single-page-todo-app-with-node-and-angular">tutorial</a>.</p>
</div>

</div>

</body>
</html>
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

0 comments on commit ff804ba

Please sign in to comment.