forked from generalgmt/RESTfulAPITutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7e9c007
Showing
6 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
logs | ||
*.log | ||
npm-debug.log* | ||
node_modules | ||
.npm | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
var mongoose = require('mongoose'), | ||
Task = mongoose.model('Tasks'); | ||
|
||
|
||
|
||
exports.list_all_tasks = function(req, res) { | ||
Task.find({}, function(err, task) { | ||
if (err) | ||
res.send(err); | ||
res.json(task); | ||
}); | ||
}; | ||
|
||
|
||
exports.create_a_task = function(req, res) { | ||
var new_task = new Task(req.body); | ||
new_task.save(function(err, task) { | ||
if (err) | ||
res.send(err); | ||
res.json(task); | ||
}); | ||
}; | ||
|
||
exports.read_a_task = function(req, res) { | ||
Task.findById(req.params.taskId, function(err, task) { | ||
if (err) | ||
res.send(err); | ||
res.json(task); | ||
}); | ||
}; | ||
|
||
exports.update_a_task = function(req, res) { | ||
Task.findOneAndUpdate(req.params.taskId, req.body, {new: true}, function(err, task) { | ||
if (err) | ||
res.send(err); | ||
res.json(task); | ||
}); | ||
}; | ||
// Task.remove({}).exec(function(){}); | ||
exports.delete_a_task = function(req, res) { | ||
|
||
Task.remove({ | ||
_id: req.params.taskId | ||
}, function(err, task) { | ||
if (err) | ||
res.send(err); | ||
res.json({ message: 'Task successfully deleted' }); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
|
||
var mongoose = require('mongoose'); | ||
var Schema = mongoose.Schema; | ||
|
||
var TaskSchema = new Schema({ | ||
name: { | ||
type: String, | ||
Required: 'Kindly enter the name of the task' | ||
}, | ||
Created_date: { | ||
type: Date, | ||
default: Date.now | ||
}, | ||
status: { | ||
type: [{ | ||
type: String, | ||
enum: ['pending', 'ongoing', 'completed'] | ||
}], | ||
default: ['pending'] | ||
} | ||
}); | ||
|
||
|
||
module.exports = mongoose.model('Tasks', TaskSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
module.exports = function(app) { | ||
var todoList = require('../controllers/todoListController'); | ||
|
||
// todoList Routes | ||
app.route('/task') | ||
.get(todoList.list_all_tasks) | ||
.post(todoList.create_a_task); | ||
|
||
app.route('/task/:taskId') | ||
.get(todoList.read_a_task) | ||
.put(todoList.update_a_task) | ||
.delete(todoList.delete_a_task); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "todolistapi", | ||
"version": "1.0.0", | ||
"description": "RESTful todoListApi", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "nodemon server.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/generalgmt/RESTfulAPITutorial.git" | ||
}, | ||
"keywords": [ | ||
"RESTful", | ||
"API", | ||
"Tutorial" | ||
], | ||
"author": "olatunde garuba", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/generalgmt/RESTfulAPITutorial/issues" | ||
}, | ||
"homepage": "https://github.com/generalgmt/RESTfulAPITutorial#readme", | ||
"devDependencies": { | ||
"nodemon": "^1.11.0" | ||
}, | ||
"dependencies": { | ||
"body-parser": "^1.15.2", | ||
"express": "^4.14.0", | ||
"mongoose": "^4.7.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var express = require('express'), | ||
app = express(), | ||
port = process.env.PORT || 3000, | ||
mongoose = require('mongoose'), | ||
Task = require('./api/models/todoListModel'), | ||
bodyParser = require('body-parser'); | ||
|
||
mongoose.Promise = global.Promise; | ||
mongoose.connect('mongodb://localhost/Tododb'); | ||
|
||
|
||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(bodyParser.json()); | ||
|
||
|
||
var routes = require('./api/routes/todoListRoutes'); | ||
routes(app); | ||
|
||
app.use(function(req, res) { | ||
res.status(404).send({url: req.originalUrl + ' not found'}) | ||
}); | ||
|
||
app.listen(port); | ||
|
||
console.log('todo list RESTful API server started on: ' + port); |