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
const (
EtcReadError = "etcdReaderErr"
EtcConnError = "etcConnectError"
ErrorReason = "reason"
PanicError = "panicError"
GormError = "gormErr"
EtcReadError = "etcdReaderErr"
EtcConnError = "etcConnectError"
ErrorReason = "reason"
PanicError = "panicError"
GormError = "gormErr"
GetServiceModelErr = "getServiceErr"
ResponseSuccess = "成功"
ResponseFail = "失败"
)

View File

@ -5,16 +5,56 @@ import (
bgm "bgm/helper"
"bgm/model"
"bgm/service"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/rs/xid"
)
func CreateCategoryH(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error {
category := &model.Category{
Key: 1,
Value: "默认",
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
import "os"
import (
"os"
"strings"
)
type QueryMap map[string]string
func GetEnvDefault(key, defVal string) string {
val, ex := os.LookupEnv(key)
@ -10,3 +15,15 @@ func GetEnvDefault(key, defVal string) string {
}
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 (
createCategory = "/sts"
category = "/category"
script = "/script"
)
type handleM map[string]func(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error
var (
mysqlMap = make(bgm.MysqlMap, 0)
err error
handlerMap = make(handleM, 0)
app = fiber.New(fiber.Config{
mysqlMap = make(bgm.MysqlMap, 0)
err error
app = fiber.New(fiber.Config{
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
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() {
//GetConfig
bgm.GetConfig()
handlerMap[createCategory] = handler.CreateCategoryH
getHandlerMap[category] = handler.CreateCategoryH
//requestId
app.Use(requestid.New())
//logger
@ -61,7 +68,7 @@ func init() {
}
func routerInit() {
handlerMap[createCategory] = handler.CreateCategoryH
getHandlerMap[category] = handler.GetCategoryH
}
func serverStart() {
@ -74,8 +81,11 @@ func serverStart() {
}
func main() {
app.Get("/*", func(ctx *fiber.Ctx) error {
return handlerMap[ctx.Path()](ctx, mysqlMap)
app.Get(fmt.Sprintf("%v", category), func(ctx *fiber.Ctx) error {
return getHandlerMap[category](ctx, mysqlMap)
})
app.Get(fmt.Sprintf("%v/:page/:size", script), func(ctx *fiber.Ctx) error {
return getHandlerMap[script](ctx, mysqlMap)
})
serverStart()
}

View File

@ -1,5 +1,14 @@
package model
import (
"bgm/consts"
bgm "bgm/helper"
"fmt"
ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap"
"gorm.io/gorm"
)
type Category struct {
//ID int `json:"id" gorm:"column:id"`
Key int `json:"key" gorm:"column:key"`
@ -10,3 +19,16 @@ type Category struct {
func (m *Category) TableName() string {
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
type Response struct {
Message string `json:"message"`
Message string `json:"message"`
Data interface{} `json:"data"`
}

View File

@ -1,16 +1,21 @@
package service
import (
bgm "bgm/helper"
"bgm/model"
ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap"
"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 {
ctxLogger.Info(nil, "createService err", zap.String("err info", err.Error()))
return err
}
return nil
}
func GetCategoryDto(page, size int, query bgm.QueryMap, db *gorm.DB) (*[]model.Category, error) {
return model.GetCategoryM(page, size, query, db)
}