Files
2026-04-28 12:55:46 +08:00

77 lines
2.2 KiB
Go

package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"knowledge-graph-backend/internal/model"
"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, model.ErrorResponse{Error: "bad_request", Message: "Node ID is required"})
return
}
node, found := h.service.GetNodeByID(nodeID)
if !found {
c.JSON(http.StatusNotFound, model.ErrorResponse{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, model.ErrorResponse{Error: "bad_request", Message: "Node ID is required"})
return
}
neighbors, found := h.service.GetNeighbors(nodeID)
if !found {
c.JSON(http.StatusNotFound, model.ErrorResponse{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"),
})
}