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 IV)
  • Loading branch information
ralvarezdev committed Jan 25, 2025
1 parent 27250d7 commit 6166b0d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
29 changes: 29 additions & 0 deletions http/factory/controller.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
package factory

import (
"github.com/goccy/go-json"
gonethttproute "github.com/ralvarezdev/go-net/http/route"
)

type (
// ControllerWrapper is the interface for the route controller
ControllerWrapper interface {
Create(baseRouter gonethttproute.RouterWrapper, path string) error
RegisterRoutes()
RegisterGroups()
gonethttproute.RouterWrapper
}

// Controller is the struct for the route controller
Controller struct {
gonethttproute.RouterWrapper
}

// Create creates the controller
func (c *Controller) Create(baseRouter gonethttproute.RouterWrapper, path string) error {
// Check if the base route is nil
if baseRouter == nil {
return gonethttproute.ErrNilRouter
}

// Set the base route
c.RouterWrapper = baseRouter.NewGroup(path)

// Register the controller routes and groups
c.RegisterRoutes()
c.RegisterGroups()
return nil
}

// RegisterRoutes registers the routes
func (c *Controller) RegisterRoutes() {}

// RegisterGroups registers the groups
func (c *Controller) RegisterGroups() {}
)
71 changes: 21 additions & 50 deletions http/factory/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import (
type (
// ModuleWrapper is the interface for the route module
ModuleWrapper interface {
Create(baseRouter gonethttproute.RouterWrapper) error
CreateSubmodule(submodule ModuleWrapper) error
CreateService() func()
CreateController() func(baseRouter gonethttproute.RouterWrapper)
CreateValidator() func()
Create(baseRouter gonethttproute.RouterWrapper, path string) error
CreateSubmodule(submodule ModuleWrapper, path string) error
}

// Module is the struct for the route module
Expand All @@ -22,54 +19,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 struct
m.CreateService()()

// Check if the service is nil
if m.Service == nil {
return ErrNilService
}

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

// Check if the validator is nil
if m.Validator == nil {
return ErrNilValidator
}

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

// Check if the controller is nil
if m.Controller == nil {
return ErrNilController
// NewModule is a function that creates a new instance of the Module struct
func NewModule(
service ServiceWrapper,
validator ValidatorWrapper,
controller ControllerWrapper,
) ModuleWrapper {
return &Module{
Service: service,
Validator: validator,
Controller: controller,
}

// Register the controller routes and groups
m.Controller.RegisterRoutes()
m.Controller.RegisterGroups()
return nil
}

// CreateSubmodule is a function that creates a new submodule of the Module struct
func (m *Module) CreateSubmodule(submodule ModuleWrapper) error {
return submodule.Create(m.Controller)
}

// CreateService is a function that creates a new instance of the Service struct
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() {
return func() {}
// Create is a function that creates a new instance of the Module struct
func (m *Module) Create(
baseRouter gonethttproute.RouterWrapper,
path string,
) error {
return m.Controller.Create(baseRouter, path)
}

// CreateController is a function that creates a new instance of the Controller struct
func (m *Module) CreateController() func(baseRouter gonethttproute.RouterWrapper) {
return func(baseRouter gonethttproute.RouterWrapper) {}
// CreateSubmodule is a function that creates a new submodule of the Module struct
func (m *Module) CreateSubmodule(submodule ModuleWrapper, path string) error {
return submodule.Create(m.Controller, path)
}

0 comments on commit 6166b0d

Please sign in to comment.