- 新增 Go 语言后端服务(server/),包含用户认证、Agent管理、数据库连接等API - 新增 Python Agent 服务(agent/),实现Agent核心逻辑和工具集 - 前端从原生HTML迁移到Vue.js框架(web/src/) - 添加 Docker Compose 支持(docker-compose.yml) - 添加项目架构文档(docs/ARCHITECTURE.md) - 添加环境变量示例(.env.example)和本地启动脚本(start-local.ps1) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
133 lines
2.9 KiB
Go
133 lines
2.9 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}
|
|
}
|
|
|
|
// Create 创建子表信息
|
|
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)
|
|
}
|
|
|
|
// GetByID 获取详情
|
|
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)
|
|
}
|
|
|
|
// ListByDatabase 获取数据库下所有子表
|
|
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})
|
|
}
|
|
|
|
// GetMappingFromFile 从文件获取映射
|
|
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})
|
|
}
|
|
|
|
// Update 更新
|
|
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)
|
|
}
|
|
|
|
// 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"})
|
|
}
|
|
|
|
// GetTablesDDL 获取数据库下所有表及DDL
|
|
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})
|
|
}
|