Refactor controllers to use httpx for JSON responses and eliminate context dependency
- Updated TagController methods to use httpx.WriteJSON for consistent JSON response handling. - Refactored TicketController to replace context-dependent methods with standalone functions using httpx. - Modified UserController to utilize httpx for error handling and response formatting. - Adjusted WechatController to remove context dependency and standardize response handling.
This commit is contained in:
@@ -2,6 +2,7 @@ package dashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cs-agent/internal/pkg/httpx"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -13,25 +14,23 @@ import (
|
||||
"cs-agent/internal/services"
|
||||
|
||||
"cs-agent/internal/pkg/httpx/params"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mlogclub/simple/web"
|
||||
)
|
||||
|
||||
type SkillDefinitionController struct {
|
||||
Ctx *gin.Context
|
||||
}
|
||||
|
||||
func (c *SkillDefinitionController) AnyList() *web.JsonResult {
|
||||
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil {
|
||||
return web.JsonError(err)
|
||||
func SkillDefinitionAnyList(ctx *gin.Context) {
|
||||
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionView); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
cnd := params.NewPagedSqlCnd(c.Ctx,
|
||||
cnd := params.NewPagedSqlCnd(ctx,
|
||||
params.QueryFilter{ParamName: "status"},
|
||||
params.QueryFilter{ParamName: "name", Op: params.Like},
|
||||
params.QueryFilter{ParamName: "code", Op: params.Like},
|
||||
).Desc("id")
|
||||
if _, ok := params.Get(c.Ctx, "status"); !ok {
|
||||
if _, ok := params.Get(ctx, "status"); !ok {
|
||||
cnd.Where("status <> ?", enums.StatusDeleted)
|
||||
}
|
||||
list, paging := services.SkillDefinitionService.FindPageByCnd(cnd)
|
||||
@@ -39,18 +38,20 @@ func (c *SkillDefinitionController) AnyList() *web.JsonResult {
|
||||
for _, item := range list {
|
||||
results = append(results, builders.BuildSkillDefinitionResponse(&item))
|
||||
}
|
||||
return web.JsonData(&web.PageResult{Results: results, Page: paging})
|
||||
httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging})
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SkillDefinitionController) GetList_all() *web.JsonResult {
|
||||
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil {
|
||||
return web.JsonError(err)
|
||||
func SkillDefinitionGetList_all(ctx *gin.Context) {
|
||||
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionView); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
cnd := params.NewSqlCnd(c.Ctx,
|
||||
cnd := params.NewSqlCnd(ctx,
|
||||
params.QueryFilter{ParamName: "status"},
|
||||
).Desc("id")
|
||||
if status, ok := params.Get(c.Ctx, "status"); !ok || strings.TrimSpace(status) == "" {
|
||||
if status, ok := params.Get(ctx, "status"); !ok || strings.TrimSpace(status) == "" {
|
||||
cnd.Where("status <> ?", enums.StatusDeleted)
|
||||
}
|
||||
list := services.SkillDefinitionService.Find(cnd)
|
||||
@@ -58,79 +59,98 @@ func (c *SkillDefinitionController) GetList_all() *web.JsonResult {
|
||||
for _, item := range list {
|
||||
results = append(results, builders.BuildSkillDefinitionResponse(&item))
|
||||
}
|
||||
return web.JsonData(results)
|
||||
httpx.WriteJSON(ctx, results)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SkillDefinitionController) GetBy(id int64) *web.JsonResult {
|
||||
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil {
|
||||
return web.JsonError(err)
|
||||
func SkillDefinitionGetBy(ctx *gin.Context, id int64) {
|
||||
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionView); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
item := services.SkillDefinitionService.Get(id)
|
||||
if item == nil {
|
||||
return web.JsonErrorMsg("Skill 不存在")
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill 不存在"))
|
||||
return
|
||||
}
|
||||
return web.JsonData(builders.BuildSkillDefinitionResponse(item))
|
||||
httpx.WriteJSON(ctx, builders.BuildSkillDefinitionResponse(item))
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SkillDefinitionController) PostCreate() *web.JsonResult {
|
||||
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionCreate)
|
||||
func SkillDefinitionPostCreate(ctx *gin.Context) {
|
||||
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionCreate)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
req := request.CreateSkillDefinitionRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
item, err := services.SkillDefinitionService.CreateSkillDefinition(req, operator)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonData(builders.BuildSkillDefinitionResponse(item))
|
||||
httpx.WriteJSON(ctx, builders.BuildSkillDefinitionResponse(item))
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SkillDefinitionController) PostUpdate() *web.JsonResult {
|
||||
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionUpdate)
|
||||
func SkillDefinitionPostUpdate(ctx *gin.Context) {
|
||||
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionUpdate)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
req := request.UpdateSkillDefinitionRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := services.SkillDefinitionService.UpdateSkillDefinition(req, operator); err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonSuccess()
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SkillDefinitionController) PostUpdate_status() *web.JsonResult {
|
||||
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionUpdate)
|
||||
func SkillDefinitionPostUpdate_status(ctx *gin.Context) {
|
||||
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionUpdate)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
req := request.UpdateSkillDefinitionStatusRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
if req.ID <= 0 {
|
||||
return web.JsonErrorMsg("Skill ID 不合法")
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill ID 不合法"))
|
||||
return
|
||||
}
|
||||
if !enums.IsValidStatus(req.Status) {
|
||||
return web.JsonErrorMsg("状态值不合法")
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("状态值不合法"))
|
||||
return
|
||||
}
|
||||
item := services.SkillDefinitionService.Get(req.ID)
|
||||
if item == nil {
|
||||
return web.JsonErrorMsg("Skill 不存在")
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill 不存在"))
|
||||
return
|
||||
}
|
||||
if item.Status == enums.StatusDeleted {
|
||||
return web.JsonErrorMsg("已删除的 Skill 不能直接修改状态,请先恢复")
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("已删除的 Skill 不能直接修改状态,请先恢复"))
|
||||
return
|
||||
}
|
||||
if req.Status == int(enums.StatusDeleted) {
|
||||
return web.JsonErrorMsg("请使用删除接口处理删除状态")
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("请使用删除接口处理删除状态"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := services.SkillDefinitionService.Updates(req.ID, map[string]any{
|
||||
@@ -139,26 +159,32 @@ func (c *SkillDefinitionController) PostUpdate_status() *web.JsonResult {
|
||||
"update_user_name": operator.Username,
|
||||
"updated_at": time.Now(),
|
||||
}); err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonSuccess()
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SkillDefinitionController) PostDelete() *web.JsonResult {
|
||||
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionDelete)
|
||||
func SkillDefinitionPostDelete(ctx *gin.Context) {
|
||||
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionDelete)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
req := request.DeleteSkillDefinitionRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
if req.ID <= 0 {
|
||||
return web.JsonErrorMsg("Skill ID 不合法")
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill ID 不合法"))
|
||||
return
|
||||
}
|
||||
if services.SkillDefinitionService.Get(req.ID) == nil {
|
||||
return web.JsonErrorMsg("Skill 不存在")
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill 不存在"))
|
||||
return
|
||||
}
|
||||
if err := services.SkillDefinitionService.Updates(req.ID, map[string]any{
|
||||
"status": enums.StatusDeleted,
|
||||
@@ -166,31 +192,38 @@ func (c *SkillDefinitionController) PostDelete() *web.JsonResult {
|
||||
"update_user_name": operator.Username,
|
||||
"updated_at": time.Now(),
|
||||
}); err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonSuccess()
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SkillDefinitionController) PostRestore() *web.JsonResult {
|
||||
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionDelete)
|
||||
func SkillDefinitionPostRestore(ctx *gin.Context) {
|
||||
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionDelete)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
req := request.RestoreSkillDefinitionRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
if req.ID <= 0 {
|
||||
return web.JsonErrorMsg("Skill ID 不合法")
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill ID 不合法"))
|
||||
return
|
||||
}
|
||||
|
||||
item := services.SkillDefinitionService.Get(req.ID)
|
||||
if item == nil {
|
||||
return web.JsonErrorMsg("Skill 不存在")
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill 不存在"))
|
||||
return
|
||||
}
|
||||
if item.Status != enums.StatusDeleted {
|
||||
return web.JsonErrorMsg("仅已删除的 Skill 支持恢复")
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("仅已删除的 Skill 支持恢复"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := services.SkillDefinitionService.Updates(req.ID, map[string]any{
|
||||
@@ -199,39 +232,49 @@ func (c *SkillDefinitionController) PostRestore() *web.JsonResult {
|
||||
"update_user_name": operator.Username,
|
||||
"updated_at": time.Now(),
|
||||
}); err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonSuccess()
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SkillDefinitionController) PostDebug_run() *web.JsonResult {
|
||||
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil {
|
||||
return web.JsonError(err)
|
||||
func SkillDefinitionPostDebug_run(ctx *gin.Context) {
|
||||
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionView); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
req := request.SkillDebugRunRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
resp, err := services.SkillRuntimeService.DebugRun(context.Background(), req)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonData(resp)
|
||||
httpx.WriteJSON(ctx, resp)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SkillDefinitionController) PostDebug_resume() *web.JsonResult {
|
||||
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil {
|
||||
return web.JsonError(err)
|
||||
func SkillDefinitionPostDebug_resume(ctx *gin.Context) {
|
||||
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionView); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
req := request.SkillDebugResumeRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
resp, err := services.SkillRuntimeService.DebugResume(context.Background(), req)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonData(resp)
|
||||
httpx.WriteJSON(ctx, resp)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user