Skip to content

Commit 20a5e9e

Browse files
committed
Initial commit
0 parents  commit 20a5e9e

File tree

7 files changed

+3260
-0
lines changed

7 files changed

+3260
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

api/controllers/tasksController.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
var mongoose = require('mongoose'),
3+
Task = mongoose.model('Tasks');
4+
5+
6+
// Retrieve all the tasks saved in the database
7+
exports.getAllTasks = function(req, res) {
8+
Task.find({}, function(err, task) {
9+
if (err) {
10+
res.status(400).send(err);
11+
} else {
12+
res.json(task);
13+
}
14+
});
15+
};
16+
17+
// Create a new task
18+
exports.createTask = function(req, res) {
19+
var new_task = new Task(req.body);
20+
new_task.save(function(err, task) {
21+
if (err) {
22+
res.status(400).send(err);
23+
} else {
24+
res.status(201).json(task);
25+
}
26+
});
27+
};
28+
29+
// Retrieve a task by taskId
30+
exports.getTaskById = function(req, res) {
31+
Task.findById(req.params.taskId, function(err, task) {
32+
if (err) {
33+
res.status(404).send({ error: { errors: [ { domain: 'global', reason: 'notFound', message: 'Not Found',
34+
description: 'Couldn\'t find the requested taskId \'' + req.params.taskId + '\'' } ], err, code: 404 } })
35+
} else {
36+
res.json(task);
37+
}
38+
});
39+
};
40+
41+
// Edit a task by taskId
42+
exports.editTaskById = function(req, res) {
43+
Task.findOneAndUpdate({_id: req.params.taskId}, req.body, {new: true}, function(err, task) {
44+
if (err) {
45+
res.status(400).send(err);
46+
} else {
47+
res.json(task);
48+
}
49+
});
50+
};
51+
52+
// Delete a task by taskId
53+
exports.deleteTaskById = function(req, res) {
54+
Task.remove({
55+
_id: req.params.taskId
56+
}, function(err, task) {
57+
if (err) {
58+
res.status(404).send({ error: { errors: [ { domain: 'global', reason: 'notFound', message: 'Not Found',
59+
description: 'Couldn\'t find the requested taskId \'' + req.params.taskId + '\'' } ], code: 404, message: 'Not Found' } })
60+
} else {
61+
res.status(204).send();
62+
//res.json({ message: 'Task successfully deleted' });
63+
}
64+
});
65+
};

api/models/tasksModel.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
var mongoose = require('mongoose');
3+
var Schema = mongoose.Schema;
4+
5+
6+
// JSON schema for
7+
var TaskSchema = new Schema({
8+
name: {
9+
type: String,
10+
required: 'Kindly enter the name of the task'
11+
},
12+
category: {
13+
type: String,
14+
required: 'Kindly enter the category of the task'
15+
},
16+
createdDate: {
17+
type: Date,
18+
default: Date.now
19+
},
20+
status: {
21+
type: [{
22+
type: String,
23+
enum: ['Pending', 'Ongoing', 'Completed']
24+
}],
25+
default: ['Pending']
26+
}
27+
});
28+
29+
module.exports = mongoose.model('Tasks', TaskSchema);

api/routes/tasksRoutes.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
var tasksList = require('../controllers/tasksController');
3+
4+
5+
module.exports = function(app) {
6+
// Tasks List Routes
7+
app.route('/tasks')
8+
.get(tasksList.getAllTasks)
9+
.post(tasksList.createTask);
10+
11+
app.route('/tasks/:taskId')
12+
.get(tasksList.getTaskById)
13+
.put(tasksList.editTaskById)
14+
.delete(tasksList.deleteTaskById);
15+
};

0 commit comments

Comments
 (0)