Skip to content

Commit fa16caf

Browse files
authored
Implement full crud (#13)
* Use NYC instead of Istanbul * Implement user show * Implement user update * Implement user delete * Remove istanbul dependency * Update cleanup script * Update eslint to support async/await
1 parent 62e9a04 commit fa16caf

22 files changed

+1502
-302
lines changed

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"es6": true,
55
"mocha": true
66
},
7+
"parserOptions": {
8+
"ecmaVersion": 2017
9+
},
710
"extends": "eslint:recommended",
811
"rules": {
912
"comma-spacing": ["error", { "before": false, "after": true }],

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules
33
config/database.js
44
coverage/
5+
.nyc_output

.istanbul.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

.nycrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"cwd": "./src",
3+
"exclude": [
4+
"container.js",
5+
"app/Application.js",
6+
"interfaces/http/Server.js"
7+
],
8+
"reporter": [
9+
"html",
10+
"lcov",
11+
"text-summary"
12+
]
13+
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"start": "node cluster.js",
1212
"dev": "cross-env NODE_PATH=. NODE_ENV=development nodemon",
1313
"test": "cross-env NODE_PATH=. NODE_ENV=test mocha",
14-
"coverage": "cross-env NODE_PATH=. NODE_ENV=test istanbul cover node_modules/mocha/bin/_mocha",
14+
"coverage": "cross-env NODE_PATH=. NODE_ENV=test nyc mocha",
1515
"lint": "eslint {src,test,config}/**/*.js",
1616
"sequelize": "cross-env NODE_PATH=. sequelize",
1717
"console": "cross-env NODE_PATH=. node src/interfaces/console/index.js",
@@ -31,6 +31,7 @@
3131
"cross-env": "^3.2.3",
3232
"del": "^2.2.2",
3333
"dotenv": "^4.0.0",
34+
"eslint": "^4.7.2",
3435
"express": "^4.15.2",
3536
"express-status-monitor": "^0.1.9",
3637
"http-status": "^1.0.1",
@@ -48,12 +49,11 @@
4849
"chai-change": "^2.1.2",
4950
"chance": "^1.0.6",
5051
"dirty-chai": "^2.0.1",
51-
"eslint": "^3.17.1",
5252
"factory-girl": "^4.0.0",
53-
"istanbul": "^0.4.5",
5453
"listr": "^0.11.0",
5554
"mocha": "^3.2.0",
5655
"nodemon": "^1.11.0",
56+
"nyc": "^11.2.1",
5757
"replace-in-file": "^2.5.0",
5858
"supertest": "^3.0.0",
5959
"supertest-as-promised": "^4.0.2"

scripts/cleanup.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ const tasks = new Listr([
2626
return replace({
2727
files: containerPath,
2828
from: [
29-
/\s*const.*app\/user'\);/,
29+
/\s*const \{(\n.*)+.*app\/user'\);/,
3030
/\s*const.*UsersRepository'\);/,
3131
/\, User: UserModel/,
3232
/\s*usersRepository.*\}\]/,
3333
/\,\s*UserModel/,
34-
/createUser.*\n/,
35-
/\s*getAllUsers.*GetAllUsers/,
34+
/\s+createUser(.|\n)+.*DeleteUser\n/,
3635
],
3736
to: ''
3837
});

src/app/user/DeleteUser.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const Operation = require('src/app/Operation');
2+
3+
class DeleteUser extends Operation {
4+
constructor({ usersRepository }) {
5+
super();
6+
this.usersRepository = usersRepository;
7+
}
8+
9+
async execute(userId) {
10+
const { SUCCESS, ERROR, NOT_FOUND } = this.outputs;
11+
12+
try {
13+
await this.usersRepository.remove(userId);
14+
this.emit(SUCCESS);
15+
} catch(error) {
16+
if(error.message === 'NotFoundError') {
17+
return this.emit(NOT_FOUND, error);
18+
}
19+
20+
this.emit(ERROR, error);
21+
}
22+
}
23+
}
24+
25+
DeleteUser.setOutputs(['SUCCESS', 'ERROR', 'NOT_FOUND']);
26+
27+
module.exports = DeleteUser;

src/app/user/GetUser.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const Operation = require('src/app/Operation');
2+
3+
class GetUser extends Operation {
4+
constructor({ usersRepository }) {
5+
super();
6+
this.usersRepository = usersRepository;
7+
}
8+
9+
async execute(userId) {
10+
const { SUCCESS, NOT_FOUND } = this.outputs;
11+
12+
try {
13+
const user = await this.usersRepository.getById(userId);
14+
this.emit(SUCCESS, user);
15+
} catch(error) {
16+
this.emit(NOT_FOUND, {
17+
type: error.message,
18+
details: error.details
19+
});
20+
}
21+
}
22+
}
23+
24+
GetUser.setOutputs(['SUCCESS', 'ERROR', 'NOT_FOUND'])
25+
26+
module.exports = GetUser;

src/app/user/UpdateUser.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const Operation = require('src/app/Operation');
2+
3+
class UpdateUser extends Operation {
4+
constructor({ usersRepository }) {
5+
super();
6+
this.usersRepository = usersRepository;
7+
}
8+
9+
async execute(userId, userData) {
10+
const {
11+
SUCCESS, NOT_FOUND, VALIDATION_ERROR, ERROR
12+
} = this.outputs;
13+
14+
try {
15+
const user = await this.usersRepository.update(userId, userData);
16+
this.emit(SUCCESS, user);
17+
} catch(error) {
18+
switch(error.message) {
19+
case 'ValidationError':
20+
return this.emit(VALIDATION_ERROR, error);
21+
case 'NotFoundError':
22+
return this.emit(NOT_FOUND, error);
23+
default:
24+
this.emit(ERROR, error);
25+
}
26+
}
27+
}
28+
}
29+
30+
UpdateUser.setOutputs(['SUCCESS', 'NOT_FOUND', 'VALIDATION_ERROR', 'ERROR']);
31+
32+
module.exports = UpdateUser;

src/app/user/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module.exports = {
22
GetAllUsers: require('./GetAllUsers'),
3-
CreateUser: require('./CreateUser')
3+
CreateUser: require('./CreateUser'),
4+
GetUser: require('./GetUser'),
5+
UpdateUser: require('./UpdateUser'),
6+
DeleteUser: require('./DeleteUser')
47
};

0 commit comments

Comments
 (0)