From 47d4149923c172d929a5c1899cbb30930f60fbe5 Mon Sep 17 00:00:00 2001 From: srgooglo Date: Tue, 5 Oct 2021 10:09:04 +0200 Subject: [PATCH] refactor controller class --- src/classes/Controller/index.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/classes/Controller/index.js b/src/classes/Controller/index.js index 1d0d8a3..6969599 100644 --- a/src/classes/Controller/index.js +++ b/src/classes/Controller/index.js @@ -1,15 +1,18 @@ class Controller { - constructor(key, exec, params) { - this.key = key ?? "controller" - this.params = { ...params } - - if (typeof exec === "function") { - this.exec = exec - } + constructor(payload) { + this.payload = {...payload} + + return this } - exec(req, res, next) { - res.json(`empty response`) + exec = async (req, res, next) => { + if (typeof this.payload.exec === "function") { + try { + await this.payload.exec (req, res, next) + } catch (error) { + return res.status(500).json({ error: error.message, endpoint: this.payload.route }) + } + } } }