package handler import ( "context" "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/service" "git.icechen.cn/monorepo/backend/pkg/proto/brahma/murder" "github.com/gofiber/fiber/v2" ctxLogger "github.com/luizsuper/ctxLoggers" "go.uber.org/zap" "google.golang.org/grpc" "log" "strconv" "time" ) 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 HelloWorld() error { // Set up a connection to the server. conn, err := grpc.Dial("localhost:3000", grpc.WithInsecure(), grpc.WithBlock()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := murder.NewHelloWorldClient(conn) // Contact the server and print out its response. ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() r, err := c.Login(ctx, &murder.HelloRequest{Code: "lht"}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.Token) return nil }