Conversation
| @@ -0,0 +1,13 @@ | |||
| module.exports = { | |||
| HOST: "localhost", | |||
There was a problem hiding this comment.
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.
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.
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.
Nome da tabela é interessante colocar sempre no plural
| type: Sequelize.STRING | ||
| }, | ||
| evolved: { | ||
| type: Sequelize.STRING |
There was a problem hiding this comment.
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.
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.
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.
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.
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.
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.
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.
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.
Seria interessante deixar isso em uma variável de ambiente
No description provided.