generated from pkg/go-template
feat: murder项目整体迁移
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
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/service"
|
||||
bgm "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/util"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
ctxLogger "github.com/luizsuper/ctxLoggers"
|
||||
"go.uber.org/zap"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type (
|
||||
CategoryReq struct {
|
||||
Category *model.Category `json:"category" `
|
||||
}
|
||||
CategoriesTagReq struct {
|
||||
Relation *model.CategoriesTagDto `json:"relation" `
|
||||
}
|
||||
)
|
||||
|
||||
func GetCategoryH(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", "")
|
||||
queryMap := bgm.GetQueryMap(s)
|
||||
m, num, err := service.GetCategory(iPage, iLimit, queryMap, 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,
|
||||
}))
|
||||
}
|
||||
|
||||
//CreateCategoryH 创建类别
|
||||
func CreateCategoryH(ctx *fiber.Ctx) error {
|
||||
m := new(CategoryReq)
|
||||
if err := createCategoryParamCheck(m, ctx); err != nil {
|
||||
ctxLogger.FError(ctx, error_process.BodyParserErr, zap.String("", fmt.Sprintf("%v", err)))
|
||||
return ctx.JSON(fail(nil))
|
||||
}
|
||||
err := service.CreateCategory(m.Category, 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 UpdateCategoryH(ctx *fiber.Ctx) error {
|
||||
m := new(CategoryReq)
|
||||
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.UpdateCategory(m.Category, 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 DeleteCategoryH(ctx *fiber.Ctx) error {
|
||||
m := new(CategoryReq)
|
||||
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.DeleteCategory(m.Category, 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))
|
||||
}
|
||||
|
||||
// CreateTagForCategory 为Category 新增tag
|
||||
func CreateTagForCategory(ctx *fiber.Ctx) error {
|
||||
m := new(CategoriesTagReq)
|
||||
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.CreateTagForCategory(m.Relation, 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))
|
||||
}
|
||||
|
||||
// DeleteTagForCategory 为Category 删除tag
|
||||
func DeleteTagForCategory(ctx *fiber.Ctx) error {
|
||||
m := new(CategoriesTagReq)
|
||||
if err := ctx.BodyParser(m); err != nil {
|
||||
ctxLogger.FError(ctx, error_process.BodyParserErr, zap.String("", fmt.Sprintf("%+v", err)))
|
||||
return ctx.JSON(fail(nil))
|
||||
}
|
||||
err := service.DeleteTagForCategory(m.Relation, 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))
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/model"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cast"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type pluginFunc func(interface{}) error
|
||||
|
||||
func checkParam(body interface{}, ctx *fiber.Ctx, plugins ...pluginFunc) error {
|
||||
//第一次执行解析body,并且调用插件方法
|
||||
if ctx != nil {
|
||||
err := ctx.BodyParser(body)
|
||||
if err != nil {
|
||||
return error_process.CommonErr(err)
|
||||
}
|
||||
for _, plugin := range plugins {
|
||||
if err = plugin(body); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
//step1 校验是否是指针
|
||||
typ := reflect.TypeOf(body)
|
||||
val := reflect.ValueOf(body)
|
||||
if val.Kind().String() != reflect.Ptr.String() {
|
||||
return errors.New(error_process.NoPointError)
|
||||
}
|
||||
if val.IsNil() {
|
||||
return errors.New(error_process.NilPointError)
|
||||
}
|
||||
num := val.Elem().NumField()
|
||||
for i := 0; i < num; i++ {
|
||||
kind := val.Elem().Field(i).Kind().String()
|
||||
switch kind {
|
||||
case reflect.Ptr.String():
|
||||
//显示指定某些指针跳过校验
|
||||
if tag := typ.Elem().Field(i).Tag.Get("valid"); tag == "omitempty" {
|
||||
continue
|
||||
}
|
||||
//递归调用
|
||||
if err := checkParam(val.Elem().Field(i).Interface(), nil); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
//非空值验证
|
||||
if tag := typ.Elem().Field(i).Tag.Get("valid"); tag == "no_empty" {
|
||||
switch kind {
|
||||
case reflect.Ptr.String():
|
||||
if val.Elem().Field(i).IsNil() == true {
|
||||
return error_process.EmptyValueErr(typ.Elem().Field(i).Tag.Get("json"))
|
||||
}
|
||||
case reflect.Slice.String():
|
||||
if val.Elem().Field(i).IsNil() == true || val.Elem().Field(i).Len() == 0 {
|
||||
return error_process.EmptyValueErr(typ.Elem().Field(i).Tag.Get("json"))
|
||||
}
|
||||
default:
|
||||
if val.Elem().Field(i).IsZero() {
|
||||
return error_process.EmptyValueErr(typ.Elem().Field(i).Tag.Get("json"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//指定制验证
|
||||
if values := typ.Elem().Field(i).Tag.Get("value"); values != "" {
|
||||
eles := make([]string, 0)
|
||||
if eles = strings.Split(values, "|"); len(eles) == 0 {
|
||||
continue
|
||||
}
|
||||
switch kind {
|
||||
case reflect.Int.String():
|
||||
for _, v := range eles {
|
||||
if b := val.Elem().Field(i).Int(); b == cast.ToInt64(v) {
|
||||
//匹配到值定制跳出此次tag处理方法
|
||||
goto LOOP
|
||||
}
|
||||
}
|
||||
return errors.New(error_process.IllegalValueError)
|
||||
default:
|
||||
return errors.New(error_process.BodyParserErr)
|
||||
}
|
||||
}
|
||||
LOOP:
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//tag uuid 覆盖写
|
||||
func tagUuidPlugin(body interface{}) error {
|
||||
if req, ok := body.(*TagReq); ok {
|
||||
if req.Tag == nil {
|
||||
return errors.New(error_process.BodyParserErr)
|
||||
}
|
||||
//if req.Tag.Uuid == "" {
|
||||
// req.Tag.Uuid = xid.New().String()
|
||||
//}
|
||||
return nil
|
||||
}
|
||||
return errors.New(error_process.BodyParserErr)
|
||||
}
|
||||
|
||||
//category uuid 覆盖写
|
||||
func categoryUuidPlugin(body interface{}) error {
|
||||
if req, ok := body.(*CategoryReq); ok {
|
||||
if req.Category == nil {
|
||||
return errors.New(error_process.BodyParserErr)
|
||||
}
|
||||
if req.Category.Uuid == "" {
|
||||
req.Category.Uuid = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New(error_process.BodyParserErr)
|
||||
}
|
||||
|
||||
//script uuid 覆盖写
|
||||
func scriptUuidPlugin(body interface{}) error {
|
||||
if req, ok := body.(*model.Scripts); ok {
|
||||
if req == nil {
|
||||
return errors.New(error_process.BodyParserErr)
|
||||
}
|
||||
if req.Uuid == "" {
|
||||
req.Uuid = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New(error_process.BodyParserErr)
|
||||
}
|
||||
|
||||
func createTagParamCheck(body interface{}, ctx *fiber.Ctx) error {
|
||||
return checkParam(body, ctx, tagUuidPlugin)
|
||||
}
|
||||
|
||||
func createCategoryParamCheck(body interface{}, ctx *fiber.Ctx) error {
|
||||
return checkParam(body, ctx, categoryUuidPlugin)
|
||||
}
|
||||
|
||||
func createScriptParamCheck(body interface{}, ctx *fiber.Ctx) error {
|
||||
return checkParam(body, ctx, scriptUuidPlugin)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package handler
|
||||
|
||||
const (
|
||||
ResponseCodeSuccess = 0
|
||||
ResponseCodeFail = -1
|
||||
ResponseSuccess = "success"
|
||||
ResponseFail = "fail"
|
||||
)
|
||||
|
||||
type (
|
||||
Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data *Data `json:"data,omitempty"`
|
||||
}
|
||||
Data struct {
|
||||
Elements interface{} `json:"elements"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
)
|
||||
|
||||
func fail(data *Data) Response {
|
||||
return Response{
|
||||
Code: ResponseCodeFail,
|
||||
Message: ResponseFail,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func success(data *Data) Response {
|
||||
return Response{
|
||||
Code: ResponseCodeSuccess,
|
||||
Message: ResponseSuccess,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
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/service"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
ctxLogger "github.com/luizsuper/ctxLoggers"
|
||||
"go.uber.org/zap"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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))
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
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/service"
|
||||
bgm "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/util"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
ctxLogger "github.com/luizsuper/ctxLoggers"
|
||||
"go.uber.org/zap"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type (
|
||||
TagReq struct {
|
||||
Tag *model.Tag `json:"tag" `
|
||||
}
|
||||
TagCategoriesReq struct {
|
||||
Relation *model.TagCategoriesDto `json:"relation" `
|
||||
}
|
||||
)
|
||||
|
||||
func CreateTagH(ctx *fiber.Ctx) error {
|
||||
m := new(TagReq)
|
||||
if err := createTagParamCheck(m, ctx); err != nil {
|
||||
ctxLogger.FError(ctx, error_process.BodyParserErr, zap.String("", fmt.Sprintf("%v", err)))
|
||||
return ctx.JSON(fail(nil))
|
||||
}
|
||||
err := service.CreateTag(m.Tag, 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 UpdateTagH(ctx *fiber.Ctx) error {
|
||||
m := new(TagReq)
|
||||
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.UpdateTag(m.Tag, 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 GetTagH(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", "")
|
||||
queryMap := bgm.GetQueryMap(s)
|
||||
m, num, err := service.GetTag(iPage, iLimit, ctx, queryMap)
|
||||
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 DeleteTagH(ctx *fiber.Ctx) error {
|
||||
m := new(TagReq)
|
||||
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.DeleteTag(m.Tag, 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))
|
||||
}
|
||||
|
||||
// CreateCategoryForTag 为Tag 新增分组
|
||||
func CreateCategoryForTag(ctx *fiber.Ctx) error {
|
||||
m := new(TagCategoriesReq)
|
||||
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.CreateCategoryForTag(m.Relation, 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))
|
||||
}
|
||||
|
||||
// DeleteCategoryForTag 为Tag 删除分组
|
||||
func DeleteCategoryForTag(ctx *fiber.Ctx) error {
|
||||
m := new(TagCategoriesReq)
|
||||
if err := ctx.BodyParser(m); err != nil {
|
||||
ctxLogger.FError(ctx, error_process.BodyParserErr, zap.String("", fmt.Sprintf("%+v", err)))
|
||||
return ctx.JSON(fail(nil))
|
||||
}
|
||||
err := service.DelCategoryForTag(m.Relation, 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))
|
||||
}
|
||||
Reference in New Issue
Block a user