backend/app/brahma/api/murder/internal/handler/scripts_h.go

104 lines
3.0 KiB
Go
Raw Normal View History

2022-01-12 14:34:31 +08:00
package handler
import (
2022-01-14 19:55:07 +08:00
"context"
2022-01-12 14:34:31 +08:00
"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"
2022-01-14 19:55:07 +08:00
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murder"
2022-01-12 14:34:31 +08:00
"github.com/gofiber/fiber/v2"
ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap"
2022-01-14 19:55:07 +08:00
"google.golang.org/grpc"
"log"
2022-01-12 14:34:31 +08:00
"strconv"
2022-01-14 19:55:07 +08:00
"time"
2022-01-12 14:34:31 +08:00
)
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))
}
2022-01-14 19:55:07 +08:00
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
}