Skip to content

Commit 910c224

Browse files
committed
refactor: added the submodules as a parameter to the NewModule function
* Added the submodules as a parameter to the NewModule function * Now, submodules controllers router are being created dynamically on the module Create function
1 parent 099b19a commit 910c224

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

http/factory/module.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type (
1515
GetValidator() ValidatorWrapper
1616
GetController() ControllerWrapper
1717
GetPath() string
18+
GetSubmodules() *[]ModuleWrapper
1819
}
1920

2021
// Module is the struct for the route module
@@ -23,6 +24,7 @@ type (
2324
service ServiceWrapper
2425
validator ValidatorWrapper
2526
controller ControllerWrapper
27+
submodules []ModuleWrapper
2628
}
2729
)
2830

@@ -32,20 +34,34 @@ func NewModule(
3234
service ServiceWrapper,
3335
validator ValidatorWrapper,
3436
controller ControllerWrapper,
37+
submodules ...ModuleWrapper,
3538
) ModuleWrapper {
3639
return &Module{
3740
path: path,
3841
service: service,
3942
validator: validator,
4043
controller: controller,
44+
submodules: submodules,
4145
}
4246
}
4347

44-
// Create is a function that creates a new instance of the Module struct
48+
// Create is a function that creates the router for the controller and its submodules
4549
func (m *Module) Create(
4650
baseRouter gonethttproute.RouterWrapper,
4751
) error {
48-
return m.controller.CreateRouter(baseRouter, m.path)
52+
// Create the router for the controller
53+
if err := m.controller.CreateRouter(baseRouter, m.path); err != nil {
54+
return err
55+
}
56+
57+
// Create the submodules controllers router
58+
router := m.controller.GetRouter()
59+
for _, submodule := range m.submodules {
60+
if err := submodule.Create(router); err != nil {
61+
return err
62+
}
63+
}
64+
return nil
4965
}
5066

5167
// GetRouter is a function that returns the router
@@ -77,3 +93,8 @@ func (m *Module) GetValidator() ValidatorWrapper {
7793
func (m *Module) GetController() ControllerWrapper {
7894
return m.controller
7995
}
96+
97+
// GetSubmodules is a function that returns the submodules
98+
func (m *Module) GetSubmodules() *[]ModuleWrapper {
99+
return &m.submodules
100+
}

0 commit comments

Comments
 (0)