-
Notifications
You must be signed in to change notification settings - Fork 1
/
Toycontroller.js
32 lines (31 loc) · 997 Bytes
/
Toycontroller.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
const db = require("../Project3-project3/models/modeltoys");
// Defining methods for the toyController
module.exports = {
findAll: function (req, res) {
db.Toy.find(req.query)
.then(dbtoy => res.json(dbtoy))
.catch(err => res.status(422).json(err));
},
findById: function (req, res) {
db.Toy.findById(req.params.id)
.then(dbtoy => res.json(dbtoy))
.catch(err => res.status(422).json(err));
},
//Save toy function
create: function (req, res) {
db.Toy.create(req.body)
.then(dbtoy => res.json(dbtoy))
.catch(err => res.status(422).json(err));
},
update: function (req, res) {
db.Toy.findOneAndUpdate({ id: req.params.id }, req.body)
.then(dbtoy => res.json(dbtoy))
.catch(err => res.status(422).json(err));
},
remove: function (req, res) {
db.Toy.findByIdAndRemove(req.params.id)
.then(dbtoy => dbtoy.remove())
.then(dbtoy => res.json(dbtoy))
.catch(err => res.status(422).json(err));
}
};