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
3 changes: 3 additions & 0 deletions modulo6/red-fox/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
build
.env
12 changes: 12 additions & 0 deletions modulo6/red-fox/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
19 changes: 19 additions & 0 deletions modulo6/red-fox/src/data/BaseDatabase.ts
Original file line number Diff line number Diff line change
@@ -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
},
});
}
38 changes: 38 additions & 0 deletions modulo6/red-fox/src/data/PokemonDatabase.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
26 changes: 26 additions & 0 deletions modulo6/red-fox/src/data/migrations.ts
Original file line number Diff line number Diff line change
@@ -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
`)
}
}
13 changes: 13 additions & 0 deletions modulo6/red-fox/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es6" ,
"module": "commonjs",
"outDir": "./build" ,
"rootDir": "./" ,
"strict": true ,
"sourceMap": true,
"removeComments": true,
"esModuleInterop": true ,
"forceConsistentCasingInFileNames": true
}
}