-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from dung13890/generate_project
Fix bug module nil
- Loading branch information
Showing
11 changed files
with
357 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import ( | |
) | ||
|
||
var ( | ||
version string = "v1.0.4" | ||
version string = "v1.0.5" | ||
) | ||
|
||
func main() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
Oops, something went wrong.