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

142 lines
4.0 KiB
Go
Raw Normal View History

2022-01-12 14:34:31 +08:00
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"
2022-01-19 19:39:04 +08:00
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/pkg"
2022-01-12 14:34:31 +08:00
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/service"
2022-01-19 19:39:04 +08:00
"git.icechen.cn/monorepo/backend/pkg/api"
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murders"
2022-01-23 14:48:44 +08:00
"git.icechen.cn/monorepo/backend/pkg/rpc"
2022-01-19 19:39:04 +08:00
"github.com/go-playground/validator/v10"
2022-01-12 14:34:31 +08:00
"github.com/gofiber/fiber/v2"
ctxLogger "github.com/luizsuper/ctxLoggers"
2022-01-19 19:39:04 +08:00
"github.com/pkg/errors"
2022-01-12 14:34:31 +08:00
"go.uber.org/zap"
"strconv"
2022-01-19 19:39:04 +08:00
"time"
)
var (
pageIsNotInt = api.WarpFError(errors.New("page 参数错误"))
limitIsNotInt = api.WarpFError(errors.New("limit 参数错误"))
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
2022-01-19 19:39:04 +08:00
func GetScriptApi(ctx *fiber.Ctx) error {
outgoingContext, cancelFunc := pkg.TransFiberCtx2NewOutgoingContext(ctx, 3*time.Second)
defer cancelFunc()
2022-01-23 14:48:44 +08:00
c := murders.NewMurdersClient(rpc.GetServiceConn("murder"))
2022-01-19 19:39:04 +08:00
page := ctx.Query("page", "1")
limit := ctx.Query("size", "10")
2022-01-23 14:48:44 +08:00
query := ctx.Query("query", "")
2022-01-19 19:39:04 +08:00
p, err := strconv.Atoi(page)
if err != nil {
ctxLogger.Error(ctx.UserContext(), pageIsNotInt.Error(), zap.String("page", page))
return pageIsNotInt
}
2022-01-23 14:48:44 +08:00
2022-01-19 19:39:04 +08:00
l, err := strconv.Atoi(limit)
if err != nil {
ctxLogger.Error(ctx.UserContext(), limitIsNotInt.Error(), zap.String("limit", limit))
return pageIsNotInt
}
validate := validator.New()
2022-01-23 14:48:44 +08:00
2022-01-19 19:39:04 +08:00
err = validate.Var(p, "gte=0")
if err != nil {
ctxLogger.Error(ctx.UserContext(), limitIsNotInt.Error(), zap.String("limit", limit))
return api.WarpFError(err)
}
2022-01-23 14:48:44 +08:00
2022-01-19 19:39:04 +08:00
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)
2022-01-23 14:48:44 +08:00
2022-01-19 19:39:04 +08:00
condition := murders.QueryCondition{
2022-01-23 14:48:44 +08:00
Page: &p64,
Size: &l64,
Query: &query,
2022-01-19 19:39:04 +08:00
}
scripts, err := c.GetScripts(outgoingContext, &condition)
if err != nil {
return api.WarpFError(err)
}
return ctx.JSON(scripts)
}