forked from bhupendra-negi/todo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
178 lines (150 loc) · 4.66 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// basic server file
var express = require("express");
//body parser module
var bodyParser = require("body-parser");
//underscore module
var _ = require("underscore");
var app = express();
var PORT = process.env.PORT || 3000;
// attach middleware to express module so that it parses everyting we get POST data
app.use(bodyParser.json());
//create collection of models for data purpose
// below data would br craeted by POST method
/*var todos = [{
id: 1,
description: "Go for haircut and trimming",
completed: false
}, {
id: 2,
description: "wash scooty",
completed: false
}, {
id: 3,
description: "Go to gym",
completed: true
}];
*/
var todos = [];
var todoNextId = 1;
// POST method to add data
app.post('/todos', function(req, res) {
var body = req.body;
//check for only two data fields provided
var filteredObj = _.pick(body, "description", "completed");
// check for validations for todoNextId
if (!filteredObj || body.description.trim().length === 0 || !_.isBoolean(body.completed) || !_.isString(body.description)) {
return res.status(400).send("Invalid data submitted");
}
// trim body description
body.description = body.description.trim();
body.id = todoNextId;
todos.push(body);
todoNextId++;
res.json("post data pushed");
});
// below method is for deleting any record
app.delete("/todos/:id", function(req, res) {
var todoId = parseInt(req.params.id,10);
var matchedTodo = _.findWhere(todos,{id:todoId});
if (matchedTodo) {
//delete record from array
todos = _.without(todos,matchedTodo);
res.send("Task deleted for ID :" + todoId);
} else {
res.status(404).send("This record does not exits");
}
});
// different request for todos task listen
// search query parameters
app.get('/todos', function(req, res) {
// return json data to request
// filter the request bsaed upon query params
var queryParams = req.query;
var filteredtodos = todos;
if (queryParams.hasOwnProperty("completed") && queryParams.completed === "true")
{
filteredtodos = _.where(todos,{completed:true});
}
else if ( queryParams.hasOwnProperty("completed") && queryParams.completed === "false" )
{
filteredtodos = _.where(todos,{completed:false});
}
else if (queryParams.hasOwnProperty("completed")) {
return res.status(400).send("wrong params to be updated");
}
if ( queryParams.hasOwnProperty("q") && queryParams.q.trim().length > 0)
{
filteredtodos = _.filter(filteredtodos, function(todo){
return todo.description.toLowerCase().indexOf(queryParams.q.toLowerCase()) > -1 ;
});
}
res.json(filteredtodos);
});
// delete request
app.delete("/todos/:id", function(req, res) {
var todoId = parseInt(req.params.id, 10);
//res.send("You are asking for Item : " + todoId );
var obj = _.findWhere(todos, {
id: todoId
})
if (obj) {
todos = _.without(todos, obj);
res.send("record removed for id :" + todoId);
} else {
res.status(404).json({
"error": "such record not exist"
})
}
});
//update request PUT
app.put("/todos/:id", function(req, res) {
var todoId = parseInt(req.params.id, 10);
var obj = _.findWhere(todos, {
id: todoId
});
if (obj) {
var validAttributes = {}
//check for only two data fields provided
var body = _.pick(req.body, "description", "completed");
if (body.hasOwnProperty("completed") && _.isBoolean(body.completed)) {
validAttributes.completed = body.completed;
} else if (body.hasOwnProperty("completed")) return res.status(400).send("data not in correct format")
if (body.hasOwnProperty("description") && _isString(body.description) && body.description.trim().length > 0) {
validAttributes.description = body.description;
} else if (body.hasOwnProperty("description")) return res.status(400).send("data not in correct format");
// here valid attributes are there
_.extend(obj, validAttributes);
res.send("updated record");
} else {
return res.status(404).json({
"error": "record does not exist"
});
}
});
// GET todos/:id
app.get("/todos/:id", function(req, res) {
var todoId = parseInt(req.params.id, 10);
//res.send("You are asking for Item : " + todoId );
var obj = _.findWhere(todos, {
id: todoId
})
if (obj) {
res.json(obj);
} else {
res.status(404).send("to do task with given ID not found")
}
/*var flag = 0;
for(var i = 0;i<todos.length;i++)
{
var obj = todos[i];
if (obj.id === todoId) { res.json(obj);flag=1}
};
if (!flag) res.status(404).send("to do task not found"); //s end 404 is not in list
*/
})
app.get('/', function(req, res) {
res.send("This is TO DO App ROOT");
});
app.listen(PORT, function() {
console.log("node server started on port :" + PORT);
})