feat: and 查询

master
liuhaotian 2021-12-14 19:45:25 +08:00
parent 9977fd1a16
commit 3679aaab18
7 changed files with 117 additions and 19 deletions

View File

@ -1,9 +1,12 @@
package consts package consts
const ( const (
EtcReadError = "etcdReaderErr" EtcReadError = "etcdReaderErr"
EtcConnError = "etcConnectError" EtcConnError = "etcConnectError"
ErrorReason = "reason" ErrorReason = "reason"
PanicError = "panicError" PanicError = "panicError"
GormError = "gormErr" GormError = "gormErr"
GetServiceModelErr = "getServiceErr"
ResponseSuccess = "成功"
ResponseFail = "失败"
) )

View File

@ -5,16 +5,56 @@ import (
bgm "bgm/helper" bgm "bgm/helper"
"bgm/model" "bgm/model"
"bgm/service" "bgm/service"
"fmt"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/rs/xid" "github.com/rs/xid"
) )
func CreateCategoryH(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error { func CreateCategoryH(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error {
category := &model.Category{ category := &model.Category{
Key: 1, Key: 1,
Value: "默认", Value: "默认",
Gid: xid.New().String(), Gid: xid.New().String(),
} }
return service.CreateCategory(category, mysqlMap[consts.TestDB]) return service.CreatCategory(category, mysqlMap[consts.TestDB])
} }
func GetCategoryH(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error {
fmt.Println(string(ctx.Request().URI().QueryString()))
page, _ := ctx.ParamsInt("page", 1)
limit, _ := ctx.ParamsInt("size", 10)
s := ctx.Query("query", "")
queryMap := bgm.GetQueryMap(s)
dto, err := service.GetCategoryDto(page, limit, queryMap, mysqlMap[consts.TestDB])
if err != nil {
return ctx.JSON(model.Response{
Message: consts.ResponseFail,
Data: nil,
})
} else {
return ctx.JSON(model.Response{
Message: consts.ResponseSuccess,
Data: dto,
})
}
}
//func GetScriptH(ctx*fiber.Ctx, mysqlMap bgm.MysqlMap) error {
// page, _ := ctx.ParamsInt("page", 1)
// limit, _ := ctx.ParamsInt("size", 10)
// queryParam := ctx.Params("query","")
//
// dto, err := service.GetCategoryDto(page, limit, mysqlMap[consts.TestDB])
// if err != nil {
// return ctx.JSON(model.Response{
// Message: consts.ResponseFail,
// Data: nil,
// })
// }else {
// return ctx.JSON(model.Response{
// Message: consts.ResponseSuccess,
// Data: dto,
// })
// }
//}

View File

@ -1,6 +1,11 @@
package bgm package bgm
import "os" import (
"os"
"strings"
)
type QueryMap map[string]string
func GetEnvDefault(key, defVal string) string { func GetEnvDefault(key, defVal string) string {
val, ex := os.LookupEnv(key) val, ex := os.LookupEnv(key)
@ -10,3 +15,15 @@ func GetEnvDefault(key, defVal string) string {
} }
return val return val
} }
func GetQueryMap(s string) QueryMap {
m := make(map[string]string)
if s != "" {
s = string([]byte(s)[1 : len([]byte(s))-1])
for _, v := range strings.Split(s, ",") {
split := strings.Split(v, "=")
m[split[0]] = split[1]
}
}
return m
}

28
main.go
View File

@ -17,26 +17,33 @@ import (
) )
const ( const (
createCategory = "/sts" category = "/category"
script = "/script"
) )
type handleM map[string]func(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error type handleM map[string]func(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error
var ( var (
mysqlMap = make(bgm.MysqlMap, 0) mysqlMap = make(bgm.MysqlMap, 0)
err error err error
handlerMap = make(handleM, 0) app = fiber.New(fiber.Config{
app = fiber.New(fiber.Config{
ErrorHandler: func(ctx *fiber.Ctx, err error) error { ErrorHandler: func(ctx *fiber.Ctx, err error) error {
return ctx.Status(fiber.StatusInternalServerError).JSON(model.Response{Message: "服务器内部错误"}) return ctx.Status(fiber.StatusInternalServerError).JSON(model.Response{Message: "服务器内部错误"})
}, },
}) })
) )
var (
getHandlerMap = make(handleM, 0)
postHandlerMap = make(handleM, 0)
putHandlerMap = make(handleM, 0)
delHandlerMap = make(handleM, 0)
)
func init() { func init() {
//GetConfig //GetConfig
bgm.GetConfig() bgm.GetConfig()
handlerMap[createCategory] = handler.CreateCategoryH getHandlerMap[category] = handler.CreateCategoryH
//requestId //requestId
app.Use(requestid.New()) app.Use(requestid.New())
//logger //logger
@ -61,7 +68,7 @@ func init() {
} }
func routerInit() { func routerInit() {
handlerMap[createCategory] = handler.CreateCategoryH getHandlerMap[category] = handler.GetCategoryH
} }
func serverStart() { func serverStart() {
@ -74,8 +81,11 @@ func serverStart() {
} }
func main() { func main() {
app.Get("/*", func(ctx *fiber.Ctx) error { app.Get(fmt.Sprintf("%v", category), func(ctx *fiber.Ctx) error {
return handlerMap[ctx.Path()](ctx, mysqlMap) return getHandlerMap[category](ctx, mysqlMap)
})
app.Get(fmt.Sprintf("%v/:page/:size", script), func(ctx *fiber.Ctx) error {
return getHandlerMap[script](ctx, mysqlMap)
}) })
serverStart() serverStart()
} }

View File

@ -1,5 +1,14 @@
package model package model
import (
"bgm/consts"
bgm "bgm/helper"
"fmt"
ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap"
"gorm.io/gorm"
)
type Category struct { type Category struct {
//ID int `json:"id" gorm:"column:id"` //ID int `json:"id" gorm:"column:id"`
Key int `json:"key" gorm:"column:key"` Key int `json:"key" gorm:"column:key"`
@ -10,3 +19,16 @@ type Category struct {
func (m *Category) TableName() string { func (m *Category) TableName() string {
return "category" return "category"
} }
func GetCategoryM(page, size int, query bgm.QueryMap, db *gorm.DB) (*[]Category, error) {
i := new([]Category)
db = db.Limit(size).Offset((page - 1) * size)
for k, v := range query {
db = db.Where(fmt.Sprintf("%v = ?", k), v)
}
if err := db.Debug().Find(i).Error; err != nil {
ctxLogger.Info(nil, consts.GetServiceModelErr, zap.String(consts.ErrorReason, err.Error()))
return nil, err
}
return i, nil
}

View File

@ -1,5 +1,6 @@
package model package model
type Response struct { type Response struct {
Message string `json:"message"` Message string `json:"message"`
Data interface{} `json:"data"`
} }

View File

@ -1,16 +1,21 @@
package service package service
import ( import (
bgm "bgm/helper"
"bgm/model" "bgm/model"
ctxLogger "github.com/luizsuper/ctxLoggers" ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap" "go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
) )
func CreateCategory(category *model.Category, db *gorm.DB) error { func CreatCategory(category *model.Category, db *gorm.DB) error {
if err := db.Create(category).Error; err != nil { if err := db.Create(category).Error; err != nil {
ctxLogger.Info(nil, "createService err", zap.String("err info", err.Error())) ctxLogger.Info(nil, "createService err", zap.String("err info", err.Error()))
return err return err
} }
return nil return nil
} }
func GetCategoryDto(page, size int, query bgm.QueryMap, db *gorm.DB) (*[]model.Category, error) {
return model.GetCategoryM(page, size, query, db)
}