diff --git a/consts/errors.go b/consts/errors.go index 9b7bcd4..9f323d0 100644 --- a/consts/errors.go +++ b/consts/errors.go @@ -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 = "失败" ) diff --git a/handler/category.go b/handler/category.go index a01c85f..c9a0e8c 100644 --- a/handler/category.go +++ b/handler/category.go @@ -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, +// }) +// } +//} diff --git a/helper/public_func.go b/helper/public_func.go index 728ae84..a946b67 100644 --- a/helper/public_func.go +++ b/helper/public_func.go @@ -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 +} diff --git a/main.go b/main.go index 447a765..ad56660 100644 --- a/main.go +++ b/main.go @@ -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() } diff --git a/model/category.go b/model/category.go index b840eb7..8ee3e1f 100644 --- a/model/category.go +++ b/model/category.go @@ -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 +} diff --git a/model/dto.go b/model/dto.go index 78ae43d..9046194 100644 --- a/model/dto.go +++ b/model/dto.go @@ -1,5 +1,6 @@ package model type Response struct { - Message string `json:"message"` + Message string `json:"message"` + Data interface{} `json:"data"` } diff --git a/service/category.go b/service/category.go index 355c374..375b2fc 100644 --- a/service/category.go +++ b/service/category.go @@ -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) +}