-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
841 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
DB_PATH=./data/database.db | ||
API_FOOTBALL_DATA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
{ | ||
"extends": ["airbnb-base", "prettier"], | ||
"plugins": ["prettier"], | ||
"plugins": ["prettier", "jest"], | ||
"rules": { | ||
"prettier/prettier": ["error"] | ||
"prettier/prettier": ["error"], | ||
"class-methods-use-this": "off" | ||
}, | ||
"env": { | ||
"jest/globals": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
database.db |
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const ClubController = require('../club'); | ||
|
||
const serviceMock = { | ||
save: jest.fn(), | ||
delete: jest.fn(), | ||
}; | ||
const controller = new ClubController(serviceMock); | ||
|
||
test('Index renderea index.html', () => { | ||
const renderMock = jest.fn(); | ||
controller.index({}, { render: renderMock }); | ||
expect(renderMock).toHaveBeenCalledTimes(1); | ||
expect(renderMock).toHaveBeenCalledWith('club/view/index.html'); | ||
}); | ||
|
||
test('View renderea form.html', () => { | ||
const renderMock = jest.fn(); | ||
controller.view({}, { render: renderMock }); | ||
expect(renderMock).toHaveBeenCalledTimes(1); | ||
expect(renderMock).toHaveBeenCalledWith('club/view/form.html'); | ||
}); | ||
|
||
test('Save llama al servicio con el body y redirecciona a /club', () => { | ||
const redirectMock = jest.fn(); | ||
const bodyMock = { id: 1 }; | ||
controller.save({ body: bodyMock }, { redirect: redirectMock }); | ||
expect(serviceMock.save).toHaveBeenCalledTimes(1); | ||
expect(serviceMock.save).toHaveBeenCalledWith(bodyMock); | ||
expect(redirectMock).toHaveBeenCalledTimes(1); | ||
expect(redirectMock).toHaveBeenCalledWith('/club'); | ||
}); | ||
|
||
test('Delete llama al servicio con el id del body y redirecciona a /club', () => { | ||
const redirectMock = jest.fn(); | ||
controller.delete({ body: { id: 1 } }, { redirect: redirectMock }); | ||
expect(serviceMock.delete).toHaveBeenCalledTimes(1); | ||
expect(serviceMock.delete).toHaveBeenCalledWith(1); | ||
expect(redirectMock).toHaveBeenCalledTimes(1); | ||
expect(redirectMock).toHaveBeenCalledWith('/club'); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const { formDataToEntity } = require('../mapper/clubMapper'); | ||
|
||
module.exports = class ClubController { | ||
/** | ||
* @param {import('../service/clubService')} clubService | ||
*/ | ||
constructor(clubService) { | ||
this.clubService = clubService; | ||
} | ||
|
||
/** | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
*/ | ||
async index(req, res) { | ||
const clubs = await this.clubService.getAll(); | ||
res.render('club/view/index.html', { data: { clubs } }); | ||
} | ||
|
||
/** | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
*/ | ||
view(req, res) { | ||
res.render('club/view/form.html'); | ||
} | ||
|
||
/** | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
*/ | ||
async save(req, res) { | ||
try { | ||
await this.clubService.save(formDataToEntity(req.body)); | ||
res.redirect('/club'); | ||
} catch (e) { | ||
console.error(e); | ||
res.end(); | ||
// res.render('club/view/form.html', { error: e }); | ||
} | ||
} | ||
|
||
/** | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
*/ | ||
delete(req, res) { | ||
this.clubService.delete(req.body.id); | ||
res.redirect('/club'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module.exports = class Club { | ||
constructor({ | ||
id, | ||
name, | ||
shortName, | ||
tla, | ||
crestUrl, | ||
address, | ||
phone, | ||
website, | ||
email, | ||
founded, | ||
clubColors, | ||
venue, | ||
}) { | ||
this.id = id; | ||
this.name = name; | ||
this.shortName = shortName; | ||
this.tla = tla; | ||
this.crestUrl = crestUrl; | ||
this.address = address; | ||
this.phone = phone; | ||
this.website = website; | ||
this.email = email; | ||
this.founded = founded; | ||
this.clubColors = clubColors; | ||
this.venue = venue; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const axios = require('axios'); | ||
const fs = require('fs'); | ||
require('dotenv').config(); | ||
|
||
if (process.env.API_FOOTBALL_DATA.length === 0) { | ||
console.log( | ||
'Se debe crear un archivo.env (copiar el .env.dist y cambiarle el nombre) y configurar la API key gratuita de https://www.football-data.org/' | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const API_TOKEN = process.env.API_FOOTBALL_DATA; | ||
const API_BASE = 'http://api.football-data.org/v2'; | ||
const ENGLAND_ID = 2021; | ||
|
||
async function obtenerEquipos() { | ||
console.log('obteniendo equipos...'); | ||
const resultado = await axios.get(`${API_BASE}/competitions/${ENGLAND_ID}/teams`, { | ||
headers: { 'X-Auth-Token': API_TOKEN }, | ||
}); | ||
return resultado.data.teams; | ||
} | ||
|
||
async function obtenerPlantel(id) { | ||
console.log(`obteniendo plantel id ${id}...`); | ||
const respuesta = await axios.get(`${API_BASE}/teams/${id}`, { | ||
headers: { 'X-Auth-Token': API_TOKEN }, | ||
}); | ||
|
||
return respuesta.data; | ||
} | ||
|
||
async function iniciar() { | ||
const DIRECTORIO = './data'; | ||
const archivo = `${DIRECTORIO}/equipos.json`; | ||
|
||
let equipos; | ||
|
||
if (!fs.existsSync(archivo)) { | ||
equipos = await obtenerEquipos(); | ||
fs.writeFileSync(archivo, JSON.stringify(equipos)); | ||
} | ||
|
||
equipos = JSON.parse(fs.readFileSync(archivo)); | ||
|
||
equipos.forEach(async (equipo) => { | ||
const archivoPlantel = `${DIRECTORIO}/equipos/${equipo.tla}.json`; | ||
|
||
if (!fs.existsSync(archivoPlantel)) { | ||
fs.writeFileSync(archivoPlantel, JSON.stringify(await obtenerPlantel(equipo.id))); | ||
} | ||
}); | ||
} | ||
|
||
iniciar(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// map from Controller to Entity | ||
// map from Entity to Controller | ||
// map from Entity to Model | ||
// map from Model to Entity | ||
|
||
const Club = require('../entity/club'); | ||
|
||
module.exports = { | ||
/** | ||
* | ||
* @param {Object} formData | ||
* @returns Club | ||
*/ | ||
formDataToEntity({ | ||
id, | ||
name, | ||
'short-name': shortName, | ||
tla, | ||
'crest-url': crestUrl, | ||
address, | ||
phone, | ||
website, | ||
email, | ||
founded, | ||
'club-colors': clubColors, | ||
venue, | ||
}) { | ||
return new Club({ | ||
id, | ||
name, | ||
shortName, | ||
tla, | ||
crestUrl, | ||
address, | ||
phone, | ||
website, | ||
email, | ||
founded, | ||
clubColors, | ||
venue, | ||
}); | ||
}, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* eslint-disable no-empty-function */ | ||
/* eslint-disable no-unused-vars */ | ||
const AbstractRepositoryError = require('./error/abstractRepositoryError'); | ||
|
||
module.exports = class AbstractClubRepository { | ||
constructor() { | ||
throw new AbstractRepositoryError('No se puede instanciar el repositorio de clubes abstracto.'); | ||
} | ||
|
||
/** | ||
* @param {import('../entity/club')} club | ||
* @returns {import('../entity/club')} | ||
*/ | ||
async save(club) {} | ||
|
||
/** | ||
* @param {Number} id | ||
*/ | ||
async delete(id) {} | ||
|
||
/** | ||
* @param {Number} id | ||
* @returns {import('../entity/club')} | ||
*/ | ||
async get(id) {} | ||
|
||
/** | ||
* @returns {Array<import('../entity/club')>} | ||
*/ | ||
async getAll() {} | ||
}; |
Oops, something went wrong.