Skip to content

Commit

Permalink
refactor: remove features/ from domain/
Browse files Browse the repository at this point in the history
  • Loading branch information
mukezhz committed Jan 11, 2024
1 parent 9224da6 commit 48059c6
Show file tree
Hide file tree
Showing 24 changed files with 218 additions and 30 deletions.
8 changes: 4 additions & 4 deletions cmd/new_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

var newModuleCmd = &cobra.Command{
Use: "gen features [name]",
Short: "Create a new features",
Use: "gen module [name]",
Short: "Create a new domain",
Args: cobra.MaximumNArgs(2),
Run: createModule,
}
Expand All @@ -35,7 +35,7 @@ func createModule(_ *cobra.Command, args []string) {
fmt.Println("Error finding Git root:", err)
return
}
mainModulePath := filepath.Join(projectPath, "domain", "features", "module.go")
mainModulePath := filepath.Join(projectPath, "domain", "module.go")
var moduleName string
if len(args) == 1 {
questions := []terminal.ProjectQuestion{
Expand All @@ -59,7 +59,7 @@ func createModule(_ *cobra.Command, args []string) {
data := utility.GetModuleDataFromModuleName(moduleName, projectModule.Module, projectModule.GoVersion)

// Define the directory structure
targetRoot := filepath.Join(".", "domain", "features", data.PackageName)
targetRoot := filepath.Join(".", "domain", data.PackageName)
templatePath := filepath.Join(".", "templates", "wesionary", "module")

err = utility.GenerateFiles(templatesFS, templatePath, targetRoot, data)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions domain/hello/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package hello

import (
"github.com/gin-gonic/gin"
"test/pkg/infrastructure"
)

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

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

func (r *HelloRoute) RegisterHelloRoutes() {
r.groupRouter.GET("", r.controller.HandleRoot)
}
File renamed without changes.
5 changes: 5 additions & 0 deletions domain/middlewares/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package middlewares

import "go.uber.org/fx"

var Module = fx.Module("domain-middlewares", fx.Options())
13 changes: 13 additions & 0 deletions domain/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package domain

import (
"test/domain/hello"
"test/domain/middlewares"

"go.uber.org/fx"
)

var Module = fx.Options(
middlewares.Module,
hello.Module,
)
19 changes: 19 additions & 0 deletions domain/user/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package user

import (
"net/http"
"github.com/gin-gonic/gin"
)

type UserController struct {
service *UserService
}

func NewUserController(service *UserService) *UserController {
return &UserController{service: service}
}

func (ctrl *UserController) HandleRoot(c *gin.Context) {
message := ctrl.service.GetMessage()
c.JSON(http.StatusOK, gin.H{"message": message.Message})
}
5 changes: 5 additions & 0 deletions domain/user/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package user

type UserModel struct {
Message string
}
16 changes: 16 additions & 0 deletions domain/user/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package user

import (
"go.uber.org/fx"
)

var Module = fx.Module("user",
fx.Options(
fx.Provide(
NewUserService,
NewUserController,
NewUserRepository,
),
fx.Invoke(NewUserRoute),
),
)
12 changes: 12 additions & 0 deletions domain/user/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package user

type UserRepository struct {
}

func NewUserRepository() *UserRepository {
return &UserRepository{}
}

func (s *UserRepository) GetMessage() UserModel {
return UserModel{Message: "Hello World"}
}
23 changes: 23 additions & 0 deletions domain/user/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package user

import (
"github.com/gin-gonic/gin"
"github.com/mukezhz/geng/pkg/infrastructure"
)

type UserRoute struct {
router *infrastructure.Router
controller *UserController
groupRouter *gin.RouterGroup
}

func NewUserRoute(router *infrastructure.Router, controller *UserController) *UserRoute {
route := UserRoute{router: router, controller: controller}
route.groupRouter = router.Group("api/user")
route.RegisterHelloRoutes()
return &route
}

func (r *UserRoute) RegisterHelloRoutes() {
r.groupRouter.GET("", r.controller.HandleRoot)
}
19 changes: 19 additions & 0 deletions domain/user/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package user

// UserService handles the business logic of the User module
type UserService struct {
// Add any dependencies here
repo *UserRepository
}

// NewUserService creates a new instance of TestService
func NewUserService(repo *UserRepository) *UserService {
return &UserService{
repo: repo,
}
}

// GetMessage returns a greeting message
func (s *UserService) GetMessage() UserModel {
return s.repo.GetMessage()
}
19 changes: 6 additions & 13 deletions pkg/utility/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func ImportPackage(node *ast.File, projectModule, packageName string) {
path := filepath.Join(projectModule, "domain", "features", packageName)
path := filepath.Join(projectModule, "domain", packageName)
importSpec := &ast.ImportSpec{
Path: &ast.BasicLit{
Kind: token.STRING,
Expand Down Expand Up @@ -59,17 +59,10 @@ func AddAnotherFxOptionsInModule(path, module, projectModule string) string {
switch x := n.(type) {
case *ast.CallExpr:
if sel, ok := x.Fun.(*ast.SelectorExpr); ok {
if sel.Sel.Name == "Module" {
x.Args = append(x.Args, &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("fx"),
Sel: ast.NewIdent("Options"),
},
Args: []ast.Expr{
ast.NewIdent(module + ".Module"),
},
Rparen: token.Pos(1),
})
if sel.Sel.Name == "Options" {
x.Args = append(x.Args, []ast.Expr{
ast.NewIdent(module + ".Module"),
}...)
}
}
}
Expand All @@ -82,7 +75,7 @@ func AddAnotherFxOptionsInModule(path, module, projectModule string) string {
fmt.Println(err)
}
formattedCode := buf.String()
providerToInsert := fmt.Sprintf("fx.Options(%v.Module),", module)
providerToInsert := fmt.Sprintf("%v.Module,", module)
formattedCode = strings.Replace(formattedCode, providerToInsert, "\n\t"+providerToInsert, 1)
return formattedCode
}
Expand Down
11 changes: 0 additions & 11 deletions templates/wesionary/project/domain/features/module.tmpl

This file was deleted.

19 changes: 19 additions & 0 deletions templates/wesionary/project/domain/hello/controller.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package hello

import (
"net/http"
"github.com/gin-gonic/gin"
)

type HelloController struct {
service *HelloService
}

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

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

type HelloModel struct {
Message string
}
16 changes: 16 additions & 0 deletions templates/wesionary/project/domain/hello/module.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package hello

import (
"go.uber.org/fx"
)

var Module = fx.Module("hello",
fx.Options(
fx.Provide(
NewHelloService,
NewHelloController,
NewHelloRepository,
),
fx.Invoke(NewHelloRoute),
),
)
12 changes: 12 additions & 0 deletions templates/wesionary/project/domain/hello/repository.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hello

type HelloRepository struct {
}

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

func (s *HelloRepository) GetMessage() HelloModel {
return HelloModel{Message: "Hello World"}
}
19 changes: 19 additions & 0 deletions templates/wesionary/project/domain/hello/service.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package hello

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

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

// GetMessage returns a greeting message
func (s *HelloService) GetMessage() HelloModel {
return s.repo.GetMessage()
}
4 changes: 2 additions & 2 deletions templates/wesionary/project/domain/module.tmpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package domain

import (
"{{.ProjectModuleName}}/domain/features"
"{{.ProjectModuleName}}/domain/hello"
"{{.ProjectModuleName}}/domain/middlewares"

"go.uber.org/fx"
)

var Module = fx.Options(
middlewares.Module,
features.Module,
hello.Module,
)

0 comments on commit 48059c6

Please sign in to comment.