refactor: 按go modules规范重构

This commit is contained in:
2026-04-15 15:19:39 +08:00
parent 8c5cd3705a
commit 5d4bf761b8
21 changed files with 524 additions and 679 deletions
+202
View File
@@ -0,0 +1,202 @@
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"knowledge-graph-backend/internal/model"
"knowledge-graph-backend/internal/service"
)
// NodeCRUDHandler 节点的增删改处理器
type NodeCRUDHandler struct {
service service.GraphService
}
// NewNodeCRUDHandler 创建节点CRUD处理器
func NewNodeCRUDHandler(svc service.GraphService) *NodeCRUDHandler {
return &NodeCRUDHandler{
service: svc,
}
}
// EdgeCRUDHandler 边的增删改处理器
type EdgeCRUDHandler struct {
service service.GraphService
}
// NewEdgeCRUDHandler 创建边CRUD处理器
func NewEdgeCRUDHandler(svc service.GraphService) *EdgeCRUDHandler {
return &EdgeCRUDHandler{
service: svc,
}
}
// CreateNode 创建新节点
// @Summary 创建节点
// @Description 在知识图谱中创建一个新节点
// @Accept json
// @Produce json
// @Param request body model.CreateNodeRequest true "节点创建请求"
// @Success 200 {object} model.Node "创建成功"
// @Failure 400 {object} model.ErrorResponse "请求错误"
// @Failure 409 {object} model.ErrorResponse "节点已存在"
// @Failure 500 {object} model.ErrorResponse "内部错误"
// @Router /api/nodes [post]
func (h *NodeCRUDHandler) CreateNode(c *gin.Context) {
var req model.CreateNodeRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.ErrorResponse{
Error: "invalid_request",
Message: err.Error(),
})
return
}
node, err := h.service.CreateNode(req)
if err != nil {
c.JSON(http.StatusConflict, model.ErrorResponse{
Error: "create_failed",
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, node)
}
// UpdateNode 更新节点
// @Summary 更新节点
// @Description 更新知识图谱中指定节点的信息
// @Accept json
// @Produce json
// @Param id path string true "节点ID"
// @Param request body model.UpdateNodeRequest true "节点更新请求"
// @Success 200 {object} model.Node "更新成功"
// @Failure 400 {object} model.ErrorResponse "请求错误"
// @Failure 404 {object} model.ErrorResponse "节点未找到"
// @Failure 500 {object} model.ErrorResponse "内部错误"
// @Router /api/nodes/{id} [put]
func (h *NodeCRUDHandler) UpdateNode(c *gin.Context) {
id := c.Param("id")
var req model.UpdateNodeRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.ErrorResponse{
Error: "invalid_request",
Message: err.Error(),
})
return
}
node, err := h.service.UpdateNode(id, req)
if err != nil {
c.JSON(http.StatusNotFound, model.ErrorResponse{
Error: "update_failed",
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, node)
}
// DeleteNode 删除节点
// @Summary 删除节点
// @Description 从知识图谱中删除指定节点及其相关的所有边
// @Produce json
// @Param id path string true "节点ID"
// @Success 200 {object} model.DeleteResponse "删除成功"
// @Failure 404 {object} model.ErrorResponse "节点未找到"
// @Failure 500 {object} model.ErrorResponse "内部错误"
// @Router /api/nodes/{id} [delete]
func (h *NodeCRUDHandler) DeleteNode(c *gin.Context) {
id := c.Param("id")
err := h.service.DeleteNode(id)
if err != nil {
c.JSON(http.StatusNotFound, model.ErrorResponse{
Error: "delete_failed",
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, model.DeleteResponse{
Success: true,
Message: "节点已成功删除",
ID: id,
})
}
// CreateEdge 创建新边
// @Summary 创建边
// @Description 在知识图谱中创建一个新边(关系)
// @Accept json
// @Produce json
// @Param request body model.CreateEdgeRequest true "边创建请求"
// @Success 200 {object} model.EdgeResponse "创建成功"
// @Failure 400 {object} model.ErrorResponse "请求错误"
// @Failure 404 {object} model.ErrorResponse "源节点或目标节点未找到"
// @Failure 409 {object} model.ErrorResponse "边已存在"
// @Failure 500 {object} model.ErrorResponse "内部错误"
// @Router /api/edges [post]
func (h *EdgeCRUDHandler) CreateEdge(c *gin.Context) {
var req model.CreateEdgeRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.ErrorResponse{
Error: "invalid_request",
Message: err.Error(),
})
return
}
edge, err := h.service.CreateEdge(req)
if err != nil {
c.JSON(http.StatusConflict, model.ErrorResponse{
Error: "create_failed",
Message: err.Error(),
})
return
}
// 转换为响应格式
response := model.EdgeResponse{
ID: edge.ID,
Source: edge.Source,
Target: edge.Target,
Label: edge.Label,
Type: edge.Type,
Properties: edge.Properties,
}
c.JSON(http.StatusOK, response)
}
// DeleteEdge 删除边
// @Summary 删除边
// @Description 从知识图谱中删除指定的边
// @Produce json
// @Param id path string true "边ID"
// @Success 200 {object} model.DeleteResponse "删除成功"
// @Failure 404 {object} model.ErrorResponse "边未找到"
// @Failure 500 {object} model.ErrorResponse "内部错误"
// @Router /api/edges/{id} [delete]
func (h *EdgeCRUDHandler) DeleteEdge(c *gin.Context) {
edgeID := c.Param("id")
err := h.service.DeleteEdge(edgeID)
if err != nil {
c.JSON(http.StatusNotFound, model.ErrorResponse{
Error: "delete_failed",
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, model.DeleteResponse{
Success: true,
Message: "边已成功删除",
ID: edgeID,
})
}
+51
View File
@@ -0,0 +1,51 @@
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"knowledge-graph-backend/internal/service"
)
type GraphHandler struct {
service service.GraphService
}
func NewGraphHandler(svc service.GraphService) *GraphHandler {
return &GraphHandler{
service: svc,
}
}
// @Summary 获取图数据
// @Description 获取知识图谱的全部节点和边数据
// @Produce json
// @Success 200 {object} model.GraphData "成功"
// @Failure 500 {object} model.ErrorResponse "内部错误"
// @Router /api/graph [get]
func (h *GraphHandler) GetGraphData(c *gin.Context) {
data := h.service.GetGraphData()
c.JSON(http.StatusOK, data)
}
// @Summary 获取图统计信息
// @Description 获取知识图谱的节点数、边数等统计信息
// @Produce json
// @Success 200 {object} map[string]interface{} "成功"
// @Failure 500 {object} model.ErrorResponse "内部错误"
// @Router /api/graph/stats [get]
func (h *GraphHandler) GetStats(c *gin.Context) {
stats := h.service.GetStats()
c.JSON(http.StatusOK, stats)
}
// @Summary 获取简化图数据
// @Description 获取知识图谱的全部节点和边数据(仅保留核心信息,适合LLM处理)
// @Produce json
// @Success 200 {object} model.SimpleGraphData "成功"
// @Failure 500 {object} model.ErrorResponse "内部错误"
// @Router /api/graph/simpleJson [get]
func (h *GraphHandler) GetSimpleGraphData(c *gin.Context) {
data := h.service.GetSimpleGraphData()
c.JSON(http.StatusOK, data)
}
+87
View File
@@ -0,0 +1,87 @@
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"knowledge-graph-backend/internal/service"
)
type NodeHandler struct {
service service.GraphService
}
func NewNodeHandler(svc service.GraphService) *NodeHandler {
return &NodeHandler{
service: svc,
}
}
// @Summary 获取节点详情
// @Description 根据节点 ID 获取知识图谱中的节点详细信息
// @Produce json
// @Param id path string true "节点 ID"
// @Success 200 {object} model.NodeResponse "成功"
// @Failure 400 {object} model.ErrorResponse "请求错误"
// @Failure 404 {object} model.ErrorResponse "节点未找到"
// @Failure 500 {object} model.ErrorResponse "内部错误"
// @Router /api/nodes/{id} [get]
func (h *NodeHandler) GetNodeByID(c *gin.Context) {
nodeID := c.Param("id")
if nodeID == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Bad Request",
"message": "Node ID is required",
})
return
}
node, found := h.service.GetNodeByID(nodeID)
if !found {
c.JSON(http.StatusNotFound, gin.H{
"error": "Not Found",
"message": "Node with ID '" + nodeID + "' not found",
})
return
}
c.JSON(http.StatusOK, node)
}
// @Summary 获取节点邻居
// @Description 根据节点 ID 获取其所有邻居节点及关联边
// @Produce json
// @Param id path string true "节点 ID"
// @Success 200 {object} model.NeighborResponse "成功"
// @Failure 400 {object} model.ErrorResponse "请求错误"
// @Failure 404 {object} model.ErrorResponse "节点未找到"
// @Failure 500 {object} model.ErrorResponse "内部错误"
// @Router /api/nodes/{id}/neighbors [get]
func (h *NodeHandler) GetNeighbors(c *gin.Context) {
nodeID := c.Param("id")
if nodeID == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Bad Request",
"message": "Node ID is required",
})
return
}
neighbors, found := h.service.GetNeighbors(nodeID)
if !found {
c.JSON(http.StatusNotFound, gin.H{
"error": "Not Found",
"message": "Node with ID '" + nodeID + "' not found",
})
return
}
c.JSON(http.StatusOK, neighbors)
}
func (h *NodeHandler) AddCustomHeaders(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "With CORS headers",
"origin": c.Request.Header.Get("Origin"),
})
}
@@ -0,0 +1,41 @@
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"knowledge-graph-backend/internal/service"
)
type SearchHandler struct {
service service.GraphService
}
func NewSearchHandler(svc service.GraphService) *SearchHandler {
return &SearchHandler{
service: svc,
}
}
// @Summary 搜索节点
// @Description 根据关键词搜索知识图谱中的节点
// @Produce json
// @Param q query string true "搜索关键词"
// @Success 200 {object} model.SearchResponse "成功"
// @Failure 400 {object} model.ErrorResponse "请求错误"
// @Failure 500 {object} model.ErrorResponse "内部错误"
// @Router /api/search [get]
func (h *SearchHandler) SearchNodes(c *gin.Context) {
query := c.Query("q")
if query == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Bad Request",
"message": "Query parameter 'q' is required",
})
return
}
results := h.service.SearchNodes(query)
c.JSON(http.StatusOK, results)
}