UNPKG

yekonga-server

Version:
102 lines (86 loc) 4.01 kB
const H = Yekonga.Helper; class ControllerResolver { params; query; className; classVariable; singularVariable; pluralVariable; methodVariable; secondaryKey; uuid; input; action; constructor(req, res) { this.defaultMethods = ['create', 'update', 'import', 'delete', 'download', 'paginate']; this.params = req.params; this.query = req.query; this.input = req.body; if (this.params.class) { this.className = H.getClass(this.params.class); this.secondaryKey = H.getVariable(`${this.className}Id`); this.classVariable = H.getVariable(this.params.class); this.singularVariable = H.toSingular(this.classVariable); this.pluralVariable = H.toPlural(this.classVariable); } if (this.params.method) { this.methodVariable = H.getVariable(this.params.method); } this.action = this.methodVariable; this.uuid = this.params.uuid; if (!this.defaultMethods.includes(this.methodVariable)) { if (this.pluralVariable == this.classVariable && !this.methodVariable) { this.action = 'list'; } else if (this.singularVariable == this.classVariable && this.methodVariable && !this.uuid) { this.action = 'show'; this.uuid = this.params.method; } } } async execute() { var data = { error: "Empty" }; var method = null; var parent = {}, params = { where: {} }, context = {}; if (this.uuid) { params.where[this.secondaryKey] = { equalTo: this.uuid } } return data; try { if (this.action == 'list') { method = H.getVariable(`${this.pluralVariable}`); data = await Yekonga.Graphql.Resolvers.Query[method](parent, params, context); } else if (this.action == 'paginate') { method = H.getVariable(`${this.className}Paginate`); data = await Yekonga.Graphql.Resolvers.Query[method](parent, params, context); } else if (this.action == 'create') { if (this.input) params.input = [this.input]; method = H.getVariable(`create${this.className}`); data = await Yekonga.Graphql.Resolvers.Mutation[method](parent, params, context); } else if (this.action == 'update') { if (this.input) params.input = this.input; method = H.getVariable(`update${H.toPlural(this.className)}`); data = await Yekonga.Graphql.Resolvers.Mutation[method](parent, params, context); } else if (this.action == 'import') { if (this.input) params.input = this.input; method = H.getVariable(`import${this.className}`); data = await Yekonga.Graphql.Resolvers.Mutation[method](parent, params, context); } else if (this.action == 'delete') { method = H.getVariable(`delete${this.className}`); data = await Yekonga.Graphql.Resolvers.Mutation[method](parent, params, context); } else if (this.action == 'download') { method = H.getVariable(`download${this.className}`); data = await Yekonga.Graphql.Resolvers.Query[method](parent, params, context); } else if (this.action == 'show') { method = H.getVariable(`${this.className}`); data = await Yekonga.Graphql.Resolvers.Query[method](parent, params, context); } } catch (error) { console.error(`Rest-api.ControllerResolver ${method} / ${this.className} / ${this.action} / ${this.uuid}`) console.error(error) } return data; } } module.exports = ControllerResolver;