Skip to content

Commit ca62f49

Browse files
committed
Release 1.0.0
1 parent ad75b0d commit ca62f49

File tree

9 files changed

+164
-141
lines changed

9 files changed

+164
-141
lines changed

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,33 @@ The code is written in ES6 and uses Promises.
1111

1212
## Requirements :
1313

14-
- [NodeJS](https://nodejs.org/en/).
15-
- [Npm](https://www.npmjs.com/).
14+
- [Git](https://git-scm.com/) if you want to clone this repository.
15+
- [NodeJS](https://nodejs.org/en/) to run the application.
16+
- [Npm](https://www.npmjs.com/) to install dependencies (see the full list below at "Dependencies").
1617
- [Postman](https://www.getpostman.com/) to test the API.
1718

1819
## Getting started :
1920

20-
0. Install this application (See Installation).
21-
1. Start the server with : `node index.js`
22-
2. Connect Postman to the API at : `http://localhost:3000`
23-
3. Try the different routes of the API (For more details, see the [documentation](https://github.com/romwaldtff/NodeJS-REST-API-SQLite/wiki)).
21+
1. Install this application (See Installation).
22+
2. Start the server with : `node index.js`
23+
3. Connect Postman to the API at : `http://localhost:3000`
24+
4. Try the different routes of the API (For more details, see the [documentation](https://github.com/romwaldtff/NodeJS-REST-API-SQLite/wiki)).
2425

2526
Alternatively, you can use `node index.js YOUR_PORT_NUMBER` to start the server with a specific port.
2627

28+
## Dependencies (installed via `npm install`)
29+
30+
- [Body-parser](https://www.npmjs.com/package/body-parser), a Node.js body parsing middleware.
31+
- [Ejs](https://www.npmjs.com/package/ejs) embedded JavaScript templates.
32+
- [Express](https://www.npmjs.com/package/express), a fast and minimalist web framework for node.
33+
- [Sqlite](https://www.npmjs.com/package/sqlite), a wrapper library that adds ES6 promises and SQL-based migrations API to [sqlite3](https://www.npmjs.com/package/sqlite3)*.
34+
- [Bluebird](https://www.npmjs.com/package/bluebird) promise library.
35+
2736
## Credits
2837

2938
- Made by [Romuald Tuffreau](https://github.com/romwaldtff).
3039
- [Laurent Guerin](https://github.com/l-gu), creator of [Telosys Tools](https://sites.google.com/site/telosystools/).
3140

3241
## License
3342

34-
This project uses the LGPL v3 License (See the LICENSE file in this repository).
43+
This project uses the [LGPL v3 License](https://www.gnu.org/licenses/lgpl-3.0.en.html) (See the LICENSE file in this repository).

app/controller/carController.js

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const CarDao = require('../dao/carDao');
33

44
/* Load Controller Common function */
5-
const controllerCommon = require('./common/controllerCommon');
5+
const ControllerCommon = require('./common/controllerCommon');
66

77
/* Load Car entity */
88
const Car = require('../model/car');
@@ -14,7 +14,7 @@ class CarController {
1414

1515
constructor() {
1616
this.carDao = new CarDao();
17-
this.common = new controllerCommon();
17+
this.common = new ControllerCommon();
1818
}
1919

2020
/**
@@ -24,47 +24,31 @@ class CarController {
2424
*/
2525
findById(req, res) {
2626
let id = req.params.id;
27-
let _self = this;
2827

2928
this.carDao.findById(id)
30-
.then(function (result) {
31-
_self.common.findSuccess(result, res);
32-
})
33-
.catch(function (error) {
34-
_self.common.findError(error, res);
35-
});
29+
.then(this.common.findSuccess(res))
30+
.catch(this.common.findError(res));
3631
};
3732

3833
/**
3934
* Finds all entities.
4035
* @return all entities
4136
*/
4237
findAll(res) {
43-
let _self = this;
44-
4538
this.carDao.findAll()
46-
.then(function (results) {
47-
_self.common.findSuccess(results, res);
48-
})
49-
.catch(function (error) {
50-
_self.common.findError(error, res);
51-
});
39+
.then(this.common.findSuccess(res))
40+
.catch(this.common.findError(res));
5241
};
5342

5443
/**
5544
* Counts all the records present in the database
5645
* @return count
5746
*/
5847
countAll(res) {
59-
let _self = this;
6048

6149
this.carDao.countAll()
62-
.then(function (result) {
63-
_self.common.findSuccess(result, res);
64-
})
65-
.catch(function (error) {
66-
_self.common.serverError(error, res);
67-
});
50+
.then(this.common.findSuccess(res))
51+
.catch(this.common.serverError(res));
6852
};
6953

7054
/**
@@ -73,16 +57,16 @@ class CarController {
7357
* @return true if the entity has been updated, false if not found and not updated
7458
*/
7559
update(req, res) {
76-
let _self = this;
77-
let car = new Car(req.body.id, req.body.maker, req.body.model, req.body.year, req.body.driver);
60+
let car = new Car();
61+
car.id = req.body.id;
62+
car.maker = req.body.maker;
63+
car.model = req.body.model;
64+
car.year = req.body.year;
65+
car.driver = req.body.driver;
7866

7967
return this.carDao.update(car)
80-
.then(function () {
81-
_self.common.editSuccess(res);
82-
})
83-
.catch(function (error) {
84-
_self.common.serverError(error, res);
85-
});
68+
.then(this.common.editSuccess(res))
69+
.catch(this.common.serverError(res));
8670
};
8771

8872
/**
@@ -91,16 +75,26 @@ class CarController {
9175
* returns database insertion status
9276
*/
9377
create(req, res) {
94-
let _self = this;
95-
let car = new Car(0, req.body.maker, req.body.model, req.body.year, req.body.driver);
96-
97-
return this.carDao.create(car)
98-
.then(function () {
99-
_self.common.editSuccess(res);
100-
})
101-
.catch(function (error) {
102-
_self.common.serverError(error, res);
103-
});
78+
let car = new Car();
79+
if (req.body.id) {
80+
car.id = req.body.id;
81+
}
82+
car.maker = req.body.maker;
83+
car.model = req.body.model;
84+
car.year = req.body.year;
85+
car.driver = req.body.driver;
86+
87+
if (req.body.id) {
88+
return this.carDao.createWithId(car)
89+
.then(this.common.editSuccess(res))
90+
.catch(this.common.serverError(res));
91+
}
92+
else {
93+
return this.carDao.create(car)
94+
.then(this.common.editSuccess(res))
95+
.catch(this.common.serverError(res));
96+
}
97+
10498
};
10599

106100
/**
@@ -109,16 +103,11 @@ class CarController {
109103
* returns database deletion status
110104
*/
111105
deleteById(req, res) {
112-
let _self = this;
113106
let id = req.params.id;
114107

115108
this.carDao.deleteById(id)
116-
.then(function () {
117-
_self.common.editSuccess(res);
118-
})
119-
.catch(function (error) {
120-
_self.common.serverError(error, res);
121-
});
109+
.then(this.common.editSuccess(res))
110+
.catch(this.common.serverError(res));
122111
};
123112

124113
/**
@@ -127,16 +116,11 @@ class CarController {
127116
* @return
128117
*/
129118
exists(req, res) {
130-
let _self = this;
131119
let id = req.params.id;
132120

133121
this.carDao.exists(id)
134-
.then(function () {
135-
_self.common.existsSuccess(res);
136-
})
137-
.catch(function (error) {
138-
_self.common.findError(error, res);
139-
});
122+
.then(this.common.existsSuccess(res))
123+
.catch(this.common.findError(res));
140124
};
141125
}
142126

app/controller/common/controllerCommon.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,39 @@
33
*/
44
class controllerCommon {
55

6-
findSuccess(result, res) {
7-
res.status(200); // Found
8-
res.json(result);
6+
findSuccess(res) {
7+
return (result) => {
8+
res.status(200); // Found
9+
res.json(result);
10+
}
911
}
1012

11-
existsSuccess(result, res) {
12-
res.status(200); // Found
13-
res.json(result);
13+
existsSuccess(res) {
14+
return (result) => {
15+
res.status(200); // Found
16+
res.json(result);
17+
}
1418
}
1519

1620
editSuccess(res) {
17-
res.status(201); // Created/Updated/Deleted
18-
res.json();
21+
return () => {
22+
res.status(201); // Created/Updated/Deleted
23+
res.json({});
24+
}
1925
}
2026

21-
serverError(error, res) {
22-
res.status(500); // Not Created/Updated/Deleted
23-
res.json(error);
27+
serverError(res) {
28+
return (error) => {
29+
res.status(500);
30+
res.json(error);
31+
}
2432
}
2533

26-
findError(error, res) {
27-
res.status(404); // Not found
28-
res.json(error);
34+
findError(res) {
35+
return (error) => {
36+
res.status(404); // Not found
37+
res.json(error);
38+
}
2939
}
3040
}
3141

0 commit comments

Comments
 (0)