From b36cf15b36779236a131bcae1c444ee2195e02bd Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Tue, 6 Jul 2021 19:29:49 -0700 Subject: [PATCH] injecting dependencies to controllers --- src/controllers/index.js | 13 +++++--- src/controllers/rest-api/index.js | 51 ++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/controllers/index.js b/src/controllers/index.js index 9bbf5c9..1692740 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -6,9 +6,6 @@ // Public npm libraries. -// Load the REST API Controllers. -const boilerplateRESTControllers = require('./rest-api') - // Load the Clean Architecture Adapters library const adapters = require('../adapters') @@ -19,6 +16,9 @@ const JSONRPC = require('./json-rpc') const UseCases = require('../use-cases') const useCases = new UseCases({ adapters }) +// Load the REST API Controllers. +const RESTControllers = require('./rest-api') + // Top-level function for this library. // Start the various Controllers and attach them to the app. async function attachControllers (app) { @@ -32,8 +32,13 @@ async function attachControllers (app) { } function attachRESTControllers (app) { + const rESTControllers = new RESTControllers({ + adapters, + useCases + }) + // Attach the REST API Controllers associated with the boilerplate code to the Koa app. - boilerplateRESTControllers.attachRESTControllers(app) + rESTControllers.attachRESTControllers(app) } // Add the JSON RPC router to the ipfs-coord adapter. diff --git a/src/controllers/rest-api/index.js b/src/controllers/rest-api/index.js index e328620..49ca36a 100644 --- a/src/controllers/rest-api/index.js +++ b/src/controllers/rest-api/index.js @@ -12,22 +12,45 @@ const UserRESTController = require('./users') const ContactRESTController = require('./contact') const LogsRESTController = require('./logs') -function attachRESTControllers (app) { - // Attach the REST API Controllers associated with the /auth route - const authRESTController = new AuthRESTController() - authRESTController.attach(app) +class RESTControllers { + constructor (localConfig) { + // Dependency Injection. + this.adapters = localConfig.adapters + if (!this.adapters) { + throw new Error( + 'Instance of Adapters library required when instantiating PostEntry REST Controller.' + ) + } + this.useCases = localConfig.useCases + if (!this.useCases) { + throw new Error( + 'Instance of Use Cases library required when instantiating PostEntry REST Controller.' + ) + } + } - // Attach the REST API Controllers associated with the /user route - const userRESTController = new UserRESTController() - userRESTController.attach(app) + attachRESTControllers (app) { + const dependencies = { + adapters: this.adapters, + useCases: this.useCases + } - // Attach the REST API Controllers associated with the /contact route - const contactRESTController = new ContactRESTController() - contactRESTController.attach(app) + // Attach the REST API Controllers associated with the /auth route + const authRESTController = new AuthRESTController(dependencies) + authRESTController.attach(app) - // Attach the REST API Controllers associated with the /logs route - const logsRESTController = new LogsRESTController() - logsRESTController.attach(app) + // Attach the REST API Controllers associated with the /user route + const userRESTController = new UserRESTController(dependencies) + userRESTController.attach(app) + + // Attach the REST API Controllers associated with the /contact route + const contactRESTController = new ContactRESTController(dependencies) + contactRESTController.attach(app) + + // Attach the REST API Controllers associated with the /logs route + const logsRESTController = new LogsRESTController(dependencies) + logsRESTController.attach(app) + } } -module.exports = { attachRESTControllers } +module.exports = RESTControllers