From 512f5b4fceab491f894987d61c3bc63c051d7a00 Mon Sep 17 00:00:00 2001 From: Jorge Mendez Ortega Date: Wed, 24 Jun 2020 20:20:10 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9E=95ADD:=20Listado=20de=20Modelos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Se genera el listado de modelos disponibles dentro de la api Issue # 6 --- app/config/env-config.js | 1 + app/src/server/index.js | 6 +++- app/src/tools/Helpers.js | 63 ++++++++++++++++++++++++++-------------- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/app/config/env-config.js b/app/config/env-config.js index 60a72a0..5fda7ac 100644 --- a/app/config/env-config.js +++ b/app/config/env-config.js @@ -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, diff --git a/app/src/server/index.js b/app/src/server/index.js index 0fa0c2f..701df39 100644 --- a/app/src/server/index.js +++ b/app/src/server/index.js @@ -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(); @@ -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("

API

"); + response.json(LIST_MODELS); } else { Helpers.error(response, `Ruta no encontrada ${path}`, "Not found"); } diff --git a/app/src/tools/Helpers.js b/app/src/tools/Helpers.js index 57c7fa6..c37fb29 100644 --- a/app/src/tools/Helpers.js +++ b/app/src/tools/Helpers.js @@ -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 * @@ -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 * @@ -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. *