Files
X-Agents/server/internal/handler/tool_handler.go
DESKTOP-72TV0V4\caoxiaozhu ecb885ee5e feat: 后端认证和工具模块更新
- main.go: 添加 Swagger 文档、初始化默认管理员
- 认证模块: 完善用户角色管理
- 新增工具模块: tool_handler, tool_repo, tool_service, tool model
- 更新 go.mod 依赖

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 14:25:55 +08:00

167 lines
4.3 KiB
Go

package handler
import (
"net/http"
"x-agents/server/internal/model"
"x-agents/server/internal/service"
"github.com/gin-gonic/gin"
)
// ToolHandler 工具处理器
type ToolHandler struct {
toolService *service.ToolService
}
// NewToolHandler 创建工具处理器
func NewToolHandler(toolService *service.ToolService) *ToolHandler {
return &ToolHandler{toolService: toolService}
}
// List 获取工具列表
// @Summary 获取工具列表
// @Description 获取所有工具列表,支持按分类和状态筛选
// @Tags 工具管理
// @Accept json
// @Produce json
// @Param category query string false "工具分类"
// @Param status query string false "工具状态"
// @Success 200 {object} map[string]interface{}
// @Router /tool/list [get]
func (h *ToolHandler) List(c *gin.Context) {
category := c.Query("category")
status := c.Query("status")
tools, err := h.toolService.GetTools(category, status)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"list": tools, "total": len(tools)})
}
// Sync 手动同步工具
// @Summary 手动同步工具
// @Description 从代码中的默认配置同步工具到数据库
// @Tags 工具管理
// @Accept json
// @Produce json
// @Success 200 {object} map[string]interface{}
// @Router /tool/sync [get]
func (h *ToolHandler) Sync(c *gin.Context) {
if err := h.toolService.InitDefaultTools(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
tools, _ := h.toolService.GetTools("", "")
c.JSON(http.StatusOK, gin.H{"message": "tools synced", "count": len(tools)})
}
// GetByID 根据ID获取工具
// @Summary 获取工具详情
// @Description 根据ID获取工具详情
// @Tags 工具管理
// @Accept json
// @Produce json
// @Param id path string true "工具ID"
// @Success 200 {object} map[string]interface{}
// @Router /tool/{id} [get]
func (h *ToolHandler) GetByID(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "tool id is required"})
return
}
tool, err := h.toolService.GetToolByID(id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "tool not found"})
return
}
c.JSON(http.StatusOK, gin.H{"tool": tool})
}
// Create 创建工具
// @Summary 创建工具
// @Description 创建新的工具
// @Tags 工具管理
// @Accept json
// @Produce json
// @Param tool body model.Tool true "工具信息"
// @Success 200 {object} map[string]interface{}
// @Router /tool/add [post]
func (h *ToolHandler) Create(c *gin.Context) {
var tool model.Tool
if err := c.ShouldBindJSON(&tool); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := h.toolService.CreateTool(&tool); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "tool created", "tool": tool})
}
// Update 更新工具
// @Summary 更新工具
// @Description 更新工具信息
// @Tags 工具管理
// @Accept json
// @Produce json
// @Param id path string true "工具ID"
// @Param tool body model.Tool true "工具信息"
// @Success 200 {object} map[string]interface{}
// @Router /tool/{id} [put]
func (h *ToolHandler) Update(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "tool id is required"})
return
}
var tool model.Tool
if err := c.ShouldBindJSON(&tool); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
tool.ID = id
if err := h.toolService.UpdateTool(&tool); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "tool updated"})
}
// Delete 删除工具
// @Summary 删除工具
// @Description 删除工具
// @Tags 工具管理
// @Accept json
// @Produce json
// @Param id path string true "工具ID"
// @Success 200 {object} map[string]interface{}
// @Router /tool/{id} [delete]
func (h *ToolHandler) Delete(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "tool id is required"})
return
}
if err := h.toolService.DeleteTool(id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "tool deleted"})
}