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

test #32

Open
wants to merge 23 commits into
base: tut1-starter
Choose a base branch
from
Open

test #32

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
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ Node provides the RESTful API. Angular provides the frontend and accesses the AP
## Requirements

- [Node and npm](http://nodejs.org)
- MongoDB: Make sure you have your own local or remote MongoDB database URI configured in `config/database.js`

## Installation

1. Clone the repository: `git clone [email protected]:scotch-io/node-todo`
2. Install the application: `npm install`
3. Place your own MongoDB URI in `config/database.js`
3. Start the server: `node server.js`
4. View in browser at `http://localhost:8080`

Expand All @@ -21,11 +23,8 @@ This repo corresponds to the Node Todo Tutorial Series on [scotch.io](http://sco

Each branch represents a certain tutorial.
- tut1-starter: [Creating a Single Page Todo App with Node and Angular](http://scotch.io/tutorials/javascript/creating-a-single-page-todo-app-with-node-and-angular)
- tut2-services: Coming Soon
- tut3-auth: Coming Soon
- tut4-sockets: Coming Soon
- tut5-redis: Coming Soon
- tut6-organization: Coming Soon
- tut2-organization: [Application Organization and Structure](https://scotch.io/tutorials/node-and-angular-to-do-app-application-organization-and-structure)
- tut3-services: [Controllers and Services](https://scotch.io/tutorials/node-and-angular-to-do-app-controllers-and-services)

Happy Todo-ing!

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

module.exports = mongoose.model('Todo', {
text : String,
done : Boolean
text: {
type: String,
default: ''
}
});
115 changes: 55 additions & 60 deletions app/routes.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,57 @@
var Todo = require('./models/todo');

module.exports = function(app) {

// api ---------------------------------------------------------------------
// get all todos
app.get('/api/todos', function(req, res) {

// use mongoose to get all todos in the database
Todo.find(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
});
});

// create todo and send back all todos after creation
app.post('/api/todos', function(req, res) {

// create a todo, information comes from AJAX request from Angular
Todo.create({
text : req.body.text,
done : false
}, function(err, todo) {
if (err)
res.send(err);

// get and return all the todos after you create another
Todo.find(function(err, todos) {
if (err)
res.send(err)
res.json(todos);
});
});

});

// delete a 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);

// get and return all the todos after you create another
Todo.find(function(err, todos) {
if (err)
res.send(err)
res.json(todos);
});
});
});

// 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)
});
};
function getTodos(res) {
Todo.find(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 ---------------------------------------------------------------------
// get all todos
app.get('/api/todos', function (req, res) {
// use mongoose to get all todos in the database
getTodos(res);
});

// create todo and send back all todos after creation
app.post('/api/todos', function (req, res) {

// create a todo, information comes from AJAX request from Angular
Todo.create({
text: req.body.text,
done: false
}, function (err, todo) {
if (err)
res.send(err);

// get and return all the todos after you create another
getTodos(res);
});

});

// delete a 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);
});
});

// application -------------------------------------------------------------
app.get('*', function (req, res) {
res.sendFile(__dirname + '/public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});
};
7 changes: 3 additions & 4 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {

// the database url to connect
url : 'mongodb://node:[email protected]:27017/uwO3mypu'
}
remoteUrl : 'mongodb://node:[email protected]:27017/uwO3mypu',
localUrl: 'mongodb://localhost/meanstacktutorials'
};
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) {{{year}}} {{{fullname}}}

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name" : "node-todo",
"version" : "0.0.0",
"description" : "Simple todo application.",
"main" : "server.js",
"author" : "Scotch",
"dependencies" : {
"express" : "~3.4.4",
"mongoose" : "~3.6.2"
"name": "node-todo",
"version": "0.0.1",
"description": "Simple todo application.",
"main": "server.js",
"author": "Scotch",
"dependencies": {
"body-parser": "^1.4.3",
"express": "^4.13.4",
"method-override": "^2.1.3",
"mongoose": "^4.4.12",
"morgan": "^1.1.1"
}
}
38 changes: 0 additions & 38 deletions public/core.js

This file was deleted.

16 changes: 13 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<!-- 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; }
Expand All @@ -20,10 +21,13 @@

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

<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 AND GET ALL TODOS WITH INITIALIZE FUNCTION -->
<!-- SET THE CONTROLLER -->
<body ng-controller="mainController">
<div class="container">

Expand All @@ -36,13 +40,19 @@ <h1>I'm a Todo-aholic <span class="label label-info">{{ todos.length }}</span></
<div id="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>

Expand Down Expand Up @@ -70,4 +80,4 @@ <h1>I'm a Todo-aholic <span class="label label-info">{{ todos.length }}</span></
</div>

</body>
</html>
</html>
50 changes: 50 additions & 0 deletions public/js/controllers/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
angular.module('todoController', [])

// inject the Todo service factory into our controller
.controller('mainController', ['$scope','$http','Todos', function($scope, $http, Todos) {
$scope.formData = {};
$scope.loading = true;

// GET =====================================================================
// when landing on the page, get all todos and show them
// use the service to get all the todos
Todos.get()
.success(function(data) {
$scope.todos = data;
$scope.loading = false;
});

// CREATE ==================================================================
// when submitting the add form, send the text to the node API
$scope.createTodo = function() {

// validate the formData to make sure that something is there
// if form is empty, nothing will happen
if ($scope.formData.text != undefined) {
$scope.loading = true;

// call the create function from our service (returns a promise object)
Todos.create($scope.formData)

// if successful creation, call our get function to get all the new todos
.success(function(data) {
$scope.loading = false;
$scope.formData = {}; // clear the form so our user is ready to enter another
$scope.todos = data; // assign our new list of todos
});
}
};

// DELETE ==================================================================
// delete a todo after checking it
$scope.deleteTodo = function(id) {
$scope.loading = true;

Todos.delete(id)
// if successful creation, call our get function to get all the new todos
.success(function(data) {
$scope.loading = false;
$scope.todos = data; // assign our new list of todos
});
};
}]);
1 change: 1 addition & 0 deletions public/js/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
angular.module('scotchTodo', ['todoController', 'todoService']);
17 changes: 17 additions & 0 deletions public/js/services/todos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
angular.module('todoService', [])

// super simple service
// each function returns a promise object
.factory('Todos', ['$http',function($http) {
return {
get : function() {
return $http.get('/api/todos');
},
create : function(todoData) {
return $http.post('/api/todos', todoData);
},
delete : function(id) {
return $http.delete('/api/todos/' + id);
}
}
}]);
Loading