Skip to content

Commit

Permalink
update pacakages and identation
Browse files Browse the repository at this point in the history
  • Loading branch information
rrfaria committed Apr 12, 2019
1 parent 139011c commit ebc9b26
Show file tree
Hide file tree
Showing 19 changed files with 1,940 additions and 1,126 deletions.
4 changes: 2 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets": ["@babel/preset-env"],
"plugins": ["transform-runtime"]
}
"plugins": ["@babel/plugin-transform-runtime"]
}
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends":"airbnb-base",
"rules":{
"indent": [ 2, 4 ],
"indent": [ 2],
"semi":2,
"import/no-extraneous-dependencies":[
"error",{
Expand Down
18 changes: 9 additions & 9 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export default {
database: 'books',
username: '',
password: '',
params: {
dialect: 'sqlite',
storage: `${process.env.NODE_ENV}_books.sqlite`,
define: {
underscored: true,
},
database: 'books',
username: '',
password: '',
params: {
dialect: 'sqlite',
storage: `${process.env.NODE_ENV}_books.sqlite`,
define: {
underscored: true,
},
},
};
50 changes: 25 additions & 25 deletions config/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ import path from 'path';
let database = null;

const loadModels = (sequelize) => {
const dir = path.join(__dirname, '../models');
const models = [];
fs.readdirSync(dir).forEach((file) => {
const modelDir = path.join(dir, file);
const model = sequelize.import(modelDir);
models[model.name] = model;
});
return models;
const dir = path.join(__dirname, '../models');
const models = [];
fs.readdirSync(dir).forEach((file) => {
const modelDir = path.join(dir, file);
const model = sequelize.import(modelDir);
models[model.name] = model;
});
return models;
};

export default function (app) {
if (!database) {
const config = app.config;
const sequelize = new Sequelize(
config.database,
config.username,
config.password,
config.params,
);
if (!database) {
const { config } = app;
const sequelize = new Sequelize(
config.database,
config.username,
config.password,
config.params,
);

database = {
sequelize,
Sequelize,
models: {},
};
database = {
sequelize,
Sequelize,
models: {},
};

database.models = loadModels(sequelize);
database.models = loadModels(sequelize);

sequelize.sync().done(() => database);
}
return database;
sequelize.sync().done(() => database);
}
return database;
}
77 changes: 38 additions & 39 deletions controllers/books.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
import HttpStatus from 'http-status';

const defaultResponse = (data, statusCode = HttpStatus.OK) => ({
data,
statusCode,
data,
statusCode,
});
const errorResponse = (message, statusCode = HttpStatus.BAD_REQUEST) => defaultResponse({
error: message,
error: message,
}, statusCode);

export default class BooksController {
constructor(Books) {
this.Books = Books;
}

getAll() {
return this.Books.findAll({})
.then(result => defaultResponse(result))
.catch(error => errorResponse(error.message));
}

getById(params) {
return this.Books.findOne({ where: params })
.then(result => defaultResponse(result))
.catch(error => errorResponse(error.message));
}

create(data) {
return this.Books.create(data)
.then(result => defaultResponse(result, HttpStatus.CREATED)) // 201 um recurso foi criado
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}

update(data, params) {
return this.Books.update(data, {
where: params,
}).then(result => defaultResponse(result))
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}

delete(params) {
return this.Books.destroy({
where: params,
}).then(result => defaultResponse(result, HttpStatus.NO_CONTENT))
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}

constructor(Books) {
this.Books = Books;
}

getAll() {
return this.Books.findAll({})
.then(result => defaultResponse(result))
.catch(error => errorResponse(error.message));
}

getById(params) {
return this.Books.findOne({ where: params })
.then(result => defaultResponse(result))
.catch(error => errorResponse(error.message));
}

create(data) {
return this.Books.create(data)
.then(result => defaultResponse(result, HttpStatus.CREATED)) // 201 um recurso foi criado
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}

update(data, params) {
return this.Books.update(data, {
where: params,
}).then(result => defaultResponse(result))
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}

delete(params) {
return this.Books.destroy({
where: params,
}).then(result => defaultResponse(result, HttpStatus.NO_CONTENT))
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}
}
77 changes: 38 additions & 39 deletions controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
import HttpStatus from 'http-status';

const defaultResponse = (data, statusCode = HttpStatus.OK) => ({
data,
statusCode,
data,
statusCode,
});
const errorResponse = (message, statusCode = HttpStatus.BAD_REQUEST) => defaultResponse({
error: message,
error: message,
}, statusCode);

export default class UsersController {
constructor(Users) {
this.Users = Users;
}

getAll() {
return this.Users.findAll({})
.then(result => defaultResponse(result))
.catch(error => errorResponse(error.message));
}

getById(params) {
return this.Users.findOne({ where: params })
.then(result => defaultResponse(result))
.catch(error => errorResponse(error.message));
}

create(data) {
return this.Users.create(data)
.then(result => defaultResponse(result, HttpStatus.CREATED)) // 201 um recurso foi criado
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}

update(data, params) {
return this.Users.update(data, {
where: params,
}).then(result => defaultResponse(result))
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}

delete(params) {
return this.Users.destroy({
where: params,
}).then(result => defaultResponse(result, HttpStatus.NO_CONTENT))
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}

constructor(Users) {
this.Users = Users;
}

getAll() {
return this.Users.findAll({})
.then(result => defaultResponse(result))
.catch(error => errorResponse(error.message));
}

getById(params) {
return this.Users.findOne({ where: params })
.then(result => defaultResponse(result))
.catch(error => errorResponse(error.message));
}

create(data) {
return this.Users.create(data)
.then(result => defaultResponse(result, HttpStatus.CREATED)) // 201 um recurso foi criado
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}

update(data, params) {
return this.Users.update(data, {
where: params,
}).then(result => defaultResponse(result))
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}

delete(params) {
return this.Users.destroy({
where: params,
}).then(result => defaultResponse(result, HttpStatus.NO_CONTENT))
.catch(error => errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY)); // 402 entidade nao pode ser processada
}
}
44 changes: 22 additions & 22 deletions models/Books.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
export default (sequelize, DataType) => {
const Books = sequelize.define('Books', {
id: {
type: DataType.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataType.STRING,
allowNull: false,
validate: {
notEmpty: true,
},
},
description: {
type: DataType.STRING,
allowNull: false,
validate: {
notEmpty: true,
},
},
});
return Books;
const Books = sequelize.define('Books', {
id: {
type: DataType.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataType.STRING,
allowNull: false,
validate: {
notEmpty: true,
},
},
description: {
type: DataType.STRING,
allowNull: false,
validate: {
notEmpty: true,
},
},
});
return Books;
};
Loading

0 comments on commit ebc9b26

Please sign in to comment.