-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestAPI.js
66 lines (58 loc) · 1.58 KB
/
requestAPI.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var thinky = require('./utils');
var r = thinky.r;
var reqLine = require('./requestLineAPI');
var Request = thinky.createModel('Request',{
reason: String,
date: Date,
hour: String,
feedingType: String,
created_at: thinky.type.date().default(r.now()),
created_by: String
});
Request.hasMany(reqLine.RequestLine,'requestLines','id','requestID');
Request.ensureIndex('date');
exports.list = function(req, res){
Request.orderBy({index: 'date'}).getJoin({requestLines: true}).run().then(function(requests){
res.json({
data: requests,
profile: res.decoded[0],
success: true
});
}).error(function(err){
res.json({message:err});
});
};
exports.add = function(req, res){
req.body.created_by = res.decoded[0].username;
var request = new Request(req.body);
request.saveAll({requestLines: true}).then(function(result){
res.json(result);
}).error(function(err){
res.json({message: err});
});
};
exports.get = function(req, res){
Request.get(req.params.id).run().then(function(request){
res.json(request);
}).error(function(err){
res.json({message: err})
});
};
exports.update = function(req, res){
delete req.body['requestLines'];
var request = new Request(req.body)
Request.get(req.params.id).update(req.body).run().then(function(result){
res.json(result);
}).error(function(err){
res.json({message: err});
});
};
exports.delete = function(req, res){
Request.get(req.params.id).getJoin({requestLines: true}).run().then(function(result){
console.log(result);
result.deleteAll();
}).error(function(err){
res.json({message: err});
});
};
exports.Request = Request;