From bf677cf2c6e29d9136bb02049eb3639b91c68c29 Mon Sep 17 00:00:00 2001 From: 0DayIg0r Date: Tue, 12 Apr 2022 18:22:54 -0300 Subject: [PATCH 1/3] iniciando --- modulo6/red-fox/package.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 modulo6/red-fox/package.json diff --git a/modulo6/red-fox/package.json b/modulo6/red-fox/package.json new file mode 100644 index 0000000..49e6749 --- /dev/null +++ b/modulo6/red-fox/package.json @@ -0,0 +1,12 @@ +{ + "name": "red-fox", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} From ba27d1b6f7b22d6e786a25de65ca4cdbfe8dc76b Mon Sep 17 00:00:00 2001 From: 0DayIg0r Date: Wed, 13 Apr 2022 11:38:13 -0300 Subject: [PATCH 2/3] criando pastas e arquivos base --- modulo6/red-fox/.gitignore | 3 +++ modulo6/red-fox/tsconfig.ts | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 modulo6/red-fox/.gitignore create mode 100644 modulo6/red-fox/tsconfig.ts diff --git a/modulo6/red-fox/.gitignore b/modulo6/red-fox/.gitignore new file mode 100644 index 0000000..c27e9ba --- /dev/null +++ b/modulo6/red-fox/.gitignore @@ -0,0 +1,3 @@ +node_modules +build +.env \ No newline at end of file diff --git a/modulo6/red-fox/tsconfig.ts b/modulo6/red-fox/tsconfig.ts new file mode 100644 index 0000000..51aa2e7 --- /dev/null +++ b/modulo6/red-fox/tsconfig.ts @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es6" , + "module": "commonjs", + "outDir": "./build" , + "rootDir": "./" , + "strict": true , + "sourceMap": true, + "removeComments": true, + "esModuleInterop": true , + "forceConsistentCasingInFileNames": true + } + } \ No newline at end of file From 2d2f240864993bbdc4d14f19231382082ca013e5 Mon Sep 17 00:00:00 2001 From: 0DayIg0r Date: Wed, 13 Apr 2022 13:08:52 -0300 Subject: [PATCH 3/3] algumas funcionalidades --- modulo6/red-fox/src/data/BaseDatabase.ts | 19 +++++++++++ modulo6/red-fox/src/data/PokemonDatabase.ts | 38 +++++++++++++++++++++ modulo6/red-fox/src/data/migrations.ts | 26 ++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 modulo6/red-fox/src/data/BaseDatabase.ts create mode 100644 modulo6/red-fox/src/data/PokemonDatabase.ts create mode 100644 modulo6/red-fox/src/data/migrations.ts diff --git a/modulo6/red-fox/src/data/BaseDatabase.ts b/modulo6/red-fox/src/data/BaseDatabase.ts new file mode 100644 index 0000000..01e01ac --- /dev/null +++ b/modulo6/red-fox/src/data/BaseDatabase.ts @@ -0,0 +1,19 @@ +import knex from 'knex' +import dotenv from 'dotenv' + + +dotenv.config() + +export class BaseDatabase { + protected static connection = knex({ + client: 'mysql', + connection: { + host: process.env.DB_HOST, + port: 3306, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_SCHEMA, + multipleStatements: true + }, + }); +} \ No newline at end of file diff --git a/modulo6/red-fox/src/data/PokemonDatabase.ts b/modulo6/red-fox/src/data/PokemonDatabase.ts new file mode 100644 index 0000000..5271165 --- /dev/null +++ b/modulo6/red-fox/src/data/PokemonDatabase.ts @@ -0,0 +1,38 @@ +import { BaseDatabase } from './BaseDatabase' +import { PokemonRepository } from '../business/PokemonRepository' +import { CustomError } from '../error/CustomError' + + +export class PokemonDatabase extends BaseDatabase implements PokemonRepository { + TABLE_NAME = 'PokemonDB' + + getAllPokemons = async (limit: number, offset: number) => { + try { + const result = await BaseDatabase.connection(this.TABLE_NAME) + .select("*") + .limit(limit) + .offset(offset) + + if (!result) { + return null + } + return result + } catch (e) { + throw new CustomError('Database error', 400) + } + } + + getPokemonsByName = async (name: string, limit: number, offset: number) => { + try { + const result = await BaseDatabase.connection(this.TABLE_NAME) + .select("Row", "name", "Type_1", "Type_2", "ATK", "DEF", "STA") + .where({ name }) + + if(!result){ + return null + } + } catch (e){ + throw new CustomError('Database error', 400) + } + } +} \ No newline at end of file diff --git a/modulo6/red-fox/src/data/migrations.ts b/modulo6/red-fox/src/data/migrations.ts new file mode 100644 index 0000000..5fd4561 --- /dev/null +++ b/modulo6/red-fox/src/data/migrations.ts @@ -0,0 +1,26 @@ +import { BaseDatabase } from './BaseDatabase' + +class Migrations extends BaseDatabase { + private createTable(){ + Migrations.connection + .raw(` + CREATE TABLE IF NOT EXISTS PokemonDB( + row VARCHAR(255) PRIMARY KEY, + name VARCHAR(255) NOT NULL, + pokedex_number INT UNIQUE NOT NULL, + generation INT NOT NULL, + evolution_stage VARCHAR(255) NOT NULL, + family_id VARCHAR(255) NOT NULL, + type_1 VARCHAR(255) NOT NULL, + type_2 VARCHAR(255) NOT NULL, + weather_1 VARCHAR(255) NOT NULL + weather_2 VARCHAR(255) NOT NULL, + stat_total INT NOT NULL, + attack INT NOT NULL, + defense INT NOT NULL, + stamina INT NOT NULL, + legendary INT NOT NULL, + shiny INT NOT NULL + `) + } +} \ No newline at end of file