Skip to content

Commit fa239ac

Browse files
Merge pull request #11 from renanlopescoder/add_users_crud
add_users_crud_to_api
2 parents 37f7a6f + 281f5fa commit fa239ac

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

app/api/user.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,53 @@ var api = {};
33

44
var model = mongoose.model('User');
55

6+
api.list = function (req, res){
7+
model.find({},function(error, list){
8+
if(error){
9+
res.status(500).json(error);
10+
}
11+
res.json(list);
12+
});
13+
14+
};
15+
16+
api.create = function(req, res){
17+
model
18+
.create(req.body).then(function(dados){
19+
res.json(dados);
20+
}, function(error){
21+
console.log(error);
22+
res.status(404).json(error);
23+
});
24+
25+
};
26+
27+
api.searchById = function(req,res){
28+
29+
model
30+
.findById(req.params.id)
31+
.then(function(id){
32+
33+
res.json(id);
34+
35+
}, function(error){
36+
console.log(error);
37+
res.status(404).json(error);
38+
});
39+
40+
};
41+
42+
api.deleteById = function(req,res){
43+
model.remove({_id: req.params.id})
44+
.then(function(){
45+
res.sendStatus(200);
46+
}, function(error){
47+
console.log(error);
48+
res.status(404).json (error);
49+
});
50+
51+
};
52+
653
api.update = function(req,res){
754
console.log(req.body);
855
model
@@ -15,9 +62,7 @@ api.update = function(req,res){
1562
console.log(error);
1663
res.status(404).json(error);
1764
});
18-
};
19-
20-
2165

66+
};
2267

2368
module.exports = api;

app/routes/user.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
module.exports = function (app){
2-
2+
33
var api = app.app.api.user;
44

5-
app.put('/update/user/:id', api.update);
5+
app.get('/list/users', api.list);
6+
7+
app.post('/create/users', api.create);
8+
9+
app.put('/update/users/:id', api.update);
10+
11+
app.get('/select/users/:id', api.searchById);
12+
13+
app.delete('/delete/users/:id', api.deleteById);
614
};

0 commit comments

Comments
 (0)