-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
176 lines (159 loc) · 4.3 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
require("./config/config");
const _ = require('lodash');
const bodyParser = require('body-parser');
const express = require('express');
const {ObjectID} = require('mongodb');
const {mongoose} = require('./db/mongoose');
const {Todo} = require('./models/todo');
const {User} = require('./models/user');
const {authenticate} = require("./middleware/authenticate");
let app = express();
const port = process.env.PORT;
console.log(port);
app.use(bodyParser.json());
//adding todo
app.post('/todos', authenticate, (req, res)=>{
let todo1 = new Todo({
text: req.body.text,
_creator: req.user._id //associating todo with its user
});
todo1.save().then((result)=>{
res.send("successfully added data to table\n"+result);
},(error)=>{
res.status(400).send(error);
});
});
//list of all todos
app.get('/todos', authenticate, (req, res)=>{
Todo.find({
_creator: req.user._id //to ensure the privacy of user
}).then((doc)=>{
res.send(doc);
},(err)=>{
res.status(400).send(err);
})
});
//finding by id ....route
app.get('/todos/:id', authenticate,(req, res)=>{
var id = req.params.id;
if(!ObjectID.isValid(id)){
return res.status(404).send("Not a valid id");
};
Todo.findOne({
_id: id,
_creator: req.user._id //finding only the associated todo
}).then((todo)=>{
if(todo == null)
res.status(404).send("No data with this id");
else
res.send("-------\n"+todo);
},(error)=>{
res.send(error);
});
});
//deleting by id ....route
app.delete('/todos/:id', authenticate, (req, res)=>{
let id = req.params.id;
if(!ObjectID.isValid(id)){
res.status(404).send("Not a valid id: "+id);
}
else{
Todo.findOneAndRemove({
_id: id,
_creator: req.user._id //again checking for only user's todo
}).then((todo)=>{
if(todo === null){
res.status(404).send("Cant find the todo");
}
else{
res.send("todo deleted :\n"+todo);
}
}).catch((e)=>{
res.status(400).send("error : "+ e);
})
}
});
//reset route
app.get('/reset',(req, res)=>{
Todo.remove({}).then((doc)=>{
console.log(doc);
console.log("\n---------------------------\n");
});
User.remove({}).then((doc)=>{
console.log(doc);
});
res.send("Data has been reset");
});
//updating //authentication added
app.patch('/todos/:id', authenticate, (req, res)=>{
let id = req.params.id;
let body = _.pick(req.body,["text","completed"]);
if(!ObjectID.isValid(id)){
return res.status(404).send("Not a valid id: "+id);
}
if(_.isBoolean(body.completed) && body.completed)
{
body.completedat = new Date().getTime();
}
else{
body.completed = false;
body.completedat = null;
}
// Todo.findByIdAndUpdate(id,{$set:{"text":"hey"}},{new : true}).then((res)=>{
// res.send(res);
// })
Todo.findOneAndUpdate({
_id: id,
_creator: req.user._id
}, {$set : body },{new : true}).then((todo)=>{
if(!todo)
{
return res.status(404).send("Cant update.");
}
res.send(todo);
}).catch((e)=>{
res.status(400).send("error: "+e);
})
});
//adding User
app.post('/users',(req, res)=>{
let body = _.pick(req.body,['email','password']);
let user = new User(body);
user.save().then(()=>{
return user.generateAuthToken();
// return res.send(user);
}).then((token)=>{
return res.header('x-auth',token).send(user);
}).catch((e)=>{
return res.status(400).send('Server : User with this email is already in db.\n\n' + e);
})
});
//auth test
app.get('/users/me', authenticate, (req, res)=>{
res.send(req.user);
})
//Login User
app.post('/users/login',(req, res)=>{
var body = _.pick(req.body,['email','password']);
let email = body.email;
let password = body.password;
User.findByCredentials(email, password).then((user)=>{
console.log("--");
user.generateAuthToken().then((token)=>{
res.header('x-auth',token).send(user);
});
}).catch((e)=>{
res.status(400).send("Incorrect Password. "+e);
});
});
//Log Out // removing token from db
app.delete("/users/me/token", authenticate, (req, res)=>{
req.user.removeToken(req.token).then(()=>{
res.status(200).send("Token removed");
},(reject)=>{
res.status(400).send(reject);
})
});
app.listen(port,()=>{
console.log(`Connected to the Server ${port}`);
});