Skip to content

Commit

Permalink
feat: invoke RegisterRoute instead of invoking NewRoute in module
Browse files Browse the repository at this point in the history
refactor: change names of controller, service, route, module inside a domain
  • Loading branch information
mukezhz committed Jan 31, 2024
1 parent 14d9a80 commit e737176
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 56 deletions.
10 changes: 5 additions & 5 deletions templates/wesionary/module/controller.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"github.com/gin-gonic/gin"
)

type {{.ModuleName}}Controller struct {
service *{{.ModuleName}}Service
type Controller struct {
service *Service
}

func New{{.ModuleName}}Controller(service *{{.ModuleName}}Service) *{{.ModuleName}}Controller {
return &{{.ModuleName}}Controller{service: service}
func NewController(service *Service) *Controller {
return &Controller{service: service}
}

func (ctrl *{{.ModuleName}}Controller) HandleRoot(c *gin.Context) {
func (ctrl *Controller) HandleRoot(c *gin.Context) {
message := ctrl.service.GetMessage()
c.JSON(http.StatusOK, gin.H{"message": message.Message})
}
2 changes: 1 addition & 1 deletion templates/wesionary/module/model.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package {{.PackageName}}

type {{.ModuleName}}Model struct {
type Model struct {
Message string
}
9 changes: 5 additions & 4 deletions templates/wesionary/module/module.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
var Module = fx.Module("{{.PackageName}}",
fx.Options(
fx.Provide(
New{{.ModuleName}}Service,
New{{.ModuleName}}Controller,
New{{.ModuleName}}Repository,
NewRepository,
NewService,
NewController,
NewRoute,
),
fx.Invoke(New{{.ModuleName}}Route),
fx.Invoke(RegisterRoute),
),
)
10 changes: 5 additions & 5 deletions templates/wesionary/module/repository.tmpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package {{.PackageName}}

type {{.ModuleName}}Repository struct {
type Repository struct {
}

func New{{.ModuleName}}Repository() *{{.ModuleName}}Repository {
return &{{.ModuleName}}Repository{}
func NewRepository() *Repository {
return &Repository{}
}

func (s *{{.ModuleName}}Repository) GetMessage() {{.ModuleName}}Model {
return {{.ModuleName}}Model{Message: "Hello World"}
func (s *Repository) GetMessage() Model {
return Model{Message: "Hello World"}
}
11 changes: 5 additions & 6 deletions templates/wesionary/module/route.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import (
"{{.ProjectModuleName}}/pkg/infrastructure"
)

type {{.ModuleName}}Route struct {
type Route struct {
router *infrastructure.Router
controller *{{.ModuleName}}Controller
controller *Controller
groupRouter *gin.RouterGroup
}

func New{{.ModuleName}}Route(router *infrastructure.Router, controller *{{.ModuleName}}Controller) *{{.ModuleName}}Route {
route := {{.ModuleName}}Route{router: router, controller: controller}
func NewRoute(router *infrastructure.Router, controller *Controller) *Route {
route := Route{router: router, controller: controller}
route.groupRouter = router.Group("api/{{.PackageName}}")
route.RegisterHelloRoutes()
return &route
}

func (r *{{.ModuleName}}Route) RegisterHelloRoutes() {
func RegisterRoute(r *Route) {
r.groupRouter.GET("", r.controller.HandleRoot)
}
14 changes: 7 additions & 7 deletions templates/wesionary/module/service.tmpl
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package {{.PackageName}}

// {{.ModuleName}}Service handles the business logic of the {{.ModuleName}} module
type {{.ModuleName}}Service struct {
// Service handles the business logic of the module
type Service struct {
// Add any dependencies here
repo *{{.ModuleName}}Repository
repo *Repository
}

// New{{.ModuleName}}Service creates a new instance of TestService
func New{{.ModuleName}}Service(repo *{{.ModuleName}}Repository) *{{.ModuleName}}Service {
return &{{.ModuleName}}Service{
// NewService creates a new instance of TestService
func NewService(repo *Repository) *Service {
return &Service{
repo: repo,
}
}

// GetMessage returns a greeting message
func (s *{{.ModuleName}}Service) GetMessage() {{.ModuleName}}Model {
func (s *Service) GetMessage() Model {
return s.repo.GetMessage()
}
10 changes: 5 additions & 5 deletions templates/wesionary/project/domain/hello/controller.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"github.com/gin-gonic/gin"
)

type HelloController struct {
service *HelloService
type Controller struct {
service *Service
}

func NewHelloController(service *HelloService) *HelloController {
return &HelloController{service: service}
func NewController(service *Service) *Controller {
return &Controller{service: service}
}

func (ctrl *HelloController) HandleRoot(c *gin.Context) {
func (ctrl *Controller) HandleRoot(c *gin.Context) {
message := ctrl.service.GetMessage()
c.JSON(http.StatusOK, gin.H{"message": message.Message})
}
2 changes: 1 addition & 1 deletion templates/wesionary/project/domain/hello/model.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package hello

type HelloModel struct {
type Model struct {
Message string
}
9 changes: 5 additions & 4 deletions templates/wesionary/project/domain/hello/module.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
var Module = fx.Module("hello",
fx.Options(
fx.Provide(
NewHelloService,
NewHelloController,
NewHelloRepository,
NewService,
NewController,
NewRepository,
NewRoute,
),
fx.Invoke(NewHelloRoute),
fx.Invoke(RegisterRoute),
),
)
10 changes: 5 additions & 5 deletions templates/wesionary/project/domain/hello/repository.tmpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package hello

type HelloRepository struct {
type Repository struct {
}

func NewHelloRepository() *HelloRepository {
return &HelloRepository{}
func NewRepository() *Repository {
return &Repository{}
}

func (s *HelloRepository) GetMessage() HelloModel {
return HelloModel{Message: "Hello World"}
func (s *Repository) GetMessage() Model {
return Model{Message: "Hello World"}
}
11 changes: 5 additions & 6 deletions templates/wesionary/project/domain/hello/route.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import (
"{{.ProjectModuleName}}/pkg/infrastructure"
)

type HelloRoute struct {
type Route struct {
router *infrastructure.Router
controller *HelloController
controller *Controller
groupRouter *gin.RouterGroup
}

func NewHelloRoute(router *infrastructure.Router, controller *HelloController) *HelloRoute {
route := HelloRoute{router: router, controller: controller}
func NewRoute(router *infrastructure.Router, controller *Controller) *Route {
route := Route{router: router, controller: controller}
route.groupRouter = route.router.Group("api/hello")
route.RegisterHelloRoutes()
return &route
}

func (r *HelloRoute) RegisterHelloRoutes() {
func RegisterRoute(r *Route) {
r.groupRouter.GET("", r.controller.HandleRoot)
}
14 changes: 7 additions & 7 deletions templates/wesionary/project/domain/hello/service.tmpl
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package hello

// HelloService handles the business logic of the Hello module
type HelloService struct {
// Service handles the business logic of the module
type Service struct {
// Add any dependencies here
repo *HelloRepository
repo *Repository
}

// NewHelloService creates a new instance of TestService
func NewHelloService(repo *HelloRepository) *HelloService {
return &HelloService{
// NewService creates a new instance of TestService
func NewService(repo *Repository) *Service {
return &Service{
repo: repo,
}
}

// GetMessage returns a greeting message
func (s *HelloService) GetMessage() HelloModel {
func (s *Service) GetMessage() Model {
return s.repo.GetMessage()
}

0 comments on commit e737176

Please sign in to comment.