Skip to content

Commit

Permalink
refactor: added the submodules as a parameter to the NewModule function
Browse files Browse the repository at this point in the history
* Added the submodules as a parameter to the NewModule function
* Now, submodules controllers router are being created dynamically on the module Create function
  • Loading branch information
ralvarezdev committed Jan 25, 2025
1 parent 099b19a commit 910c224
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions http/factory/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type (
GetValidator() ValidatorWrapper
GetController() ControllerWrapper
GetPath() string
GetSubmodules() *[]ModuleWrapper
}

// Module is the struct for the route module
Expand All @@ -23,6 +24,7 @@ type (
service ServiceWrapper
validator ValidatorWrapper
controller ControllerWrapper
submodules []ModuleWrapper
}
)

Expand All @@ -32,20 +34,34 @@ func NewModule(
service ServiceWrapper,
validator ValidatorWrapper,
controller ControllerWrapper,
submodules ...ModuleWrapper,
) ModuleWrapper {
return &Module{
path: path,
service: service,
validator: validator,
controller: controller,
submodules: submodules,
}
}

// Create is a function that creates a new instance of the Module struct
// Create is a function that creates the router for the controller and its submodules
func (m *Module) Create(
baseRouter gonethttproute.RouterWrapper,
) error {
return m.controller.CreateRouter(baseRouter, m.path)
// Create the router for the controller
if err := m.controller.CreateRouter(baseRouter, m.path); err != nil {
return err
}

// Create the submodules controllers router
router := m.controller.GetRouter()
for _, submodule := range m.submodules {
if err := submodule.Create(router); err != nil {
return err
}
}
return nil
}

// GetRouter is a function that returns the router
Expand Down Expand Up @@ -77,3 +93,8 @@ func (m *Module) GetValidator() ValidatorWrapper {
func (m *Module) GetController() ControllerWrapper {
return m.controller
}

// GetSubmodules is a function that returns the submodules
func (m *Module) GetSubmodules() *[]ModuleWrapper {
return &m.submodules
}

0 comments on commit 910c224

Please sign in to comment.