-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.js
49 lines (42 loc) · 1.31 KB
/
items.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
var mongoose = require('mongoose');
var itemSchema = mongoose.Schema({
item: String
});
var Item = mongoose.model('Item', itemSchema);
exports.create = function(req, res){
if (!req.param('item')) {
res.send('Item not valid');
res.statusCode = 400;
} else {
var item = new Item({ item: req.param('item')});
console.log(item);
item.save(function (err, item) {
if (err) return console.error(err);
res.send("adding " + item);
});
}
};
exports.retrieveAll = function(req, res){
Item.find(function (err, items) {
if (err) return console.error(err);
res.send(items);
});
};
exports.retrieveOne = function(req, res) {
Item.find({'_id':mongoose.Types.ObjectId(req.param('id'))}, function (err, item) {
if (err) return console.error(err);
res.send(item);
});
};
exports.update = function(req, res) {
Item.findByIdAndUpdate(mongoose.Types.ObjectId(req.param('id')), {'item':req.param('item')}, function (err, result) {
if (err) return console.error(err);
res.send(result);
});
};
exports.delete = function(req, res) {
Item.remove({'_id':mongoose.Types.ObjectId(req.param('id'))}, function (err, result) {
if (err) return console.error(err);
res.send({});
});
};