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
4 changes: 4 additions & 0 deletions modulo5/pokemon_go/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
package-lock.json
build
.env
29 changes: 29 additions & 0 deletions modulo5/pokemon_go/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "pokemon_go",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start-dev": "ts-node-dev ./src/index.ts",
"start": "tsc && node ./build/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/knex": "^0.16.1",
"@types/uuid": "^8.3.4",
"ts-node-dev": "^1.1.8",
"typescript": "^4.6.4"
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"knex": "^2.0.0",
"mysql": "^2.18.1",
"uuid": "^8.3.2"
}
}
825 changes: 825 additions & 0 deletions modulo5/pokemon_go/pokemonGo.json

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions modulo5/pokemon_go/src/Data/migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import connection from "../connection";

const error= (error:any) => console.log(error.message || error.sqlMessage);

export const createTable= () => connection.raw(`
CREATE TABLE IF NOT EXISTS Pokemons (
id VARCHAR(255) PRIMARY KEY,
table_row INT NOT NULL,
name VARCHAR(255) NOT NULL,
type_1 VARCHAR(255),
type_2 VARCHAR(255),
stat_total INT,
atk INT,
def INT,
sta INT
);
`)
.then(() => console.log("Tabela criada com sucesso."))
.catch(error);
18 changes: 18 additions & 0 deletions modulo5/pokemon_go/src/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import knex from "knex";
import dotenv from "dotenv";

dotenv.config();

const connection = knex({
client: "mysql",
connection: {
host: process.env.DB_HOST,
port: 3306,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
multipleStatements:true
}
});

export default connection;
63 changes: 63 additions & 0 deletions modulo5/pokemon_go/src/endpoints/allPokemons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Request, Response } from "express";
import {createTable} from "../Data/migrations";
import {Pokemon} from "../entities/Pokemon";
import {CreateIds} from "../services/createId";
import connection from "../connection";
const pokemon= require("../../pokemonGo");

export const allPokemons= async (req: Request, res: Response): Promise<void> => {
let codeError: number = 400;

try{
await createTable();

const arrayPokemons= pokemon.map((obj: any) => {
const newIds= new CreateIds();
const newPokemon= new Pokemon(newIds.id(), +obj.Row, obj.Name, obj['Type 1'], obj['Type 2'], +obj['STAT TOTAL'], +obj.ATK, +obj.DEF, +obj.STA)

return {...newPokemon};
});

arrayPokemons.forEach(async (obj:any) => {

const checkPokemonInTable= await connection("Pokemons")
.where("name","like",`${obj.name}`)
.then(data => data)
.catch(error => error);

if(checkPokemonInTable.length){
codeError= 409;
throw new Error("Pokemon já cadastrado na tabela.");
};

await connection("Pokemons")
.insert([obj])
.then()
.catch((error:any) => {
console.log(error.message || error.sqlMessage);
})
});

const showPokemons: any = await connection("Pokemons")
.select("*")
.then(data => data)
.catch((error:any) => {
console.log(error.message || error.sqlMessage);
});

showPokemons.sort((a: any, b: any) => {
if (a.table_row > b.table_row) {
return 1;
}
if (a.table_row < b.table_row) {
return -1;
}
return 0;
});

res.status(200).send(showPokemons);

}catch(error: any){
res.status(codeError).send(error.message || error.sqlMessage);
}
}
41 changes: 41 additions & 0 deletions modulo5/pokemon_go/src/endpoints/pokemonByName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Request, Response } from "express";
import connection from "../connection";

export const pokemonByName=async (req: Request, res: Response): Promise<void> => {
let codeError: number = 400;

try{
const nome= req.body.name.toLowerCase();

if(!nome){
codeError= 422;
res.status(codeError).send("Campo vazio. Preencha corretamente o campo 'name'");
return;
};

if(nome.length < 3){
codeError= 422;
res.status(codeError).send("Name deve possuir no mínimo 3 caracteres.");
return;
};

const settingName= nome[0].toUpperCase() + nome.slice(1);

const checkPokemonInTable: any= await connection("Pokemons")
.where("name","like",`%${settingName}%`)
.then(data => data)
.catch((error:any) => {
console.log(error.message || error.sqlMessage);
})

if(!checkPokemonInTable.length){
codeError= 422;
throw new Error("Pokemon não encontrado.");
}

res.status(200).send(checkPokemonInTable);

}catch(error: any){
res.status(codeError).send(error.message || error.sqlMessage);
}
}
72 changes: 72 additions & 0 deletions modulo5/pokemon_go/src/entities/Pokemon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export class Pokemon{
private id: string;
private table_row: number;
private name: string;
private type_1?: string;
private type_2?: string;
private stat_total?: number;
private atk?: number;
private def?: number;
private sta?: number;

constructor(
id: string,
table_row: number,
name: string,
type_1?: string,
type_2?: string,
stat_total?: number,
atk?: number,
def?: number,
sta?: number
){

this.id= id;
this.table_row=table_row;
this.name= name;
this.type_1 =type_1;
this.type_2 = type_2;
this.stat_total=stat_total;
this.atk= atk;
this.def= def;
this.sta= sta;
};

public get idPokemon(){
return this.id;
};

public get table_rowPokemon(){
return this.table_row;
};

public get namePokemon(){
return this.name;
};

public get type_1Pokemon(){
return this.type_1;
};

public get type_2Pokemon(){
return this.type_2;
};

public get stat_totalPokemon(){
return this.stat_total;
};

public get atkPokemon(){
return this.atk;
};

public get defPokemon(){
return this.def;
};

public get staPokemon(){
return this.sta;
};
}


24 changes: 24 additions & 0 deletions modulo5/pokemon_go/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import express from "express";
import { AddressInfo } from "net";
import {allPokemons} from "./endpoints/allPokemons";
import {pokemonByName} from "./endpoints/pokemonByName";

const app = express();
app.use(express.json());

// Pegar todos os pokemons
app.get("/", allPokemons);


// Pegar pokemon pelo nome
app.get("/name", pokemonByName);


const server = app.listen(process.env.PORT || 3003, () => {
if (server) {
const address = server.address() as AddressInfo;
console.log(`Server is running in http://localhost:${address.port}`);
} else {
console.error(`Failure upon starting server.`);
}
});;
7 changes: 7 additions & 0 deletions modulo5/pokemon_go/src/services/createId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { v4 } from "uuid";

export class CreateIds{
public id(): string {
return v4();
}
};
11 changes: 11 additions & 0 deletions modulo5/pokemon_go/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"outDir": "./build" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"strict": true /* Enable all strict type-checking options. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}