Skip to content

Commit

Permalink
refactor: create Service, Validator, Controller and Module structs (p…
Browse files Browse the repository at this point in the history
…art III)
  • Loading branch information
ralvarezdev committed Jan 25, 2025
1 parent 81fe5ca commit 27250d7
Showing 1 changed file with 29 additions and 34 deletions.
63 changes: 29 additions & 34 deletions http/factory/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ type (
ModuleWrapper interface {
Create(baseRouter gonethttproute.RouterWrapper) error
CreateSubmodule(submodule ModuleWrapper) error
CreateService() func() error
CreateController() func(baseRouter gonethttproute.RouterWrapper) error
CreateValidator() func() error
CreateService() func()
CreateController() func(baseRouter gonethttproute.RouterWrapper)
CreateValidator() func()
}

// Module is the struct for the route module
Expand All @@ -24,15 +24,28 @@ type (

// Create is a function that creates a new instance of the Module struct
func (m *Module) Create(baseRouter gonethttproute.RouterWrapper) error {
// Create the service, validator and controller structs
if err := m.CreateService()(); err != nil {
return err
// Create the service struct
m.CreateService()()

// Check if the service is nil
if m.Service == nil {
return ErrNilService
}
if err := m.CreateValidator()(); err != nil {
return err

// Create the validator struct
m.CreateValidator()()

// Check if the validator is nil
if m.Validator == nil {
return ErrNilValidator
}
if err := m.CreateController()(baseRouter); err != nil {
return err

// Create the controller struct
m.CreateController()(baseRouter)

// Check if the controller is nil
if m.Controller == nil {
return ErrNilController
}

// Register the controller routes and groups
Expand All @@ -47,34 +60,16 @@ func (m *Module) CreateSubmodule(submodule ModuleWrapper) error {
}

// CreateService is a function that creates a new instance of the Service struct
func (m *Module) CreateService() func() error {
return func() error {
// Check if the service is nil
if m.Service == nil {
return ErrNilService
}
return nil
}
func (m *Module) CreateService() func() {
return func() {}
}

// CreateValidator is a function that creates a new instance of the Validator struct
func (m *Module) CreateValidator() func() error {
return func() error {
// Check if the validator is nil
if m.Validator == nil {
return ErrNilValidator
}
return nil
}
func (m *Module) CreateValidator() func() {
return func() {}
}

// CreateController is a function that creates a new instance of the Controller struct
func (m *Module) CreateController() func(baseRouter gonethttproute.RouterWrapper) error {
return func(baseRouter gonethttproute.RouterWrapper) error {
// Check if the controller is nil
if m.Controller == nil {
return ErrNilController
}
return nil
}
func (m *Module) CreateController() func(baseRouter gonethttproute.RouterWrapper) {
return func(baseRouter gonethttproute.RouterWrapper) {}
}

0 comments on commit 27250d7

Please sign in to comment.