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/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" +} 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 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