forked from logorn/NodeJS-REST-API-SQLite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriverController.js
More file actions
122 lines (106 loc) · 3.16 KB
/
driverController.js
File metadata and controls
122 lines (106 loc) · 3.16 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Load Driver Data Access Object */
const DriverDao = require('../dao/driverDao');
/* Load Controller Common function */
const controllerCommon = require('./common/controllerCommon');
/* Load Driver entity */
const Driver = require('../model/driver');
/**
* Driver Controller
*/
class DriverController {
constructor() {
this.driverDao = new DriverDao();
this.common = new controllerCommon();
}
/**
* Tries to find an entity using its Id / Primary Key
* @params req, res
* @return entity
*/
findById(req, res) {
let id = req.params.id;
this.driverDao.findById(id)
.then(this.common.findSuccess(res))
.catch(this.common.findError(res));
};
/**
* Finds all entities.
* @return all entities
*/
findAll(res) {
this.driverDao.findAll()
.then(this.common.findSuccess(res))
.catch(this.common.findError(res));
};
/**
* Counts all the records present in the database
* @return count
*/
countAll(res) {
this.driverDao.countAll()
.then(this.common.findSuccess(res))
.catch(this.common.serverError(res));
};
/**
* Updates the given entity in the database
* @params req, res
* @return true if the entity has been updated, false if not found and not updated
*/
update(req, res) {
let driver = new Driver();
driver.id = req.body.id;
driver.firstName = req.body.firstName;
driver.lastName = req.body.lastName;
driver.car = req.body.car;
return this.driverDao.update(driver)
.then(this.common.editSuccess(res))
.catch(this.common.serverError(res));
};
/**
* Creates the given entity in the database
* @params req, res
* returns database insertion status
*/
create(req, res) {
let driver = new Driver();
if (req.body.id) {
driver.id = req.body.id;
}
driver.firstName = req.body.firstName;
driver.lastName = req.body.lastName;
driver.car = req.body.car;
if (req.body.id) {
return this.driverDao.createWithId(driver)
.then(this.common.editSuccess(res))
.catch(this.common.serverError(res));
}
else {
return this.driverDao.create(driver)
.then(this.common.editSuccess(res))
.catch(this.common.serverError(res));
}
};
/**
* Deletes an entity using its Id / Primary Key
* @params req, res
* returns database deletion status
*/
deleteById(req, res) {
let id = req.params.id;
this.driverDao.deleteById(id)
.then(this.common.editSuccess(res))
.catch(this.common.serverError(res));
};
/**
* Returns true if an entity exists with the given Id / Primary Key
* @params req, res
* @return
*/
exists(req, res) {
let id = req.params.id;
this.driverDao.exists(id)
.then(this.common.existsSuccess(res))
.catch(this.common.findError(res));
};
}
module.exports = DriverController;