forked from sf-wdi-25/express_self_api
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathbirdsController.js
More file actions
79 lines (68 loc) · 1.59 KB
/
birdsController.js
File metadata and controls
79 lines (68 loc) · 1.59 KB
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
//DB
const db = require("../models");
//GET /api/birds -- get all birds
function index(req, res) {
db.Bird.find({}, function(err, allBirds) {
if (err) {
console.log("Failed /api/birds ");
return;
}
res.json(allBirds);
})
}
//POST /api/birds -- adding a bird
function create(req, res) {
db.Bird.create(req.body, function(err, bird) {
console.log('entering db.Bird.create()');
if (err) {
console.log(`create bird failed: err =${err}`);
return;
}
console.log("create bird success");
res.json(bird);
});
}
function update(req, res) {
db.Bird.findById(req.params.birdId, function(err, bird){
if (err) {
console.log(`Did not find bird id: ${req.params.birdId} in db`);
return;
}
bird.name = req.body.name;
bird.type = req.body.type;
bird.comments = req.body.comments;
bird.save(function(err, bird) {
if (err) {
console.log(`Failed to update bird id: ${bird._id} in db`);
return;
}
res.json(bird);
});
});
}
function destroy(req, res) {
db.Bird.findByIdAndRemove(req.params.birdId, function(err, bird) {
if (err) {
console.log(`Failed to delete bird id ${req.params.birdId}`);
return;
}
res.json(bird);
});
}
function show(req, res) {
db.Bird.findById(req.params.birdId, function(err, bird) {
if (err) {
console.log(`Cannot find bird id ${req.params.birdId} in db`);
return;
}
res.json(bird);
});
}
//public methods
module.exports = {
index: index,
create: create,
update: update,
destroy: destroy,
show: show,
}