From 56f799be5b11e012ed10b11e27b2f29055d9ff37 Mon Sep 17 00:00:00 2001 From: mirza Date: Tue, 17 Jan 2023 05:13:13 +0700 Subject: [PATCH] update --- command/genapplication/README.md | 6 ++ command/gencontroller/README.md | 7 ++ command/gendomain/README.md | 20 ++++ command/gendomain/command.go | 100 ------------------ command/gengateway/README.md | 6 ++ command/genopenapi/command.go | 4 +- command/genrepository/README.md | 55 ++++++++++ command/genservice/README.md | 42 ++++++++ command/genusecase/README.md | 44 ++++++++ .../${entityname}api/interceptor._go | 2 +- .../usecase/${usecasename}/inport._go | 1 - .../usecase/${usecasename}/inport._go | 2 +- 12 files changed, 184 insertions(+), 105 deletions(-) create mode 100644 command/genapplication/README.md create mode 100644 command/gencontroller/README.md create mode 100644 command/gendomain/README.md create mode 100644 command/gengateway/README.md create mode 100644 command/genrepository/README.md create mode 100644 command/genservice/README.md create mode 100644 command/genusecase/README.md diff --git a/command/genapplication/README.md b/command/genapplication/README.md new file mode 100644 index 0000000..69a6c7f --- /dev/null +++ b/command/genapplication/README.md @@ -0,0 +1,6 @@ +# Gogen Application + +Call `gogen application myapp` will + +* create a folder and file application +* binding all together the controller usecase and gateway \ No newline at end of file diff --git a/command/gencontroller/README.md b/command/gencontroller/README.md new file mode 100644 index 0000000..e2e7f0b --- /dev/null +++ b/command/gencontroller/README.md @@ -0,0 +1,7 @@ +# Gogen Controller + +Call `gogen controller restapi` will + +* Create a controller folder +* Collect all the fields from InportRequest and InportResponse +* create an handler and router based on gogenrc diff --git a/command/gendomain/README.md b/command/gendomain/README.md new file mode 100644 index 0000000..26b10bb --- /dev/null +++ b/command/gendomain/README.md @@ -0,0 +1,20 @@ +# Gogen Domain + +Calling `gogen domain yourdomainname` will + +* Provide all those files + + ``` + ├── Dockerfile + ├── README.md + ├── config.json + ├── config.sample.json + ├── docker-compose.yml + ├── domain_yourdomainname + │ └── README.md + ├── go.mod + └── gitignore + ``` + +* Generate unique random secret key for your application in `config.json` +* Copy the basic template for controller, gateway and crud \ No newline at end of file diff --git a/command/gendomain/command.go b/command/gendomain/command.go index 1af5ae6..d377032 100644 --- a/command/gendomain/command.go +++ b/command/gendomain/command.go @@ -92,103 +92,3 @@ func Run(inputs ...string) error { return nil } - -//func insertNewDomainName(filePath, domainName string) error { -// -// f, err := os.Open(filePath) -// if err != nil { -// return err -// } -// defer func(f *os.File) { -// err := f.Close() -// if err != nil { -// -// } -// }(f) -// -// isEmptyFile := true -// -// fileContent := "" -// scanner := bufio.NewScanner(f) -// for scanner.Scan() { -// -// line := strings.TrimSpace(scanner.Text()) -// -// if line == "" { -// continue -// } -// -// isEmptyFile = false -// -// x := line -// if strings.HasPrefix(line, "-") { -// x = line[1:] -// } -// -// if x == domainName { -// return fmt.Errorf("domain name already exist") -// } -// -// fileContent += line -// fileContent += "\n" -// } -// if err := scanner.Err(); err != nil { -// return err -// } -// -// if isEmptyFile { -// fileContent += fmt.Sprintf("-%s", domainName) -// } else { -// fileContent += domainName -// } -// -// fileContent += "\n" -// -// return os.WriteFile(filePath, []byte(fileContent), 0644) -//} -// -//func Rewrite(srcDir, destDir string) { -// -// // Walk through the source directory and copy each file and subdirectory to the destination directory -// err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error { -// if err != nil { -// fmt.Println(err) -// return err -// } -// -// // Construct the destination path by replacing the source directory with the destination directory -// destPath := filepath.Join(destDir, path[len(srcDir):]) -// -// // If the current path is a directory, create it in the destination directory -// if info.IsDir() { -// os.MkdirAll(destPath, info.Mode()) -// return nil -// } -// -// // If the current path is a file, copy it to the destination directory -// srcFile, err := os.Open(path) -// if err != nil { -// fmt.Println(err) -// return err -// } -// defer srcFile.Close() -// -// destFile, err := os.Create(destPath) -// if err != nil { -// fmt.Println(err) -// return err -// } -// defer destFile.Close() -// -// _, err = io.Copy(destFile, srcFile) -// if err != nil { -// fmt.Println(err) -// return err -// } -// -// return nil -// }) -// if err != nil { -// return -// } -//} diff --git a/command/gengateway/README.md b/command/gengateway/README.md new file mode 100644 index 0000000..ab40308 --- /dev/null +++ b/command/gengateway/README.md @@ -0,0 +1,6 @@ +# Gogen Gateway + +* Read the Outport and collect all must have method +* Read the existing Gateway and collect all existing method +* Create a gateway folder and file then +* Create all non-existing must have method on Gateway based on gogenrc diff --git a/command/genopenapi/command.go b/command/genopenapi/command.go index 152d8bc..900146e 100644 --- a/command/genopenapi/command.go +++ b/command/genopenapi/command.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" "github.com/mirzaakhena/gogen/utils" - "io/ioutil" + "os" ) // ObjTemplate ... @@ -244,7 +244,7 @@ func Run(inputs ...string) error { return err } - err = ioutil.WriteFile(fmt.Sprintf("domain_%s/openapi.json", domainName), file, 0644) + err = os.WriteFile(fmt.Sprintf("domain_%s/openapi.json", domainName), file, 0644) if err != nil { return err } diff --git a/command/genrepository/README.md b/command/genrepository/README.md new file mode 100644 index 0000000..5537554 --- /dev/null +++ b/command/genrepository/README.md @@ -0,0 +1,55 @@ +# Gogen Repository + +Call `gogen repository SaveOrder Order RunOrderCreate` will + +* Create an entity Order (if not exist) + ``` + └── domain_yourdomainname + └── model + ├── entity + │ └── order.go + └── vo + └── order_id.go + ``` + +* Create repository SaveOrderRepo (if not exist) + ``` + └── domain_yourdomainname + └── model + └── repository + └── repository.go + ``` + +* Inject code into Outport + ``` + type Outport interface { + repository.SaveOrderRepo + } + ``` + +* Inject code into Interactor. It will replace the `//!` flag + ``` + func (r *runOrderCreateInteractor) Execute(ctx context.Context, req InportRequest) (*InportResponse, error) { + + res := &InportResponse{} + + // code your usecase definition here ... + + orderObj, err := entity.NewOrder(entity.OrderCreateRequest{}) + if err != nil { + return nil, err + } + + err = r.outport.SaveOrder(ctx, orderObj) + if err != nil { + return nil, err + } + + //! + + return res, nil + } + ``` + + + diff --git a/command/genservice/README.md b/command/genservice/README.md new file mode 100644 index 0000000..8dbf6ce --- /dev/null +++ b/command/genservice/README.md @@ -0,0 +1,42 @@ +# Gogen Service + +Call `gogen service PublishOrder RunOrderCreate` will + +* Create service `PublishOrderService` (if not exist) + ``` + └── domain_yourdomainname + └── model + └── service + └── service.go + ``` + +* Inject code into Outport + ``` + type Outport interface { + service.PublishOrderService + } + ``` + +* Inject code into Interactor. It will replace the `//!` flag + ``` + func (r *runOrderCreateInteractor) Execute(ctx context.Context, req InportRequest) (*InportResponse, error) { + + res := &InportResponse{} + + // code your usecase definition here ... + + publishOrderResponse, err := r.outport.PublishOrder(ctx, service.PublishOrderServiceRequest{}) + if err != nil { + return nil, err + } + + _ = publishOrderResponse + + //! + + return res, nil + } + ``` + + + diff --git a/command/genusecase/README.md b/command/genusecase/README.md new file mode 100644 index 0000000..84a07b3 --- /dev/null +++ b/command/genusecase/README.md @@ -0,0 +1,44 @@ +# Gogen Usecase + +Call `gogen usecase RunProductCreate` will + +* Create file Inport, Interactor and Outport + + ``` + └── domain_yourdomainname + └── usecase + └── runordercreate + ├── README.md + ├── inport.go + ├── interactor.go + └── outport.go + ``` + +* There are specialization for Inport. + + if using prefix `GetAll` then InportRequest and InportResponse will have + ```go + type InportRequest struct { + Page int + Size int + } + + type InportResponse struct { + Count int64 + Items []any + } + ``` + otherwise it will be empty + ```go + type InportRequest struct { + + } + + type InportResponse struct { + + } + ``` + + + + diff --git a/utils/templates/crud/gin/domain_${domainname}/controller/${entityname}api/interceptor._go b/utils/templates/crud/gin/domain_${domainname}/controller/${entityname}api/interceptor._go index f64e1c1..d10d93d 100644 --- a/utils/templates/crud/gin/domain_${domainname}/controller/${entityname}api/interceptor._go +++ b/utils/templates/crud/gin/domain_${domainname}/controller/${entityname}api/interceptor._go @@ -30,7 +30,7 @@ func (r *controller) authentication() gin.HandlerFunc { } } -func (r *ginController) authorization() gin.HandlerFunc { +func (r *controller) authorization() gin.HandlerFunc { return func(c *gin.Context) { diff --git a/utils/templates/usecase/getall/domain_${domainname}/usecase/${usecasename}/inport._go b/utils/templates/usecase/getall/domain_${domainname}/usecase/${usecasename}/inport._go index 5a8e4ca..414874d 100644 --- a/utils/templates/usecase/getall/domain_${domainname}/usecase/${usecasename}/inport._go +++ b/utils/templates/usecase/getall/domain_${domainname}/usecase/${usecasename}/inport._go @@ -1,7 +1,6 @@ package {{LowerCase .UsecaseName}} import ( - "context" "{{.PackagePath}}/shared/gogen" ) diff --git a/utils/templates/usecase/run/domain_${domainname}/usecase/${usecasename}/inport._go b/utils/templates/usecase/run/domain_${domainname}/usecase/${usecasename}/inport._go index 2af3bcd..efe1f6c 100644 --- a/utils/templates/usecase/run/domain_${domainname}/usecase/${usecasename}/inport._go +++ b/utils/templates/usecase/run/domain_${domainname}/usecase/${usecasename}/inport._go @@ -1,7 +1,7 @@ package {{LowerCase .UsecaseName}} import ( - "context" + "{{.PackagePath}}/shared/gogen" ) type Inport = gogen.Inport[InportRequest, InportResponse]