Skip to content

Commit

Permalink
➕ADD: Listado de Modelos
Browse files Browse the repository at this point in the history
Se genera el listado de modelos disponibles dentro de la api

Issue # 6
  • Loading branch information
konami12 committed Jun 25, 2020
1 parent 43634f5 commit 512f5b4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
1 change: 1 addition & 0 deletions app/config/env-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
"process.env.APP_PORT": APP_PORT,
"process.env.ODBC": `mongodb+srv://${USER_DB}:${PASS}@cluster0-eke4y.mongodb.net/${DB}?retryWrites=true&w=majority`,
"process.env.HOST": HOST,
"process.env.README": "https://github.com/OrcaPracticas/workshop-mongo/blob/develop/README.md",
"process.env.CONFIG": {
useNewUrlParser: true,
useUnifiedTopology: true,
Expand Down
6 changes: 5 additions & 1 deletion app/src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import ApiRouter from "./router";
// ======================== CONSTANTES ======================== //

const PORT = process.env.APP_PORT;
const HOST_APP = process.env.HOST;
const README_APP = process.env.README;
const ROOT_PATH = Path.join(__dirname, "../../");
const PATH = `${ROOT_PATH}src/models`;
const Helpers = Tools.instance;
const Server = Express();

Expand Down Expand Up @@ -57,9 +60,10 @@ Server.use(ApiRouter(Router, Helpers));
Server.use("/", (request, response) => {
const { path = "" } = request;
if (path === "/") {
const LIST_MODELS = Helpers.listModels(PATH, HOST_APP, README_APP);
Helpers.messages("Solicitud recibida", "s");
response.status(200);
response.send("<h1>API</h1>");
response.json(LIST_MODELS);
} else {
Helpers.error(response, `Ruta no encontrada ${path}`, "Not found");
}
Expand Down
63 changes: 42 additions & 21 deletions app/src/tools/Helpers.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
import Colors from "cli-color";
import { readdirSync } from "fs";

/* eslint-disable class-methods-use-this */
class Helper {
// Propiedad privada
static #instanceClass = null;

/**
* Permite conseguir los parametros que son enviados a las consultas.
*
* @param {Express} request Objeto referen te a expess.
*
* @returns {void}.
*/
prepareSetting(request) {
const { body = {}, params } = request;
const { field = "" } = params;
params.field = (/_?id/.test(field)) ? "_id" : field;
if (params.Model === "pokemon" && !body.img) {
const { name = "default" } = body;
const IMG = this.formatString(name);
body.img = `${process.env.HOST}/pokemons/${IMG}.jpg`;
}
const setting = { body, Model: params.Model };
delete params.Model;
return { setting, ...params };
}

/**
* Permite manejar el error 404
*
Expand Down Expand Up @@ -101,6 +81,26 @@ class Helper {
response.setHeader("Cache-Control", `max-age=${ttl}`);
}

/**
* Permite listar los modelos referentes a las colecciones disponibles.
*
* @param {String} path Ruta de donde se pueden cargar los modelos.
* @param {String} host Url del host donde se esta probando.
*
* @return {Object}.
*/
listModels(path, host, readme) {
const CONTENT = readdirSync(path);
const models = CONTENT.reduce((list, file) => {
if (file !== "index.js") {
const MODEL = file.replace(/\..*/g, "");
list[MODEL] = `${host}/${MODEL}/all`;
}
return list;
}, {});
return { readme, models };
}

/**
* Permite el envio de mensajes
*
Expand Down Expand Up @@ -148,6 +148,27 @@ class Helper {
console.log(log(` [${msg}] => ${auxText}`)); // eslint-disable-line
}

/**
* Permite conseguir los parametros que son enviados a las consultas.
*
* @param {Express} request Objeto referen te a expess.
*
* @returns {void}.
*/
prepareSetting(request) {
const { body = {}, params } = request;
const { field = "" } = params;
params.field = (/_?id/.test(field)) ? "_id" : field;
if (params.Model === "pokemon" && !body.img) {
const { name = "default" } = body;
const IMG = this.formatString(name);
body.img = `${process.env.HOST}/pokemons/${IMG}.jpg`;
}
const setting = { body, Model: params.Model };
delete params.Model;
return { setting, ...params };
}

/**
* Permite conseguir una instancia de la clase.
*
Expand Down

0 comments on commit 512f5b4

Please sign in to comment.