From f383a85f8ed569821ea0e929139dcd68c5541519 Mon Sep 17 00:00:00 2001 From: Kumar Damani Date: Wed, 3 Aug 2016 14:30:06 -0400 Subject: all files --- README.md | 14 +++++++ app/models/todo.js | 8 ++++ app/routes.js | 56 +++++++++++++++++++++++++ package.json | 14 +++++++ public/css/style.css | 7 ++++ public/index.html | 72 +++++++++++++++++++++++++++++++++ public/js/app.js | 1 + public/js/controllers/todoController.js | 44 ++++++++++++++++++++ public/js/services/todoService.js | 16 ++++++++ server.js | 30 ++++++++++++++ 10 files changed, 262 insertions(+) create mode 100644 README.md create mode 100644 app/models/todo.js create mode 100644 app/routes.js create mode 100644 package.json create mode 100644 public/css/style.css create mode 100644 public/index.html create mode 100644 public/js/app.js create mode 100644 public/js/controllers/todoController.js create mode 100644 public/js/services/todoService.js create mode 100644 server.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..c0f3ccd --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +## Requirements + +- [Node and npm](http://nodejs.org) + +## Installation + +1. Clone the repository: `git clone git@github.com: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` + + + diff --git a/app/models/todo.js b/app/models/todo.js new file mode 100644 index 0000000..02f4088 --- /dev/null +++ b/app/models/todo.js @@ -0,0 +1,8 @@ +var mongoose = require('mongoose'); + +module.exports = mongoose.model('todoCollect', { + text: { + type: String, + default: '' + } +}); \ No newline at end of file diff --git a/app/routes.js b/app/routes.js new file mode 100644 index 0000000..7b596dc --- /dev/null +++ b/app/routes.js @@ -0,0 +1,56 @@ +// get the todos collection +var todoCollect = require('./models/todo'); + + +// use mongo to get all the items in db +function getTodos (res) { + todoCollect.find(function (err, todos) { + if (err) res.send(err); + + res.json(todos); // return all todos in JSON format + // console.log(todos); + }); +} + + +// ===========================API=============================== +module.exports = function (app) { + + // get all items + app.get('/api/todos', function (req, res) { + getTodos(res); + }); + + // create an item + app.post('/api/todos', function (req, res) { + + // add item to the list + todoCollect.create({ + text: req.body.text, + done: false + }, function (err, todo) { + if (err) res.send(err); + + // 'refresh' to-do list + getTodos(res); + }); + + }); + + // delete item + app.delete('/api/todos/:todo_id', function (req, res) { + todoCollect.remove({ + _id: req.params.todo_id + }, function (err, todo) { + if (err) res.send(err); + + // 'refresh' to-do list + getTodos(res); + }); + }); + + // load the static html file for now + app.get('*', function (req, res) { + res.sendFile(__dirname + '/public/index.html'); + }); +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..29b783b --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "One MEAN To-do List", + "version": "0.0.1", + "description": "a to-do List.", + "main": "server.js", + "author": "Kumar Damani", + "dependencies": { + "body-parser": "^1.4.3", + "express": "^4.13.4", + "method-override": "^2.1.3", + "mongoose": "^4.4.12", + "morgan": "^1.1.1" + } +} diff --git a/public/css/style.css b/public/css/style.css new file mode 100644 index 0000000..31b3391 --- /dev/null +++ b/public/css/style.css @@ -0,0 +1,7 @@ +body { + padding-top:50px; +} + +#todo-list { + margin-bottom:30px; +} diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..61548a5 --- /dev/null +++ b/public/index.html @@ -0,0 +1,72 @@ + + + + + + + + A Simple To-do List + + + + + + + + + + + + + + + + + +
+ + +
+

You have {{ todos.length }} to-dos

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

+ +

+ +
+
+ + +
+
+
+
+ + + +
+ + + +
+
+
+ +
+ + + diff --git a/public/js/app.js b/public/js/app.js new file mode 100644 index 0000000..01ec9be --- /dev/null +++ b/public/js/app.js @@ -0,0 +1 @@ +angular.module('toDoList', ['todoController', 'todoService']); diff --git a/public/js/controllers/todoController.js b/public/js/controllers/todoController.js new file mode 100644 index 0000000..eb73feb --- /dev/null +++ b/public/js/controllers/todoController.js @@ -0,0 +1,44 @@ +angular.module('todoController', []) + + // inject service factory into our controller + .controller('mainController', ['$scope','$http','Todos', function($scope, $http, Todos) { + $scope.formData = {}; + $scope.loading = true; + + // on first visit 'refresh' the todo list + Todos.get() + .success(function(data) { + $scope.todos = data; + $scope.loading = false; + }); + + // when submitting the add form, send formdata to api + $scope.createTodo = function() { + + // validate the formData to make sure that something is there + if ($scope.formData.text != undefined) { + $scope.loading = true; + + Todos.create($scope.formData) + + // if successful creation, 'refresh' todo list + .success(function(data) { + $scope.loading = false; + $scope.formData = ""; // clear the form + $scope.todos = data; // assign our new list of todo + }); + } + }; + + // delete a todo when pressed checkbox + $scope.deleteTodo = function(id) { + $scope.loading = true; + + Todos.delete(id) + // if successful, 'refresh' the list with latest data + .success(function(data) { + $scope.loading = false; + $scope.todos = data; + }); + }; + }]); \ No newline at end of file diff --git a/public/js/services/todoService.js b/public/js/services/todoService.js new file mode 100644 index 0000000..649c61f --- /dev/null +++ b/public/js/services/todoService.js @@ -0,0 +1,16 @@ +angular.module('todoService', []) + + // use the api + .factory('Todos', ['$http', function($http) { + return { + get : function() { + return $http.get('/api/todos'); + }, + create : function(data) { + return $http.post('/api/todos', data); + }, + delete : function(id) { + return $http.delete('/api/todos/' + id); + } + } + }]); \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 0000000..18ea468 --- /dev/null +++ b/server.js @@ -0,0 +1,30 @@ +// set up the app +var express = require('express'); +var app = express(); +var mongoose = require('mongoose'); // for mongodb +var port = process.env.PORT || 3000; // the port +// var database = require('./config/database'); // load the database configuration (uri) +// var morgan = require('morgan'); // for debugging +var bodyParser = require('body-parser'); // for ease of use +var methodOverride = require('method-override'); // just in case + +// set up for functionality +mongoose.connect('mongodb://test:admin@ds139665.mlab.com:39665/to_list'); // connect to mongodb +// mongoose.connect('mongodb://localhost/2701'); // connect to mongodb + +app.use(express.static('./public')); // set the static files location /public/img will be /img for users +// app.use(morgan('dev')); // log every request to the console +app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application ...url +app.use(bodyParser.json()); // parse application/json +app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json +app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request + + +// connect with api +require('./app/routes.js')(app); + +// start the app +app.listen(port); +console.log("Listening on port " + port); + + -- cgit v1.2.3