-
Notifications
You must be signed in to change notification settings - Fork 0
/
employeeAPI.js
62 lines (52 loc) · 1.31 KB
/
employeeAPI.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
var thinky = require('./utils');
var Employee = thinky.createModel('Employee',{
name: String,
lastName: String,
departmentId: String,
completeName: String,
pin: Number,
type: String
});
Employee.ensureIndex('name');
exports.Employee = Employee;
exports.list = function(req, res){
Employee.orderBy({index: 'name'}).getJoin({department: true}).run().then(function(employees){
res.json({
data: employees,
profile: res.decoded[0],
success: true
});
}).error(function(err){
res.json({message:err});
});
};
exports.add = function(req, res){
var employee = new Employee(req.body);
employee.saveAll({department: true}).then(function(result){
res.json(result);
}).error(function(err){
res.json({message: err});
});
};
exports.get = function(req, res){
Employee.get(req.params.id).run().then(function(employee){
res.json(employee);
}).error(function(err){
res.json({message: err})
});
};
exports.update = function(req, res){
delete req.body['department'];
Employee.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){
Employee.get(req.params.id).delete().execute().then(function(result){
res.json(result);
}).error(function(err){
res.json({message: err});
});
};