Skip to content

Commit

Permalink
Configuração de ESLint e script de format (admissoesevida#29)
Browse files Browse the repository at this point in the history
* Configuração de Linter do projeto do API (admissoesevida#28)

* Source format
  • Loading branch information
paulojunior95 authored Mar 24, 2019
1 parent e5b5360 commit 782f55a
Show file tree
Hide file tree
Showing 16 changed files with 1,033 additions and 182 deletions.
2 changes: 0 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = true
64 changes: 48 additions & 16 deletions api/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
module.exports = {
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
}
};
env: {
es6: true,
node: true
},
extends: 'eslint:recommended',
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module'
},
plugins: ['sort-imports-es6-autofix', 'prettier'],
rules: {
'spaced-comment': ['error', 'always', { line: { exceptions: ['-'] } }],
quotes: [
'error',
'single',
{ allowTemplateLiterals: true, avoidEscape: true }
],
'max-len': [
'error',
{
code: 80,
ignoreUrls: true,
ignoreStrings: true,
ignoreTemplateLiterals: true,
ignoreRegExpLiterals: true
}
],
'newline-per-chained-call': ['error', { ignoreChainWithDepth: 2 }],
semi: ['error', 'always'],
'prettier/prettier': ['error', { singleQuote: true }],
indent: [
'error',
2,
{ ArrayExpression: 1, MemberExpression: 1, ObjectExpression: 1 }
],
'sort-imports-es6-autofix/sort-imports-es6': [
'error',
{
ignoreCase: false,
ignoreMemberSort: false
}
]
}
};
5 changes: 4 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"main": "./src/server.js",
"scripts": {
"test": "npm run lint && mocha --require @babel/register --require babel-polyfill --exit",
"lint": "eslint src/**",
"lint": "eslint src/** test/**",
"format": "npm run format:prettier && npm run format:eslint",
"format:eslint": "eslint src/** test/** --fix",
"format:prettier": "prettier --write src/**/* test/**/*",
"dev": "nodemon --exec babel-node src/server.js",
"serve": "node dist/server.js",
"build": "babel src --out-dir dist --source-maps",
Expand Down
23 changes: 10 additions & 13 deletions api/src/controllers/member.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export default (app, db) => {
app.get("/members", (req, res) =>
app.get('/members', (req, res) =>
db.member.findAll().then(result => res.json(result))
);

app.get("/member/:id", (req, res) =>
db.member.findByPk(req.params.id).then(result => res.json(result)) //findById is deprecated
app.get('/member/:id', (req, res) =>
db.member.findByPk(req.params.id).then(result => res.json(result))
);

app.post("/member", (req, res) =>
app.post('/member', (req, res) =>
db.member
.create({
name: req.body.name,
Expand All @@ -16,20 +16,17 @@ export default (app, db) => {
.then(result => res.json(result))
);

app.put("/member/:id", (req, res) =>
app.put('/member/:id', (req, res) =>
db.member
.update(
req.body,
{
where: {
id: req.params.id
}
.update(req.body, {
where: {
id: req.params.id
}
)
})
.then(result => res.json(result))
);

app.delete("/member/:id", (req, res) =>
app.delete('/member/:id', (req, res) =>
db.member
.destroy({
where: {
Expand Down
82 changes: 39 additions & 43 deletions api/src/controllers/provider.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
export default (app, db) => {
app.get("/providers", (req, res) =>
db.provider.findAll().then(result => res.json(result))
);

app.get("/provider/:id", (req, res) =>
db.provider.findByPk(req.params.id).then(result => res.json(result)) //findById is deprecated
);

app.post("/provider", (req, res) => {
db.provider
.create({
name: req.body.name,
cpf_cnpj: req.body.cpf_cnpj,
email: req.body.email,
phone: req.body.phone
})
.then(result => res.json(result))
});

app.put("/provider/:id", (req, res) =>
db.provider
.update(
req.body,
{
where: {
id: req.params.id
}
}
)
.then(result => res.json(result))
);

app.delete("/provider/:id", (req, res) =>
db.provider
.destroy({
where: {
id: req.params.id
}
})
.then(result => res.json(result))
);
};

app.get('/providers', (req, res) =>
db.provider.findAll().then(result => res.json(result))
);

app.get('/provider/:id', (req, res) =>
db.provider.findByPk(req.params.id).then(result => res.json(result))
);

app.post('/provider', (req, res) =>
db.provider
.create({
name: req.body.name,
cpf_cnpj: req.body.cpf_cnpj,
email: req.body.email,
phone: req.body.phone
})
.then(result => res.json(result))
);

app.put('/provider/:id', (req, res) =>
db.provider
.update(req.body, {
where: {
id: req.params.id
}
})
.then(result => res.json(result))
);

app.delete('/provider/:id', (req, res) =>
db.provider
.destroy({
where: {
id: req.params.id
}
})
.then(result => res.json(result))
);
};
7 changes: 4 additions & 3 deletions api/src/models/expense.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = (sequelize, DataTypes) => {
const Expense = sequelize.define(
"expense",
'expense',
{
id: {
type: DataTypes.INTEGER,
Expand All @@ -12,7 +12,7 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false
},
value: {
type: DataTypes.DECIMAL(16,2),
type: DataTypes.DECIMAL(16, 2),
allowNull: false
},
note: DataTypes.STRING(5000)
Expand All @@ -26,7 +26,8 @@ module.exports = (sequelize, DataTypes) => {
Expense.belongsTo(models.provider);
};

//Caso faça alguma alteração na estrutura da tabela, descomente uma das linhas de sincronização no index.js desta pasta
// Caso faça alguma alteração na estrutura da tabela,
// descomente uma das linhas de sincronização no index.js desta pasta

return Expense;
};
7 changes: 4 additions & 3 deletions api/src/models/incoming.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = (sequelize, DataTypes) => {
const Incoming = sequelize.define(
"incoming",
'incoming',
{
id: {
type: DataTypes.INTEGER,
Expand All @@ -12,7 +12,7 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false
},
value: {
type: DataTypes.DECIMAL(16,2),
type: DataTypes.DECIMAL(16, 2),
allowNull: false
},
note: DataTypes.STRING(5000)
Expand All @@ -26,7 +26,8 @@ module.exports = (sequelize, DataTypes) => {
Incoming.belongsTo(models.member);
};

//Caso faça alguma alteração na estrutura da tabela, descomente uma das linhas de sincronização no index.js desta pasta
// Caso faça alguma alteração na estrutura da tabela,
// descomente uma das linhas de sincronização no index.js desta pasta

return Incoming;
};
38 changes: 20 additions & 18 deletions api/src/models/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use strict";
'use strict';

const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || "development";
const config = require(__dirname + "/../../config/config.json")[env];
const config_pass_path = __dirname + "/../../config/pwd.json";
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../../config/config.json')[env];
const config_pass_path = __dirname + '/../../config/pwd.json';
const db = {};
let env_pass = '';

Expand All @@ -20,22 +20,17 @@ let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(
config.database,
config.username,
db_pass,
config
);
sequelize = new Sequelize(config.database, config.username, db_pass, config);
}

fs.readdirSync(__dirname)
.filter(file => {
return (
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js'
);
})
.forEach(file => {
const model = sequelize["import"](path.join(__dirname, file));
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});

Expand All @@ -45,9 +40,16 @@ Object.keys(db).forEach(modelName => {
}
});

//Descomente uma das duas linhas para sincronizar as alterações com o banco de dados.
sequelize.sync({alter: true}); //Se quiser somente alterar as estruturas das tabelas sem remover os dados
//sequelize.sync({force: true}); //Se quiser deletar as tabelas e cria-las novamente
/**
* Descomente uma das duas linhas para sincronizar
* as alterações com o banco de dados.
*/

// Se quiser somente alterar as estruturas das tabelas sem remover os dados
sequelize.sync({ alter: true });

// Se quiser deletar as tabelas e cria-las novamente
// sequelize.sync({force: true});

db.sequelize = sequelize;
db.Sequelize = Sequelize;
Expand Down
5 changes: 3 additions & 2 deletions api/src/models/member.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = (sequelize, DataTypes) => {
const Member = sequelize.define(
"member",
'member',
{
id: {
type: DataTypes.INTEGER,
Expand All @@ -22,7 +22,8 @@ module.exports = (sequelize, DataTypes) => {
Member.hasMany(models.incoming);
};

//Caso faça alguma alteração na estrutura da tabela, descomente uma das linhas de sincronização no index.js desta pasta
// Caso faça alguma alteração na estrutura da tabela,
// descomente uma das linhas de sincronização no index.js desta pasta

return Member;
};
5 changes: 3 additions & 2 deletions api/src/models/provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = (sequelize, DataTypes) => {
const Provider = sequelize.define(
"provider",
'provider',
{
id: {
type: DataTypes.INTEGER,
Expand Down Expand Up @@ -35,7 +35,8 @@ module.exports = (sequelize, DataTypes) => {
Provider.hasMany(models.expense);
};

//Caso faça alguma alteração na estrutura da tabela, descomente uma das linhas de sincronização no index.js desta pasta
// Caso faça alguma alteração na estrutura da tabela,
// descomente uma das linhas de sincronização no index.js desta pasta

return Provider;
};
Loading

0 comments on commit 782f55a

Please sign in to comment.