Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix service name naming #43

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/candi/cli_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) == "" {
Expand Down Expand Up @@ -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) == "" {
Expand Down
4 changes: 3 additions & 1 deletion cmd/candi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"runtime"
"text/template"

"github.com/golangid/candi/candihelper"
)

func main() {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/candi/template_delivery_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/candi/template_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/candi/template_etc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
12 changes: 6 additions & 6 deletions cmd/candi/template_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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}}
Expand All @@ -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...).
Expand Down Expand Up @@ -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"}}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions codebase/app/redis_worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```
Loading