提交 4448e3b1 编写于 作者: 5 520MianXiangDuiXiang520

:art:Improve structure

上级 70d84c9f
...@@ -11,5 +11,6 @@ func Register(c *gin.Engine) { ...@@ -11,5 +11,6 @@ func Register(c *gin.Engine) {
utils.HandlerRoute(c, "api/tag", routes.TagRegister) utils.HandlerRoute(c, "api/tag", routes.TagRegister)
utils.HandlerRoute(c, "api/talking", routes.TalkingRegister) utils.HandlerRoute(c, "api/talking", routes.TalkingRegister)
utils.HandlerRoute(c, "api/admin", routes.AdminRegister) utils.HandlerRoute(c, "api/admin", routes.AdminRegister)
utils.HandlerRoute(c, "api/friendship", routes.FriendShipRoutes)
} }
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
"net/http" "net/http"
) )
func CheckList(ctx *gin.Context) (utils.RespHeader, error) { func ListCheck(ctx *gin.Context) (utils.RespHeader, error) {
req := new(message.ListReq) req := new(message.ListReq)
var resp *message.BaseRespHeader var resp *message.BaseRespHeader
err := ctx.ShouldBindJSON(&req) err := ctx.ShouldBindJSON(&req)
......
...@@ -8,9 +8,9 @@ import ( ...@@ -8,9 +8,9 @@ import (
) )
func ArticleRegister(rg *gin.RouterGroup) { func ArticleRegister(rg *gin.RouterGroup) {
rg.POST("/list", utils.EasyHandler(&utils.LogicContext{CheckFunc: check.CheckList, LogicFunc: server.ListLogic})) rg.POST("/list", utils.EasyHandler(check.ListCheck, server.ListLogic))
rg.POST("/add", utils.EasyHandler(&utils.LogicContext{CheckFunc: check.CheckList, LogicFunc: server.ListLogic})) rg.POST("/add", utils.EasyHandler(check.ListCheck, server.ListLogic))
rg.POST("/update", utils.EasyHandler(&utils.LogicContext{CheckFunc: check.CheckList, LogicFunc: server.ListLogic})) rg.POST("/update", utils.EasyHandler(check.ListCheck, server.ListLogic))
rg.POST("/delete", utils.EasyHandler(&utils.LogicContext{CheckFunc: check.CheckList, LogicFunc: server.ListLogic})) rg.POST("/delete", utils.EasyHandler(check.ListCheck, server.ListLogic))
rg.POST("/detail", utils.EasyHandler(&utils.LogicContext{CheckFunc: check.CheckList, LogicFunc: server.ListLogic})) rg.POST("/detail", utils.EasyHandler(check.ListCheck, server.ListLogic))
} }
...@@ -5,25 +5,19 @@ import ( ...@@ -5,25 +5,19 @@ import (
"net/http" "net/http"
) )
type RespHeader interface {} type RespHeader interface{}
type LogicContext struct { type CheckFunc func(ctx *gin.Context) (RespHeader, error)
CheckFunc func(ctx *gin.Context) (RespHeader, error) type LogicFunc func(ctx *gin.Context) RespHeader
LogicFunc func(ctx *gin.Context) RespHeader
}
func (lc *LogicContext) do() gin.HandlerFunc { func EasyHandler(cf CheckFunc, lf LogicFunc) gin.HandlerFunc {
return func(context *gin.Context) { return func(context *gin.Context) {
var resp interface{} var resp interface{}
if checkResp, err := lc.CheckFunc(context); err != nil { if checkResp, err := cf(context); err != nil {
resp = checkResp resp = checkResp
} else { } else {
resp = lc.LogicFunc(context) resp = lf(context)
} }
context.JSON(http.StatusOK, resp) context.JSON(http.StatusOK, resp)
} }
} }
func EasyHandler(lc *LogicContext) gin.HandlerFunc {
return lc.do()
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册