Skip to content

Commit cfbefb7

Browse files
authored
Feat/rollback (#74)
1 parent f195af1 commit cfbefb7

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ SRC_DIR=$(GOPATH)/src
1515

1616
.PHONY: proto
1717
proto:
18-
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/*.proto
18+
#protoc-go-inject-tag -I ./proto -I ${GOPATH}/src --go_out=plugins=grpc: proto/${W}/${V}/*;
19+
find proto/ -name '*.proto' -exec protoc --proto_path=$(PROTO_PATH) $(PROTO_FLAGS) --go_out=plugins=grpc:. {} \;
1920

2021

2122
.PHONY: lint
2223
lint:
2324
golangci-lint run -v ./...
25+
fix-lint:
26+
goimports -w .

pkg/response/actions/get_gorm.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ func NewGetGorm(m schema.Tabler, key string) *Get {
3333
func (e *Get) getGorm(c *gin.Context, key string) {
3434
api := response.Make(c)
3535
m := pkg.TablerDeepCopy(e.ModelGorm)
36-
err := gormdb.DB.First(m, "id = ?", c.Param(key)).Error
36+
preloads := c.QueryArray("preloads[]")
37+
query := gormdb.DB.Model(m).Where("id = ?", c.Param(key))
38+
for _, preload := range preloads {
39+
query = query.Preload(preload)
40+
}
41+
err := query.First(m).Error
3742
if err != nil {
3843
if errors.Is(err, gorm.ErrRecordNotFound) {
3944
api.Err(http.StatusNotFound)

pkg/response/model.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@ func (e *response) OK(c *gin.Context, data interface{}) {
9292
res := Default.Clone()
9393
res.SetList(data)
9494
res.SetTraceID(pkg.GenerateMsgIDFromContext(c))
95+
status := http.StatusOK
9596
switch c.Request.Method {
9697
case http.MethodDelete:
97-
res.SetCode(http.StatusNoContent)
98-
c.AbortWithStatusJSON(http.StatusNoContent, data)
99-
return
98+
status = http.StatusNoContent
10099
case http.MethodPost:
101-
res.SetCode(http.StatusCreated)
102-
c.AbortWithStatusJSON(http.StatusCreated, data)
100+
status = http.StatusCreated
101+
}
102+
res.SetCode(status)
103+
if data == nil {
104+
c.AbortWithStatus(status)
103105
return
104-
default:
105-
res.SetCode(http.StatusOK)
106-
c.AbortWithStatusJSON(http.StatusOK, data)
107106
}
107+
c.AbortWithStatusJSON(status, data)
108108
}
109109

110110
// PageOK page ok

0 commit comments

Comments
 (0)