From 68192a50f48124531900bead95e90ec4130b5262 Mon Sep 17 00:00:00 2001 From: Willy Kristianto Date: Wed, 25 Sep 2024 14:50:23 +0700 Subject: [PATCH 1/2] fixing naming service name --- README.md | 6 +++--- cmd/candi/cli_stage.go | 4 ++-- cmd/candi/template_delivery_worker.go | 2 +- cmd/candi/template_domain.go | 4 ++-- cmd/candi/template_etc.go | 2 +- codebase/app/redis_worker/README.md | 13 +++++++++++-- 6 files changed, 20 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ea3058e0..d0305d89 100644 --- a/README.md +++ b/README.md @@ -101,9 +101,9 @@ $ make run If include GRPC handler, run `$ make proto` for generate rpc files from proto (must install `protoc` compiler min version `libprotoc 3.14.0`) ## Server handlers example: -* [**Example REST API in delivery layer**](https://github.com/golangid/backend-microservices/tree/master/services/user-service/internal/modules/auth/delivery/resthandler) -* [**Example gRPC in delivery layer**](https://github.com/golangid/backend-microservices/blob/master/services/storage-service/internal/modules/storage/delivery/grpchandler/grpchandler.go) -* [**Example GraphQL in delivery layer**](https://github.com/golangid/backend-microservices/tree/master/services/user-service/internal/modules/auth/delivery/graphqlhandler) +* [**Example REST API in delivery layer**](https://github.com/agungdwiprasetyo/backend-microservices/tree/master/services/user-service/internal/modules/auth/delivery/resthandler/resthandler.go) +* [**Example gRPC in delivery layer**](https://github.com/agungdwiprasetyo/backend-microservices/blob/master/services/storage-service/internal/modules/storage/delivery/grpchandler/grpchandler.go) +* [**Example GraphQL in delivery layer**](https://github.com/agungdwiprasetyo/backend-microservices/tree/master/services/user-service/internal/modules/auth/delivery/graphqlhandler) ## Worker handlers example: * [**Example Cron worker in delivery layer**](https://github.com/golangid/candi/tree/master/codebase/app/cron_worker) (Static Scheduler) diff --git a/cmd/candi/cli_stage.go b/cmd/candi/cli_stage.go index 7a3a9cec..d6f661f1 100644 --- a/cmd/candi/cli_stage.go +++ b/cmd/candi/cli_stage.go @@ -10,7 +10,7 @@ import ( ) func cliStageInputServiceName() (serviceName string) { - serviceName = readInput("Please input service name:") + serviceName = candihelper.ToDelimited(readInput("Please input service name:"), '-') _, err := os.Stat(serviceName) var errMessage string if strings.TrimSpace(serviceName) == "" { @@ -42,7 +42,7 @@ func inputOwnerName() (ownerName string) { func cliStageInputExistingServiceName(prefixPath, cmd string) string { stageInputServiceName: - serviceName := readInput(cmd) + serviceName := candihelper.ToDelimited(readInput(cmd), '-') _, err := os.Stat(filepath.Join(prefixPath, serviceName)) var errMessage string if strings.TrimSpace(serviceName) == "" { diff --git a/cmd/candi/template_delivery_worker.go b/cmd/candi/template_delivery_worker.go index f3d6d3f3..f1f0b21b 100644 --- a/cmd/candi/template_delivery_worker.go +++ b/cmd/candi/template_delivery_worker.go @@ -249,7 +249,7 @@ func NewPostgresListenerHandler(uc usecase.Usecase, deps dependency.Dependency) // MountHandlers mount handler group func (h *PostgresListenerHandler) MountHandlers(group *types.WorkerHandlerGroup) { - group.Add("{{snake .ModuleName}}s", h.handleDataChangeOn{{upper (camel .ModuleName)}}) // listen data change on table "{{.ModuleName}}s" + group.Add("{{plural .ModuleName}}", h.handleDataChangeOn{{upper (camel .ModuleName)}}) // listen data change on table "{{plural .ModuleName}}" } func (h *PostgresListenerHandler) handleDataChangeOn{{upper (camel .ModuleName)}}(eventContext *candishared.EventContext) error { diff --git a/cmd/candi/template_domain.go b/cmd/candi/template_domain.go index d5d7f184..da87531b 100644 --- a/cmd/candi/template_domain.go +++ b/cmd/candi/template_domain.go @@ -19,12 +19,12 @@ type {{upper (camel .ModuleName)}} struct { {{if .SQLUseGORM}} // TableName return table name of {{upper (camel .ModuleName)}} model func ({{upper (camel .ModuleName)}}) TableName() string { - return "{{snake .ModuleName}}s" + return "{{plural .ModuleName}}" }{{end}} {{if or .MongoDeps .ArangoDeps}} // CollectionName return collection name of {{upper (camel .ModuleName)}} model func ({{upper (camel .ModuleName)}}) CollectionName() string { - return "{{snake .ModuleName}}s" + return "{{plural .ModuleName}}" }{{end}} ` templateModuleDomain = `package domain diff --git a/cmd/candi/template_etc.go b/cmd/candi/template_etc.go index d51e1d40..da8eaae6 100644 --- a/cmd/candi/template_etc.go +++ b/cmd/candi/template_etc.go @@ -304,7 +304,7 @@ $ make docker "$ candi -init\n" + "```\n" + ` -### If include GRPC handler, run this command (must install ` + "`protoc`" + ` compiler min version ` + "`libprotoc` 3.14.0`" + `) +### If include GRPC handler, run this command (must install ` + "`protoc`" + ` compiler min version ` + "`libprotoc 3.14.0`" + `) ` + "```" + ` $ make proto service={{service_name}} diff --git a/codebase/app/redis_worker/README.md b/codebase/app/redis_worker/README.md index 4d930ffd..aee87d14 100644 --- a/codebase/app/redis_worker/README.md +++ b/codebase/app/redis_worker/README.md @@ -84,7 +84,16 @@ import ( func (uc *usecaseImpl) someUsecase(ctx context.Context) { // scheduled exec to "scheduled-push-notif" handler after 5 minutes from now - key := candihelper.BuildRedisPubSubKeyTopic("scheduled-push-notif", map[string]string{"message": "hello"}) - uc.cache.Set(ctx, key, "ok", 5*time.Minute) + + timerDuration, _ := time.ParseDuration("5m") + message := "hello" + job := candishared.PublisherArgument{ + Topic: "foobar", + Key: "foo_007", + Message: candihelper.ToBytes(message), + Delay: timerDuration, + IsDeleteMessage: true, + } + uc.deps.GetBroker(types.RedisSubscriber).GetPublisher().PublishMessage(ctx, &job) } ``` \ No newline at end of file From 86c13f0e994479d13fa10fbd6fc10bbad88d3498 Mon Sep 17 00:00:00 2001 From: Willy Kristianto Date: Wed, 25 Sep 2024 15:32:59 +0700 Subject: [PATCH 2/2] fixing naming table name --- cmd/candi/main.go | 4 +++- cmd/candi/template_repository.go | 12 ++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/cmd/candi/main.go b/cmd/candi/main.go index 9027eb30..fe4809e8 100644 --- a/cmd/candi/main.go +++ b/cmd/candi/main.go @@ -8,6 +8,8 @@ import ( "os" "runtime" "text/template" + + "github.com/golangid/candi/candihelper" ) func main() { @@ -104,7 +106,7 @@ func selectScope(flagParam flagParameter, scope string) { case InitMonorepo: // 1 logger.Printf("\033[1mPlease input monorepo project name (enter for default):\033[0m") fmt.Printf(">> ") - cmdInput := readInput() + cmdInput := candihelper.ToDelimited(readInput(), '-') if cmdInput != "" { flagParam.monorepoProjectName = cmdInput } diff --git a/cmd/candi/template_repository.go b/cmd/candi/template_repository.go index b840390e..ab334a0e 100644 --- a/cmd/candi/template_repository.go +++ b/cmd/candi/template_repository.go @@ -621,7 +621,7 @@ func (r *{{camel .ModuleName}}RepoSQL) FetchAll(ctx context.Context, filter *dom if len(args) > 0 { where = " WHERE " + where } - query := fmt.Sprintf("SELECT id, field, created_at, updated_at FROM {{snake .ModuleName}}s%s ORDER BY %s %s LIMIT %d OFFSET %d", + query := fmt.Sprintf("SELECT id, field, created_at, updated_at FROM {{plural .ModuleName}}%s ORDER BY %s %s LIMIT %d OFFSET %d", where, filter.OrderBy, filter.Sort, filter.Limit, filter.CalculateOffset()) trace.Log("query", query) rows, err := r.readDB.Query(query, args...) @@ -650,7 +650,7 @@ func (r *{{camel .ModuleName}}RepoSQL) Count(ctx context.Context, filter *domain if len(args) > 0 { where = " WHERE " + where } - query := "SELECT COUNT(*) FROM {{snake .ModuleName}}s" + where + query := "SELECT COUNT(*) FROM {{plural .ModuleName}}" + where r.readDB.QueryRow(query, args...).Scan(&count) trace.Log("query", query) trace.Log("args", args){{end}} @@ -664,7 +664,7 @@ func (r *{{camel .ModuleName}}RepoSQL) Find(ctx context.Context, filter *domain. {{if .SQLUseGORM}}err = r.setFilter{{upper (camel .ModuleName)}}({{ if .IsMonorepo }}global{{end}}shared.SetSpanToGorm(ctx, r.readDB), filter).First(&result).Error {{else}}where, args := r.setFilter{{upper (camel .ModuleName)}}(filter) - query := "SELECT id, field, created_at, updated_at FROM {{snake .ModuleName}}s WHERE " + where + " LIMIT 1" + query := "SELECT id, field, created_at, updated_at FROM {{plural .ModuleName}} WHERE " + where + " LIMIT 1" trace.Log("query", query) trace.Log("args", args) err = r.readDB.QueryRow(query, args...). @@ -697,7 +697,7 @@ func (r *{{camel .ModuleName}}RepoSQL) Save(ctx context.Context, data *shareddom data.CreatedAt = time.Now() } if data.ID == 0 { - query = "INSERT INTO {{snake .ModuleName}}s (field, created_at, updated_at) VALUES ({{if eq .SQLDriver "postgres"}}$1,$2,$3{{else}}?,?,?{{end}})" + query = "INSERT INTO {{plural .ModuleName}} (field, created_at, updated_at) VALUES ({{if eq .SQLDriver "postgres"}}$1,$2,$3{{else}}?,?,?{{end}})" args = []interface{}{data.Field, data.CreatedAt, data.UpdatedAt} } else { var updatedFields []string{{if eq .SQLDriver "postgres"}} @@ -707,7 +707,7 @@ func (r *{{camel .ModuleName}}RepoSQL) Save(ctx context.Context, data *shareddom updatedFields = append(updatedFields, {{if eq .SQLDriver "postgres"}}fmt.Sprintf("%s=$%d", field, i)) i++{{else}}fmt.Sprintf("%s=?", field)){{end}} } - query = fmt.Sprintf("UPDATE {{snake .ModuleName}}s SET %s WHERE id={{if eq .SQLDriver "postgres"}}$%d", strings.Join(updatedFields, ", "), i){{else}}?", strings.Join(updatedFields, ", ")){{end}} + query = fmt.Sprintf("UPDATE {{plural .ModuleName}} SET %s WHERE id={{if eq .SQLDriver "postgres"}}$%d", strings.Join(updatedFields, ", "), i){{else}}?", strings.Join(updatedFields, ", ")){{end}} args = append(args, data.ID) } trace.Log("query", query) @@ -745,7 +745,7 @@ func (r *{{camel .ModuleName}}RepoSQL) Delete(ctx context.Context, filter *domai if len(args) == 0 { return errors.New("Cannot empty filter") } - query := "DELETE FROM {{snake .ModuleName}}s WHERE " + where + query := "DELETE FROM {{plural .ModuleName}} WHERE " + where trace.Log("query", query) trace.Log("args", args) var stmt *sql.Stmt