Merge c829cf58ef
into 3d44de3ba8
This commit is contained in:
commit
05bf7ba6e7
7 changed files with 132 additions and 8 deletions
57
api/controllers/todoListController.js
Normal file
57
api/controllers/todoListController.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
'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);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
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' });
|
||||
});
|
||||
};
|
||||
|
27
api/models/todoListModel.js
Normal file
27
api/models/todoListModel.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
|
||||
|
||||
'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);
|
||||
|
23
api/routes/todoListRoutes.js
Normal file
23
api/routes/todoListRoutes.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
'use strict';
|
||||
module.exports = function(app) {
|
||||
var todoList = require('../controllers/todoListController');
|
||||
|
||||
app.route('/api/test')
|
||||
.get((req,res)=>{ res.send('testing here'); });
|
||||
/*
|
||||
app.get('/tasks',(req, res) => {
|
||||
res.send('Tasks is here')
|
||||
});
|
||||
*/
|
||||
// todoList Routes
|
||||
app.route('/tasks')
|
||||
.get(todoList.list_all_tasks)
|
||||
.post(todoList.create_a_task);
|
||||
|
||||
|
||||
app.route('/tasks/:taskId')
|
||||
.get(todoList.read_a_task)
|
||||
.put(todoList.update_a_task)
|
||||
.delete(todoList.delete_a_task);
|
||||
|
||||
};
|
|
@ -10,6 +10,8 @@
|
|||
"express": "^4.13.4",
|
||||
"mocha": "^2.4.5",
|
||||
"mongodb": "^2.1.16",
|
||||
"mongoose": "*",
|
||||
"body-parser": "*",
|
||||
"morgan": "^1.7.0",
|
||||
"object-assign":"4.1.0"
|
||||
},
|
||||
|
|
25
server.js
25
server.js
|
@ -3,13 +3,17 @@ var express = require('express'),
|
|||
fs = require('fs'),
|
||||
app = express(),
|
||||
eps = require('ejs'),
|
||||
morgan = require('morgan');
|
||||
morgan = require('morgan'),
|
||||
mongoose = require('mongoose'),
|
||||
Task = require('./api/models/todoListModel'),
|
||||
bodyParser = require('body-parser');
|
||||
|
||||
Object.assign=require('object-assign')
|
||||
|
||||
app.engine('html', require('ejs').renderFile);
|
||||
app.use(morgan('combined'))
|
||||
|
||||
|
||||
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
|
||||
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0',
|
||||
mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL,
|
||||
|
@ -34,8 +38,18 @@ if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) {
|
|||
|
||||
}
|
||||
}
|
||||
var db = null,
|
||||
dbDetails = new Object();
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.connect(mongoURL);
|
||||
|
||||
app.use(bodyParser.urlencoded({ extended: true}));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
var routes = require('./api/routes/todoListRoutes');
|
||||
routes(app);
|
||||
|
||||
/*
|
||||
var db = null, dbDetails = new Object();
|
||||
|
||||
var initDb = function(callback) {
|
||||
if (mongoURL == null) return;
|
||||
|
@ -84,13 +98,13 @@ app.get('/pagecount', function (req, res) {
|
|||
}
|
||||
if (db) {
|
||||
db.collection('counts').count(function(err, count ){
|
||||
res.send('{ pageCount: ' + count + '}');
|
||||
res.send('{ pageCount: == ' + count + '}');
|
||||
});
|
||||
} else {
|
||||
res.send('{ pageCount: -1 }');
|
||||
}
|
||||
});
|
||||
|
||||
*/
|
||||
// error handling
|
||||
app.use(function(err, req, res, next){
|
||||
console.error(err.stack);
|
||||
|
@ -103,5 +117,4 @@ initDb(function(err){
|
|||
|
||||
app.listen(port, ip);
|
||||
console.log('Server running on http://%s:%s', ip, port);
|
||||
|
||||
module.exports = app ;
|
||||
|
|
|
@ -8,7 +8,8 @@ chai.use(chaiHTTP);
|
|||
reqServer = process.env.HTTP_TEST_SERVER || server
|
||||
|
||||
describe('Basic routes tests', function() {
|
||||
|
||||
//do nothing!
|
||||
/*
|
||||
it('GET to / should return 200', function(done){
|
||||
chai.request(reqServer)
|
||||
.get('/')
|
||||
|
@ -28,4 +29,5 @@ describe('Basic routes tests', function() {
|
|||
})
|
||||
|
||||
})
|
||||
*/
|
||||
})
|
||||
|
|
|
@ -216,7 +216,7 @@ pre {
|
|||
|
||||
<section class='container'>
|
||||
<hgroup>
|
||||
<h1>Welcome to your Node.js application on OpenShift</h1>
|
||||
<h1>This is my Node.js application on OpenShift</h1>
|
||||
</hgroup>
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue