-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-component.js
More file actions
63 lines (52 loc) · 1.98 KB
/
Copy pathserver-component.js
File metadata and controls
63 lines (52 loc) · 1.98 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
class AppComponent {
beforeRegister() {
this.is = 'server-component';
}
ready() {
const bodyParser = require('body-parser');
const logger = require('morgan');
this.bodyParserMW = bodyParser.json();
this.bodyParserURLEncodedMW = bodyParser.urlencoded({extended: true});
this.loggerMW = logger('dev');
this.paramCallback = (app) => {
app.param('collectionName', function(req, res, next, collectionName){
// req.collection = db.collection(collectionName)
return next()
});
};
this.getIndex = (req, res) => {
res.send('please select a collection, e.g., /collections/messages');
};
this.getCollection = (req, res, next) => {
req.collection.find({} ,{limit: 10, sort: {'_id': -1}}).toArray(function(e, results){
if (e) return next(e)
res.send(results)
});
}
this.postCollection = (req, res, next) => {
req.collection.insert(req.body, {}, function(e, results){
if (e) return next(e)
res.send(results)
});
}
this.getCollectionEntity = (req, res, next) => {
req.collection.findById(req.params.id, function(e, result){
if (e) return next(e)
res.send(result)
});
}
this.putCollectionEntity = (req, res, next) => {
req.collection.updateById(req.params.id, {$set: req.body}, {safe: true, multi: false}, function(e, result){
if (e) return next(e)
res.send((result === 1) ? {msg:'success'} : {msg: 'error'})
});
}
this.deleteCollectionEntity = (req, res, next) => {
req.collection.removeById(req.params.id, function(e, result){
if (e) return next(e)
res.send((result === 1)?{msg: 'success'} : {msg: 'error'})
});
}
}
}
Polymer(AppComponent);