package handler import ( "fmt" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/model" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/pkg" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/service" "git.icechen.cn/monorepo/backend/pkg/api" "git.icechen.cn/monorepo/backend/pkg/proto/brahma/murders" "git.icechen.cn/monorepo/backend/pkg/rpc" "github.com/go-playground/validator/v10" "github.com/gofiber/fiber/v2" ctxLogger "github.com/luizsuper/ctxLoggers" "github.com/pkg/errors" "go.uber.org/zap" "strconv" "time" ) var ( pageIsNotInt = api.WarpFError(errors.New("page 参数错误")) limitIsNotInt = api.WarpFError(errors.New("limit 参数错误")) ) func GetScriptsH(ctx *fiber.Ctx) error { page := ctx.Query("page", "1") limit := ctx.Query("size", "10") iPage, _ := strconv.Atoi(page) iLimit, _ := strconv.Atoi(limit) s := ctx.Query("query", "") query := model.GetQuery{Query: s} processMap, err := query.ProcessMap() if err != nil { ctxLogger.FError(ctx, error_process.GrammerError, zap.String("", fmt.Sprintf("%+v", err))) return ctx.JSON(fail(nil)) } m, num, err := service.GetScripts(iPage, iLimit, processMap, ctx) if err != nil { ctxLogger.FError(ctx, error_process.ServiceErr, zap.String("", fmt.Sprintf("%+v", err))) return ctx.JSON(fail(nil)) } return ctx.JSON(success(&Data{ Elements: m, Total: num, })) } func UpdateScriptsH(ctx *fiber.Ctx) error { m := new(model.Scripts) if err := checkParam(m, ctx); err != nil { ctxLogger.FError(ctx, error_process.BodyParserErr, zap.String("", fmt.Sprintf("%v", err))) return ctx.JSON(fail(nil)) } err := service.UpdateScripts(m, ctx) if err != nil { ctxLogger.FError(ctx, error_process.ServiceErr, zap.String("", fmt.Sprintf("%+v", err))) return ctx.JSON(fail(nil)) } return ctx.JSON(success(nil)) } func CreateScriptsH(ctx *fiber.Ctx) error { m := new(model.Scripts) if err := createScriptParamCheck(m, ctx); err != nil { ctxLogger.FError(ctx, error_process.BodyParserErr, zap.String("", fmt.Sprintf("%v", err))) return ctx.JSON(fail(nil)) } err := service.CreateScripts(m, ctx) if err != nil { ctxLogger.FError(ctx, error_process.ServiceErr, zap.String("", fmt.Sprintf("%+v", err))) return ctx.JSON(fail(nil)) } return ctx.JSON(success(nil)) } func DeleteScriptsH(ctx *fiber.Ctx) error { m := new(model.Scripts) if err := checkParam(m, ctx); err != nil { ctxLogger.FError(ctx, error_process.BodyParserErr, zap.String("", fmt.Sprintf("%v", err))) return ctx.JSON(fail(nil)) } err := service.DeleteScripts(m, ctx) if err != nil { ctxLogger.FError(ctx, error_process.ServiceErr, zap.String("", fmt.Sprintf("%+v", err))) return ctx.JSON(fail(nil)) } return ctx.JSON(success(nil)) } func GetScriptApi(ctx *fiber.Ctx) error { // outgoingContext, cancelFunc := pkg.TransFiberCtx2NewOutgoingContext(ctx, 3*time.Second) defer cancelFunc() c := murders.NewMurdersClient(rpc.GetServiceConn("token")) page := ctx.Query("page", "1") limit := ctx.Query("size", "10") query := ctx.Query("query", "") p, err := strconv.Atoi(page) if err != nil { ctxLogger.Error(ctx.UserContext(), pageIsNotInt.Error(), zap.String("page", page)) return pageIsNotInt } l, err := strconv.Atoi(limit) if err != nil { ctxLogger.Error(ctx.UserContext(), limitIsNotInt.Error(), zap.String("limit", limit)) return pageIsNotInt } validate := validator.New() err = validate.Var(p, "gte=0") if err != nil { ctxLogger.Error(ctx.UserContext(), limitIsNotInt.Error(), zap.String("limit", limit)) return api.WarpFError(err) } err = validate.Var(l, "gte=-1") if err != nil { ctxLogger.Error(ctx.UserContext(), limitIsNotInt.Error(), zap.String("limit", limit)) return api.WarpFError(err) } p64 := int64(p) l64 := int64(l) condition := murders.QueryCondition{ Page: &p64, Size: &l64, Query: &query, } scripts, err := c.GetScripts(outgoingContext, &condition) if err != nil { return api.WarpFError(err) } return ctx.JSON(scripts) }