-
Notifications
You must be signed in to change notification settings - Fork 0
/
departmentAPI.js
65 lines (55 loc) · 1.51 KB
/
departmentAPI.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
var thinky = require('./utils');
var employee = require('./employeeAPI');
var r = thinky.r;
var Department = thinky.createModel('Department',{
name: String,
abbr: String
});
Department.hasMany(employee.Employee,'employees','id','departmentId');
Department.ensureIndex('name');
exports.Department = Department;
exports.list = function(req, res){
Department.orderBy({index: 'name'}).run().then(function(departments){
res.json({
data: departments,
profile: res.decoded[0],
success: true
});
}).error(function(err){
res.json({message: err});
});
};
exports.add = function(req, res){
var department = new Department(req.body);
department.saveAll({employees: true}).then(function(result){
res.json(result);
}).error(function(err){
res.json({message: err})
});
};
exports.get = function(req,res){
Department.get(req.params.id).run().then(function(department){
res.json(department);
}).error(function(err){
res.json({message: err});
});
};
//body must be raw on request
exports.delete = function(req,res){
Department.get(req.params.id).delete().execute().then(function(result){
res.json(result);
}).error(function(err){
res.json({message: err});
});
};
exports.update = function(req,res){
Department.get(req.params.id).run().then(function(department){
if(req.body.name) department.name = req.body.name;
if(req.body.abbr) department.abbr = req.body.abbr;
department.save().then(function(result){
res.json(result);
}).error(function(err){
res.json({message: err});
});
});
};