Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## DATABASE VARS

DB_PORT=
DB_USERNAME=
DB_PASSWORD=
DB_NAME=db-
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote":false,
"trailingComma":"es5"
}
8 changes: 8 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { resolve } = require('path')

module.exports={
config: resolve(__dirname, 'src', 'config', 'database.js'),
"models-path": resolve(__dirname, 'src', 'app', 'models'),
"migrations-path": resolve(__dirname, 'src', 'database', 'migrations'),
"seeders-path": resolve(__dirname, 'src', 'database', 'seeders')
}
Binary file removed Pokemon Go.xlsx
Binary file not shown.
Binary file added PokemonGo.xlsx
Binary file not shown.
5 changes: 5 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"execMap": {
"js": "node -r sucrase/register"
}
}
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "vaga-backend-teste",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "nodemon src/server.js"
},
"repository": "https://github.com/WilliamWJD/vaga-backend-teste.git",
"author": "williamwjd <[email protected]>",
"license": "MIT",
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1",
"pg": "^8.3.0",
"pg-hstore": "^2.3.3",
"sequelize": "^5.21.11",
"xlsx": "^0.16.4",
"yup": "^0.29.1"
},
"devDependencies": {
"nodemon": "^2.0.4",
"prettier": "^2.0.5",
"sequelize-cli": "^6.2.0",
"sucrase": "^3.15.0"
}
}
22 changes: 22 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import express from "express";
import routes from "./routes";

import "./database";

class App {
constructor() {
this.server = express();
this.middlewares();
this.routes();
}

middlewares() {
this.server.use(express.json());
}

routes() {
this.server.use(routes);
}
}

export default new App().server;
25 changes: 25 additions & 0 deletions src/app/controllers/PlanImportController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Pokemon from "../models/Pokemon";
import { xlsConvertToJson } from "../../services/excelToJson";

class PlanImportController {
async importPlanData(req, res) {
//VERIFICA SE JÁ EXISTEM DADOS IMPORTADOS NO BANCO
const checkData = await Pokemon.findAll();
if (checkData.length !== 0) {
return res.status(401).json({
error: "Os dados já foram importados no banco de dados",
});
}
try {
const plan = xlsConvertToJson();
plan.map(async (item) => {
await Pokemon.create(item);
});
return res.json(plan);
} catch (error) {
return res.json(error);
}
}
}

export default new PlanImportController();
133 changes: 133 additions & 0 deletions src/app/controllers/PokemonController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import Pokemon from "../models/Pokemon";
import * as Yup from "yup";
import { Op } from "sequelize";

class PokemonController {
async index(req, res) {
const page = req.query.page || 1;
const limit = req.query.limit || 25;

const { name, type, type2 } = req.query;

let where = {};

if (name) {
where = {
...where,
name: {
[Op.iLike]: name,
},
};
}

if (type) {
where = {
...where,
type: {
[Op.iLike]: type,
},
};
}

if (type2) {
where = {
...where,
type2: {
[Op.iLike]: type2,
},
};
}

const pokemons = await Pokemon.findAll({
where,
order: ["name"],
limit,
offset: (page - 1) * limit,
});

return res.json(pokemons);
}

async show(req, res) {
const { id } = req.params;

const pokemon = await Pokemon.findByPk(id);

if (!pokemon) {
return res.status(401).json({ error: "Pokemon não encontrado" });
}

return res.json(pokemon);
}

async store(req, res) {
const schema = Yup.object().shape({
name: Yup.string().required(),
type: Yup.string().required(),
atk: Yup.number().required(),
def: Yup.number().required(),
sta: Yup.number().required(),
});

if (!(await schema.isValid(req.body))) {
return res.status(401).json({ error: "Schema is invalid" });
}

const registerLast = await Pokemon.max("id");

const data = { id: registerLast + 1, ...req.body };

const checkExistPokemon = await Pokemon.findByPk(data.id);

if (checkExistPokemon) {
return res
.status(401)
.json({ error: `Pokemon já cadastrado com o id: ${data.id}` });
}

const pokemon = await Pokemon.create(data);
return res.json(pokemon);
}

async update(req, res) {
const schema = Yup.object().shape({
name: Yup.string().required(),
type: Yup.string().required(),
atk: Yup.number().required(),
def: Yup.number().required(),
sta: Yup.number().required(),
});

if (!(await schema.isValid(req.body))) {
return res.status(401).json({ error: "Schema is invalid" });
}

const { id } = req.params;

const pokemon = await Pokemon.findByPk(id);

if (!pokemon) {
return res.status(401).json({ error: "Pokemon não encontrado" });
}

await pokemon.update(req.body);

return res.json(pokemon);
}

async delete(req, res) {
const { id } = req.params;

const pokemon = await Pokemon.findByPk(id);

if (!pokemon) {
return res.status(401).json({ error: "Pokemon não encontrado" });
}

await pokemon.destroy();

return res.json({ message: "Pokemon excluido com sucesso" });
}
}

export default new PokemonController();
46 changes: 46 additions & 0 deletions src/app/models/Pokemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Sequelize, { Model } from "sequelize";

class Pokemon extends Model {
static init(sequelize) {
super.init(
{
name: Sequelize.STRING,
pokedex_number: Sequelize.INTEGER,
img_name: Sequelize.STRING,
generation: Sequelize.INTEGER,
evolution_stage: Sequelize.STRING,
evolved: Sequelize.INTEGER,
familyid: Sequelize.INTEGER,
crossgen: Sequelize.INTEGER,
type: Sequelize.STRING,
type2: Sequelize.STRING,
weather1: Sequelize.STRING,
weather2: Sequelize.STRING,
stat_total: Sequelize.INTEGER,
atk: Sequelize.INTEGER,
def: Sequelize.INTEGER,
sta: Sequelize.INTEGER,
stat_total: Sequelize.INTEGER,
legendary: Sequelize.INTEGER,
aquireable: Sequelize.INTEGER,
spawns: Sequelize.INTEGER,
regional: Sequelize.INTEGER,
raidable: Sequelize.INTEGER,
hatchable: Sequelize.INTEGER,
shiny: Sequelize.INTEGER,
nest: Sequelize.INTEGER,
new: Sequelize.INTEGER,
not_gettable: Sequelize.INTEGER,
future_envolve: Sequelize.INTEGER,
cp_40: Sequelize.INTEGER,
cp_39: Sequelize.INTEGER,
},
{
sequelize,
tableName: "pokemons",
}
);
}
}

export default Pokemon;
13 changes: 13 additions & 0 deletions src/config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require("dotenv").config();

module.exports = {
dialect: "postgres",
host: "localhost",
port: process.env.DB_PORT || 5432,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
define: {
timestamps: false,
},
};
19 changes: 19 additions & 0 deletions src/database/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Sequelize from "sequelize";
import dbConfig from "../config/database";

import Pokemon from "../app/models/Pokemon";

const models = [Pokemon];

class Database {
constructor() {
this.connection = new Sequelize(dbConfig);
this.init();
}

init() {
models.map((model) => model.init(this.connection));
}
}

export default new Database();
Loading