Skip to content

Commit

Permalink
Feat/rollback (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwnmengjing authored Dec 18, 2023
1 parent f195af1 commit cfbefb7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ SRC_DIR=$(GOPATH)/src

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


.PHONY: lint
lint:
golangci-lint run -v ./...
fix-lint:
goimports -w .
7 changes: 6 additions & 1 deletion pkg/response/actions/get_gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ func NewGetGorm(m schema.Tabler, key string) *Get {
func (e *Get) getGorm(c *gin.Context, key string) {
api := response.Make(c)
m := pkg.TablerDeepCopy(e.ModelGorm)
err := gormdb.DB.First(m, "id = ?", c.Param(key)).Error
preloads := c.QueryArray("preloads[]")
query := gormdb.DB.Model(m).Where("id = ?", c.Param(key))
for _, preload := range preloads {
query = query.Preload(preload)
}
err := query.First(m).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
api.Err(http.StatusNotFound)
Expand Down
16 changes: 8 additions & 8 deletions pkg/response/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ func (e *response) OK(c *gin.Context, data interface{}) {
res := Default.Clone()
res.SetList(data)
res.SetTraceID(pkg.GenerateMsgIDFromContext(c))
status := http.StatusOK
switch c.Request.Method {
case http.MethodDelete:
res.SetCode(http.StatusNoContent)
c.AbortWithStatusJSON(http.StatusNoContent, data)
return
status = http.StatusNoContent
case http.MethodPost:
res.SetCode(http.StatusCreated)
c.AbortWithStatusJSON(http.StatusCreated, data)
status = http.StatusCreated
}
res.SetCode(status)
if data == nil {
c.AbortWithStatus(status)
return
default:
res.SetCode(http.StatusOK)
c.AbortWithStatusJSON(http.StatusOK, data)
}
c.AbortWithStatusJSON(status, data)
}

// PageOK page ok
Expand Down

0 comments on commit cfbefb7

Please sign in to comment.