-
Notifications
You must be signed in to change notification settings - Fork 47
Joao pedro lima soares #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| @@ -0,0 +1,13 @@ | |||
| module.exports = { | |||
| HOST: "localhost", | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Poderia ter criado variáveis ambientes
| app.use(express.urlencoded({ extended: true })); | ||
| initRoutes(app); | ||
| db.sequelize.sync(); | ||
| // db.sequelize.sync({ force: true }).then(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pode apagar comentários desnecessários
| const excelController = require("../controllers/pokemon/excel.controllers"); | ||
| const upload = require("../middlewares/upload"); | ||
| let routes = (app) => { | ||
| router.post("/upload", upload.single("file"), excelController.upload); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Poderia separar em duas rotas, uma para fazer o upload do excel de pokemon e outra para listar os pokemons do banco.
| @@ -0,0 +1,96 @@ | |||
| module.exports = (sequelize, Sequelize) => { | |||
| const Pokemon = sequelize.define("pokemon", { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nome da tabela é interessante colocar sempre no plural
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
O plural de pokemon é pokemon 🤣
| type: Sequelize.STRING | ||
| }, | ||
| evolved: { | ||
| type: Sequelize.STRING |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Poderia converter os dados numéricos e booleano e salvar com o tipo certo no banco para ocupar menos espaço
| return res.status(400).send("Please upload an excel file!"); | ||
| } | ||
| let path = | ||
| __basedir + "/resources/static/assets/uploads/" + req.file.filename; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Poderia usar a lib fs pra pegar o caminho do diretório e facilitar essa busca
| // skip header | ||
| rows.shift(); | ||
| let pokemons = []; | ||
| rows.forEach((row) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aqui vc poderia usar o map, ele é mais rápido em termos de performance e já retorna um array pra você, não precisando criar um array de dar push.
| let pokemons = []; | ||
| rows.forEach((row) => { | ||
| let pokemon = { | ||
| id: row[0], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ao invés de pegar cada coluna da linha indicando o índice(row[0]) por exemplo, voce poderia desestruturar, assim:
const [id, name, pokedex_number, img_name, generation...] = row
| }; | ||
| pokemons.push(pokemon); | ||
| }); | ||
| Pokemon.bulkCreate(pokemons) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Se a função é assíncrona, poderia então usar o await ao invés do then e catch
| } | ||
| }; | ||
| const getPokemons = (req, res) => { | ||
| Pokemon.findAll() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Poderia deixar assíncrono e fazer assim:
return await Pokemon.findAll()| }); | ||
| Pokemon.bulkCreate(pokemons) | ||
| .then(() => { | ||
| res.status(200).send({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
o status padrão pra criação de dados é o 201
| .catch((error) => { | ||
| res.status(500).send({ | ||
| message: "Fail to import data into database!", | ||
| error: error.message, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
o stacktrace do erro fica perdido aqui.
| // db.sequelize.sync({ force: true }).then(() => { | ||
| // console.log("Drop and re-sync db."); | ||
| // }); | ||
| let port = 8080; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seria interessante deixar isso em uma variável de ambiente
No description provided.