diff --git a/cmd/new_module.go b/cmd/new_module.go index 075f6e6..57585de 100644 --- a/cmd/new_module.go +++ b/cmd/new_module.go @@ -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, } @@ -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{ @@ -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) diff --git a/templates/wesionary/project/domain/features/hello/controller.tmpl b/domain/hello/controller.go similarity index 100% rename from templates/wesionary/project/domain/features/hello/controller.tmpl rename to domain/hello/controller.go diff --git a/templates/wesionary/project/domain/features/hello/model.tmpl b/domain/hello/model.go similarity index 100% rename from templates/wesionary/project/domain/features/hello/model.tmpl rename to domain/hello/model.go diff --git a/templates/wesionary/project/domain/features/hello/module.tmpl b/domain/hello/module.go similarity index 100% rename from templates/wesionary/project/domain/features/hello/module.tmpl rename to domain/hello/module.go diff --git a/templates/wesionary/project/domain/features/hello/repository.tmpl b/domain/hello/repository.go similarity index 100% rename from templates/wesionary/project/domain/features/hello/repository.tmpl rename to domain/hello/repository.go diff --git a/domain/hello/route.go b/domain/hello/route.go new file mode 100644 index 0000000..e50db30 --- /dev/null +++ b/domain/hello/route.go @@ -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) +} diff --git a/templates/wesionary/project/domain/features/hello/service.tmpl b/domain/hello/service.go similarity index 100% rename from templates/wesionary/project/domain/features/hello/service.tmpl rename to domain/hello/service.go diff --git a/domain/middlewares/module.go b/domain/middlewares/module.go new file mode 100644 index 0000000..7d8f1c4 --- /dev/null +++ b/domain/middlewares/module.go @@ -0,0 +1,5 @@ +package middlewares + +import "go.uber.org/fx" + +var Module = fx.Module("domain-middlewares", fx.Options()) diff --git a/domain/module.go b/domain/module.go new file mode 100644 index 0000000..5e6609a --- /dev/null +++ b/domain/module.go @@ -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, +) diff --git a/domain/user/controller.go b/domain/user/controller.go new file mode 100644 index 0000000..254f093 --- /dev/null +++ b/domain/user/controller.go @@ -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}) +} diff --git a/domain/user/model.go b/domain/user/model.go new file mode 100644 index 0000000..daf1fe2 --- /dev/null +++ b/domain/user/model.go @@ -0,0 +1,5 @@ +package user + +type UserModel struct { + Message string +} diff --git a/domain/user/module.go b/domain/user/module.go new file mode 100644 index 0000000..ace3214 --- /dev/null +++ b/domain/user/module.go @@ -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), + ), +) diff --git a/domain/user/repository.go b/domain/user/repository.go new file mode 100644 index 0000000..8209728 --- /dev/null +++ b/domain/user/repository.go @@ -0,0 +1,12 @@ +package user + +type UserRepository struct { +} + +func NewUserRepository() *UserRepository { + return &UserRepository{} +} + +func (s *UserRepository) GetMessage() UserModel { + return UserModel{Message: "Hello World"} +} diff --git a/domain/user/route.go b/domain/user/route.go new file mode 100644 index 0000000..30e5e1d --- /dev/null +++ b/domain/user/route.go @@ -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) +} diff --git a/domain/user/service.go b/domain/user/service.go new file mode 100644 index 0000000..b2df7f0 --- /dev/null +++ b/domain/user/service.go @@ -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() +} diff --git a/pkg/utility/ast.go b/pkg/utility/ast.go index 03b9613..0d35e42 100644 --- a/pkg/utility/ast.go +++ b/pkg/utility/ast.go @@ -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, @@ -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"), + }...) } } } @@ -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 } diff --git a/templates/wesionary/project/domain/features/module.tmpl b/templates/wesionary/project/domain/features/module.tmpl deleted file mode 100644 index 6cdf014..0000000 --- a/templates/wesionary/project/domain/features/module.tmpl +++ /dev/null @@ -1,11 +0,0 @@ -package features - -import ( - "{{.ProjectModuleName}}/domain/features/hello" - - "go.uber.org/fx" -) - -var Module = fx.Module("features", - fx.Options(hello.Module), -) diff --git a/templates/wesionary/project/domain/hello/controller.tmpl b/templates/wesionary/project/domain/hello/controller.tmpl new file mode 100644 index 0000000..9230897 --- /dev/null +++ b/templates/wesionary/project/domain/hello/controller.tmpl @@ -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}) +} diff --git a/templates/wesionary/project/domain/hello/model.tmpl b/templates/wesionary/project/domain/hello/model.tmpl new file mode 100644 index 0000000..75aed10 --- /dev/null +++ b/templates/wesionary/project/domain/hello/model.tmpl @@ -0,0 +1,5 @@ +package hello + +type HelloModel struct { + Message string +} diff --git a/templates/wesionary/project/domain/hello/module.tmpl b/templates/wesionary/project/domain/hello/module.tmpl new file mode 100644 index 0000000..266b0c1 --- /dev/null +++ b/templates/wesionary/project/domain/hello/module.tmpl @@ -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), + ), +) diff --git a/templates/wesionary/project/domain/hello/repository.tmpl b/templates/wesionary/project/domain/hello/repository.tmpl new file mode 100644 index 0000000..3e4238a --- /dev/null +++ b/templates/wesionary/project/domain/hello/repository.tmpl @@ -0,0 +1,12 @@ +package hello + +type HelloRepository struct { +} + +func NewHelloRepository() *HelloRepository { + return &HelloRepository{} +} + +func (s *HelloRepository) GetMessage() HelloModel { + return HelloModel{Message: "Hello World"} +} diff --git a/templates/wesionary/project/domain/features/hello/route.tmpl b/templates/wesionary/project/domain/hello/route.tmpl similarity index 100% rename from templates/wesionary/project/domain/features/hello/route.tmpl rename to templates/wesionary/project/domain/hello/route.tmpl diff --git a/templates/wesionary/project/domain/hello/service.tmpl b/templates/wesionary/project/domain/hello/service.tmpl new file mode 100644 index 0000000..ca6edfa --- /dev/null +++ b/templates/wesionary/project/domain/hello/service.tmpl @@ -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() +} diff --git a/templates/wesionary/project/domain/module.tmpl b/templates/wesionary/project/domain/module.tmpl index 39a316d..b581ae8 100644 --- a/templates/wesionary/project/domain/module.tmpl +++ b/templates/wesionary/project/domain/module.tmpl @@ -1,7 +1,7 @@ package domain import ( - "{{.ProjectModuleName}}/domain/features" + "{{.ProjectModuleName}}/domain/hello" "{{.ProjectModuleName}}/domain/middlewares" "go.uber.org/fx" @@ -9,5 +9,5 @@ import ( var Module = fx.Options( middlewares.Module, - features.Module, + hello.Module, )