Skip to content

Commit

Permalink
Merge pull request #10 from dung13890/generate_project
Browse files Browse the repository at this point in the history
Fix bug module nil
  • Loading branch information
dung13890 authored May 30, 2023
2 parents 5de8710 + b3ce2aa commit 624bbf8
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 12 deletions.
42 changes: 31 additions & 11 deletions cmd/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ var (
"internal/modules/:name/usecase",
"internal/modules/:name/repository",
}
dStructsWithoutModule = []string{
"internal/domain",
"internal/delivery/http",
"internal/usecase",
"internal/repository",
}
dFiles = []string{
"internal/domain/domain.go.tmpl",
"internal/modules/name/delivery/http/domain_http.go.tmpl",
Expand All @@ -39,6 +45,18 @@ var (
"internal/modules/name/repository/domain_dao.go.tmpl",
"internal/modules/name/repository/repository.go.tmpl",
}
dFilesWithoutModule = []string{
"internal/domain/domain.go.tmpl",
"internal/delivery/http/domain_http.go.tmpl",
"internal/delivery/http/domain_dto.go.tmpl",
"internal/delivery/http/handler.go.tmpl",
"internal/delivery/http/middleware.go.tmpl",
"internal/usecase/domain_uc.go.tmpl",
"internal/usecase/usecase.go.tmpl",
"internal/repository/domain_repo.go.tmpl",
"internal/repository/domain_dao.go.tmpl",
"internal/repository/repository.go.tmpl",
}
)

type domain struct {
Expand Down Expand Up @@ -185,12 +203,13 @@ func (*domain) destroy(context.Context) error {
// generateStruct is a function to generate struct for domain
func (d *domain) generateStruct(context.Context) error {
dir := getDir(d.Path, d.Project, d.ForcePath)
structs := dStructsWithoutModule
if d.Module != "" {
structs = dStructs
}
// Generate struct
for _, s := range dStructs {
target := strings.Replace(s, "/modules/:name/", "/", 1)
if d.Module != "" {
target = strings.Replace(s, ":name", d.Module, 1)
}
for _, s := range structs {
target := strings.Replace(s, ":name", d.Module, 1)
// Check directory exist or not
if _, err := os.Stat(filepath.Join(dir, target)); !errors.Is(err, os.ErrNotExist) {
continue
Expand All @@ -209,18 +228,15 @@ func (d *domain) generateFile(_ context.Context) error {
tmpl, err := template.NewTemplate("tmpl", []string{
"tmpl/*/*.tmpl",
"tmpl/*/*/*.tmpl",
"tmpl/*/*/*/*.tmpl",
"tmpl/*/*/*/*/*.tmpl",
"tmpl/*/*/*/*/*/*.tmpl",
})
if err != nil {
return err
}
rplModule := "/"
if d.Module != "" {
rplModule = "/modules/" + d.Module + "/"
}
rl := strings.NewReplacer(
"/modules/name/", rplModule,
"/modules/name/", "/modules/"+d.Module+"/",
"domain.go", d.Domain+".go",
"domain_http.go", d.Domain+"_http.go",
"domain_grpc.go", d.Domain+"_grpc.go",
Expand All @@ -229,7 +245,11 @@ func (d *domain) generateFile(_ context.Context) error {
"domain_dao.go", d.Domain+"_dao.go",
"domain_uc.go", d.Domain+"_uc.go",
)
for _, f := range dFiles {
files := dFilesWithoutModule
if d.Module != "" {
files = dFiles
}
for _, f := range files {
index := strings.TrimSuffix(f, ".tmpl")
target := filepath.Join(dir, rl.Replace(index))

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
version string = "v1.0.4"
version string = "v1.0.5"
)

func main() {
Expand Down
43 changes: 43 additions & 0 deletions template/tmpl/internal/delivery/http/domain_dto.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{{/*
Copyright (c) 2023 dung13890. All rights reserved.
Mit License (MIT)
*/}}

{{define "internal/delivery/http/domain_dto.go"}}package http

import (
"time"

"{{.Project}}/internal/domain"
)

// {{.Domain | capitalize}}Request is request for create {{.Domain}}
type {{.Domain | capitalize}}Request struct {
}

// {{.Domain | capitalize}}Response is struct used for {{.Domain}}
type {{.Domain | capitalize}}Response struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

// {{.Domain | capitalize}}StatusResponse is struct when success
type {{.Domain | capitalize}}StatusResponse struct {
Status bool `json:"status"`
}

// convert{{.Domain | capitalize}}RequestToEntity DTO
func convert{{.Domain | capitalize}}RequestToEntity(*{{.Domain | capitalize}}Request) *domain.{{.Domain | capitalize}} {
return &domain.{{.Domain | capitalize}}{}
}

// convert{{.Domain | capitalize}}EntityToResponse DTO
func convert{{.Domain | capitalize}}EntityToResponse(dm *domain.{{.Domain | capitalize}}) {{.Domain | capitalize}}Response {
return {{.Domain | capitalize}}Response{
ID: dm.ID,
CreatedAt: dm.CreatedAt,
UpdatedAt: dm.UpdatedAt,
}
}
{{end}}
75 changes: 75 additions & 0 deletions template/tmpl/internal/delivery/http/domain_http.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{{/*
Copyright (c) 2023 dung13890. All rights reserved.
Mit License (MIT)
*/}}

{{define "internal/delivery/http/domain_http.go"}}package http

import (
"net/http"
"strconv"

"{{.Project}}/internal/domain"
"{{.Project}}/pkg/errors"

"github.com/labstack/echo/v4"
)

// {{.Domain | capitalize}}Handler represent the http handler
type {{.Domain | capitalize}}Handler struct {
Usecase domain.{{.Domain | capitalize}}Usecase
}

// Index will fetch data
func (hl *{{.Domain | capitalize}}Handler) Index(c echo.Context) error {
ctx := c.Request().Context()
items, err := hl.Usecase.Fetch(ctx)
if err != nil {
return errors.Throw(err)
}
itemsRes := make([]{{.Domain | capitalize}}Response, 0)
for i := range items {
item := convert{{.Domain | capitalize}}EntityToResponse(&items[i])
itemsRes = append(itemsRes, item)
}

return c.JSON(http.StatusOK, itemsRes)
}

// Show will Find data
func (hl *{{.Domain | capitalize}}Handler) Show(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return errors.ErrBadRequest.Wrap(err)
}
ctx := c.Request().Context()
item, err := hl.Usecase.Find(ctx, id)
if err != nil {
return errors.Throw(err)
}

return c.JSON(http.StatusOK, convert{{.Domain | capitalize}}EntityToResponse(item))
}

// Store will create data
func (hl *{{.Domain | capitalize}}Handler) Store(c echo.Context) error {
request := new({{.Domain | capitalize}}Request)
if err := c.Bind(request); err != nil {
return errors.ErrBadRequest.Wrap(err)
}

err := c.Validate(request)
if err != nil {
return errors.ErrUnprocessableEntity.Wrap(err)
}

entity := convert{{.Domain | capitalize}}RequestToEntity(request)

ctx := c.Request().Context()
if err := hl.Usecase.Store(ctx, entity); err != nil {
return errors.Throw(err)
}

return c.JSON(http.StatusCreated, {{.Domain | capitalize}}StatusResponse{Status: true})
}
{{end}}
26 changes: 26 additions & 0 deletions template/tmpl/internal/delivery/http/handler.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{/*
Copyright (c) 2023 dung13890. All rights reserved.
Mit License (MIT)
*/}}

{{define "internal/delivery/http/handler.go"}}package http

import (
"{{.Project}}/internal/usecase"

"github.com/labstack/echo/v4"
)

// NewHandler will initialize the {{.Module}} module endpoints
func NewHandler(g *echo.Group, uc *usecase.Usecase) {
// {{.Domain}}Handler will initialize the {{.Domain}}/ resources endpoint
{{.Domain}}Handler := &{{.Domain | capitalize}}Handler{
Usecase: uc.{{.Domain | capitalize}}UC,
}

// {{.Domain | capitalize}} routes
g.GET("/{{.Domain}}s", {{.Domain}}Handler.Index)
g.GET("/{{.Domain}}s/:id", {{.Domain}}Handler.Show)
g.POST("/{{.Domain}}s", {{.Domain}}Handler.Store)
}
{{end}}
7 changes: 7 additions & 0 deletions template/tmpl/internal/delivery/http/middleware.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{/*
Copyright (c) 2023 dung13890. All rights reserved.
Mit License (MIT)
*/}}

{{define "internal/delivery/http/middleware.go"}}package http
{{end}}
42 changes: 42 additions & 0 deletions template/tmpl/internal/repository/domain_dao.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{{/*
Copyright (c) 2023 dung13890. All rights reserved.
Mit License (MIT)
*/}}

{{define "internal/repository/domain_dao.go"}}package repository

import (
"{{.Project}}/internal/domain"

"gorm.io/gorm"
)

// {{.Domain | capitalize}} DAO model
type {{.Domain | capitalize}} struct {
gorm.Model
}

// convert{{.Domain | capitalize}}ToEntity .-
func convert{{.Domain | capitalize}}ToEntity(dao *{{.Domain | capitalize}}) *domain.{{.Domain | capitalize}} {
e := &domain.{{.Domain | capitalize}}{
ID: dao.ID,
CreatedAt: dao.CreatedAt,
UpdatedAt: dao.UpdatedAt,
}

return e
}

// convert{{.Domain | capitalize}}ToDao .-
func convert{{.Domain | capitalize}}ToDao(entity *domain.{{.Domain | capitalize}}) *{{.Domain | capitalize}} {
d := &{{.Domain | capitalize}}{
Model: gorm.Model{
ID: entity.ID,
CreatedAt: entity.CreatedAt,
UpdatedAt: entity.UpdatedAt,
},
}

return d
}
{{end}}
63 changes: 63 additions & 0 deletions template/tmpl/internal/repository/domain_repo.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{{/*
Copyright (c) 2023 dung13890. All rights reserved.
Mit License (MIT)
*/}}

{{define "internal/repository/domain_repo.go"}}package repository

import (
"context"

"{{.Project}}/internal/domain"
"{{.Project}}/pkg/errors"

"gorm.io/gorm"
)

// {{.Domain | capitalize}}Repository ...
type {{.Domain | capitalize}}Repository struct {
*gorm.DB
}

// Fetch will fetch content from db
func (rp *{{.Domain | capitalize}}Repository) Fetch(ctx context.Context) ([]domain.{{.Domain | capitalize}}, error) {
dao := []{{.Domain | capitalize}}{}
if err := rp.DB.WithContext(ctx).Find(&dao).Error; err != nil {
return nil, errors.ErrUnexpectedDBError.Wrap(err)
}

items := []domain.{{.Domain | capitalize}}{}

for i := range dao {
r := convert{{.Domain | capitalize}}ToEntity(&dao[i])
items = append(items, *r)
}

return items, nil
}

// Find will find content from db
func (rp *{{.Domain | capitalize}}Repository) Find(ctx context.Context, id int) (*domain.{{.Domain | capitalize}}, error) {
dao := {{.Domain | capitalize}}{}
if err := rp.DB.WithContext(ctx).First(&dao, id).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.ErrNotFound.Wrap(err)
}
return nil, errors.ErrUnexpectedDBError.Wrap(err)
}

return convert{{.Domain | capitalize}}ToEntity(&dao), nil
}

// Store will create data to db
func (rp *{{.Domain | capitalize}}Repository) Store(ctx context.Context, dm *domain.{{.Domain | capitalize}}) error {
dao := convert{{.Domain | capitalize}}ToDao(dm)
if err := rp.DB.WithContext(ctx).Create(&dao).Error; err != nil {
return errors.ErrUnexpectedDBError.Wrap(err)
}

*dm = *convert{{.Domain | capitalize}}ToEntity(dao)

return nil
}
{{end}}
7 changes: 7 additions & 0 deletions template/tmpl/internal/repository/repository.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{/*
Copyright (c) 2023 dung13890. All rights reserved.
Mit License (MIT)
*/}}

{{define "internal/repository/repository.go"}}package repository
{{end}}
Loading

0 comments on commit 624bbf8

Please sign in to comment.