Files
X-Agents/server/internal/handler/sub_table_handler.go
DESKTOP-72TV0V4\caoxiaozhu fdd6b2c17d fix: 优化后端各模块 handler
- database_handler, knowledge_handler, model_handler
- neo4j_handler, sub_table_handler
- system_handler, upload_handler
- knowledge_service, upload_service

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

192 lines
5.0 KiB
Go

package handler
import (
"net/http"
"x-agents/server/internal/model"
"x-agents/server/internal/service"
"github.com/gin-gonic/gin"
)
type SubTableHandler struct {
service *service.SubTableService
}
func NewSubTableHandler(svc *service.SubTableService) *SubTableHandler {
return &SubTableHandler{service: svc}
}
// @Summary 创建子表映射
// @Description 添加数据库的子表映射关系
// @Tags 子表管理
// @Accept json
// @Produce json
// @Param request body model.CreateSubTableRequest true "子表信息"
// @Success 201 {object} model.SubTableInfo
// @Failure 400 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /sub-table/add [post]
func (h *SubTableHandler) Create(c *gin.Context) {
var req model.CreateSubTableRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
info, err := h.service.Create(req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, info)
}
// @Summary 获取子表详情
// @Description 根据ID获取子表映射详情
// @Tags 子表管理
// @Accept json
// @Produce json
// @Param id path string true "子表ID"
// @Success 200 {object} model.SubTableInfo
// @Failure 404 {object} map[string]string
// @Router /sub-table/{id} [get]
func (h *SubTableHandler) GetByID(c *gin.Context) {
id := c.Param("id")
info, err := h.service.GetByID(id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
return
}
c.JSON(http.StatusOK, info)
}
// @Summary 获取数据库下所有子表
// @Description 获取指定数据库的所有子表映射列表
// @Tags 子表管理
// @Accept json
// @Produce json
// @Param database_id path string true "数据库ID"
// @Success 200 {object} map[string]interface{}
// @Failure 500 {object} map[string]string
// @Router /sub-table/database/{database_id} [get]
func (h *SubTableHandler) ListByDatabase(c *gin.Context) {
databaseID := c.Param("database_id")
list, err := h.service.ListByDatabaseID(databaseID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if list == nil {
list = []model.SubTableInfo{}
}
c.JSON(http.StatusOK, gin.H{"list": list})
}
// @Summary 从文件获取映射
// @Description 从文件中读取子表映射关系
// @Tags 子表管理
// @Accept json
// @Produce json
// @Param database_id path string true "数据库ID"
// @Success 200 {object} map[string]interface{}
// @Failure 500 {object} map[string]string
// @Router /sub-table/mapping/{database_id} [get]
func (h *SubTableHandler) GetMappingFromFile(c *gin.Context) {
databaseID := c.Param("database_id")
mapping, err := h.service.GetMappingFromFile(databaseID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if mapping == nil {
c.JSON(http.StatusOK, gin.H{"mapping": nil, "message": "no mapping file found"})
return
}
c.JSON(http.StatusOK, gin.H{"mapping": mapping})
}
// @Summary 更新子表映射
// @Description 更新子表映射信息
// @Tags 子表管理
// @Accept json
// @Produce json
// @Param id path string true "子表ID"
// @Param request body model.UpdateSubTableRequest true "更新信息"
// @Success 200 {object} model.SubTableInfo
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /sub-table/{id} [put]
func (h *SubTableHandler) Update(c *gin.Context) {
id := c.Param("id")
var req model.UpdateSubTableRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
info, err := h.service.Update(id, req)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
return
}
c.JSON(http.StatusOK, info)
}
// @Summary 删除子表映射
// @Description 删除指定的子表映射
// @Tags 子表管理
// @Accept json
// @Produce json
// @Param id path string true "子表ID"
// @Success 200 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /sub-table/{id} [delete]
func (h *SubTableHandler) Delete(c *gin.Context) {
id := c.Param("id")
err := h.service.Delete(id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
}
// @Summary 获取表结构DDL
// @Description 获取数据库下所有表的DDL语句
// @Tags 子表管理
// @Accept json
// @Produce json
// @Param database_id path string true "数据库ID"
// @Success 200 {object} map[string]interface{}
// @Failure 500 {object} map[string]string
// @Router /sub-table/ddl/{database_id} [get]
func (h *SubTableHandler) GetTablesDDL(c *gin.Context) {
databaseID := c.Param("database_id")
tables, err := h.service.GetTableDDLFromDatabase(databaseID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if tables == nil {
tables = []model.TableDDLInfo{}
}
c.JSON(http.StatusOK, gin.H{"tables": tables})
}