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 semana23/projeto/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
package-lock.json
build
.env
28 changes: 28 additions & 0 deletions semana23/projeto/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "projeto",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node-dev ./src/index.ts",
"start": "tsc && node ./build/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/knex": "^0.16.1",
"cors": "^2.8.5",
"dotenv": "^14.3.2",
"express": "^4.17.2",
"knex": "^1.0.1",
"mysql": "^2.18.1"
},
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/node": "^17.0.12",
"ts-node-dev": "^1.1.8",
"typescript": "^4.5.5"
}
}
19 changes: 19 additions & 0 deletions semana23/projeto/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import express, {Express} from 'express'
import cors from 'cors'
import { AddressInfo } from 'net';

const app: Express = express();

app.use(express.json());
app.use(cors());

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.`);
}
});

export default app
12 changes: 12 additions & 0 deletions semana23/projeto/src/data/Pokemon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const xlsxj = require("xlsx-to-json");
xlsxj({
input: "Pokemon.xlsx",
output: "Pokemon.json"
}, function(err: any, result: any) {
if(err) {
console.error(err);
}else {
console.log(result);
}
});

Binary file added semana23/projeto/src/data/Pokemon.xlsx
Binary file not shown.
Empty file added semana23/projeto/src/index.ts
Empty file.
17 changes: 17 additions & 0 deletions semana23/projeto/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"sourceMap": true,
"outDir": "./build",
"rootDir": "./src",
"removeComments": true,
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true

}
}
5 changes: 5 additions & 0 deletions semana23/semana23-projeto/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

node_modules
package-lock.json
build
.env
32 changes: 32 additions & 0 deletions semana23/semana23-projeto/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "semana23-projeto",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"pokemon": "tsc && node ./build/dataPokemon/pokemon.js",
"dev": "ts-node-dev ./src/index.ts",
"start": "tsc && node ./build/index.js",
"migrations": "tsnd ./src/data/migrations.ts",
"tratando": "tsnd ./src/data/tratando.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/node": "^17.0.13",
"ts-node-dev": "^1.1.8",
"typescript": "^4.5.5"
},
"dependencies": {
"@types/knex": "^0.16.1",
"cors": "^2.8.5",
"dotenv": "^14.3.2",
"express": "^4.17.2",
"knex": "^1.0.1",
"mysql": "^2.18.1",
"xlsx-to-json": "^0.3.0"
}
}
19 changes: 19 additions & 0 deletions semana23/semana23-projeto/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import express, {Express} from 'express'
import cors from 'cors'
import { AddressInfo } from 'net';

const app: Express = express();

app.use(express.json());
app.use(cors());

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.`);
}
});

export default app
15 changes: 15 additions & 0 deletions semana23/semana23-projeto/src/data/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import knex from "knex";
import dotenv from "dotenv";

dotenv.config();

export 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
}
});
56 changes: 56 additions & 0 deletions semana23/semana23-projeto/src/data/migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { connection } from "./connection"
import pokemons from "../dataPokemon/pokemonGo.json"



const printError = (error: any) => { console.log(error.sqlMessage || error.message) }

const createTables = () => connection
.raw(`
CREATE TABLE IF NOT EXISTS pokemonGo(
id VARCHAR(255) ,
name VARCHAR(255) NOT NULL,
pokedexNumber VARCHAR(255),
imgName VARCHAR(255),
generation VARCHAR(255),
evolutionStage VARCHAR(255),
evolved VARCHAR(255),
familyID VARCHAR(255),
crossGen VARCHAR(255),
type1 VARCHAR(255),
type2 VARCHAR(255),
weather1 VARCHAR(255),
weather2 VARCHAR(255),
statTotal VARCHAR(255),
ATK VARCHAR(255),
DEF VARCHAR(255),
STA VARCHAR(255),
legendary VARCHAR(255),
aquireable VARCHAR(255),
spawns VARCHAR(255),
regional VARCHAR(255),
raidable VARCHAR(255),
hatchable VARCHAR(255),
shiny VARCHAR(255),
nest VARCHAR(255),
new VARCHAR(255),
notGettable VARCHAR(255),
futureEvolve VARCHAR(255),
CP40 VARCHAR(255),
CP39 VARCHAR(255)
);

`)
.then(() => { console.log("Tabela criada") })
.catch(printError)

const insertPokemon = () => connection("pokemonGo")
.insert(pokemons)
.then(() => { console.log("Pokemons inseridos.") })
.catch(printError)

const closeConnection = () => { connection.destroy() }

createTables()
.then(insertPokemon)
.finally(closeConnection)
11 changes: 11 additions & 0 deletions semana23/semana23-projeto/src/dataPokemon/pokemon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const xlsxj = require("xlsx-to-json")
xlsxj({
input: "./src/dataPokemon/pokemonGo.xlsx",
output: "./src/dataPokemon/pokemonGo.json"
}, function(err: any, result: any) {
if(err) {
console.error(err);
}else {
console.log(result);
}
});
1 change: 1 addition & 0 deletions semana23/semana23-projeto/src/dataPokemon/pokemonGo.json

Large diffs are not rendered by default.

Binary file not shown.
25 changes: 25 additions & 0 deletions semana23/semana23-projeto/src/endpoints/countPokemonsGeneration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Request, Response } from "express";
import { connection } from "../data/connection";

export const countPokemonsGeneration = async(req:Request, res:Response):Promise<void> => {
try {

const generation = req.query.generation || "%"

const [existGeneration] = await connection("pokemonGo")
.where("generation", "LIKE", `%${generation}%`)

if(!existGeneration){
throw new Error("Geração não existe")
}


const result = await connection("pokemonGo")
.count()
.where("generation", "LIKE", `%${generation}%`)

res.status(200).send(result)
} catch (error:any) {
res.status(400).send({message: error.sqlMessage || error.message})
}
}
30 changes: 30 additions & 0 deletions semana23/semana23-projeto/src/endpoints/searchByGeneration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Request, Response } from "express";
import { connection } from "../data/connection";

export const searchByGeneration = async(req:Request, res:Response):Promise<void> => {
try {

const {generation} = req.params

if(!generation){
throw new Error("Insira um parâmetro válido")
}

//Validando se a geração inserida é igual a geração do banco de dados

const [existGeneration] = await connection("pokemonGo")
.where({generation})

if(!existGeneration){
throw new Error("Insira uma geração de 1 à 7")
}

const result = await connection("pokemonGo")
.where({generation})


res.status(200).send(result)
} catch (error:any) {
res.status(400).send({message: error.sqlMessage || error.message})
}
}
23 changes: 23 additions & 0 deletions semana23/semana23-projeto/src/endpoints/searchByName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Request, Response } from "express";
import { connection } from "../data/connection";

export const searchByName = async(req:Request, res:Response):Promise<void> => {
try {

const name = req.query.name || "%"

const [existName] = await connection("pokemonGo")
.where("name", "LIKE", `%${name}%`)

if(!existName){
throw new Error("Pokemon não cadastrado")
}

const result = await connection("pokemonGo")
.where("name", "LIKE", `%${name}%`)

res.status(200).send(result)
} catch (error:any) {
res.status(400).send({message: error.sqlMessage || error.message})
}
}
19 changes: 19 additions & 0 deletions semana23/semana23-projeto/src/endpoints/searchByStatTotal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Request, Response } from "express";
import { connection } from "../data/connection";

export const searchByStatTotal = async(req:Request, res:Response) => {
try {
const {value1, value2} = req.query

const [result]= await connection.raw(`
SELECT * FROM pokemonGo
WHERE statTotal BETWEEN '${value1}' AND '${value2}';
`);

res.status(200).send(result)


} catch (error:any) {
res.status(400).send({message: error.sqlMessage || error.message})
}
}
19 changes: 19 additions & 0 deletions semana23/semana23-projeto/src/endpoints/searchByTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Request, Response } from "express";
import { connection } from "../data/connection";

export const searchByTypes = async(req:Request, res:Response) => {
try {
const {type1, type2} = req.query

const [result]= await connection.raw(`
SELECT * FROM pokemonGo
WHERE type1 = "${type1}" OR type2 = "${type2}";
`);

res.status(200).send(result)


} catch (error:any) {
res.status(400).send({message: error.sqlMessage || error.message})
}
}
Loading