88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"knowledge-graph-backend/services"
|
|
)
|
|
|
|
type NodeHandler struct {
|
|
service services.GraphService
|
|
}
|
|
|
|
func NewNodeHandler(service services.GraphService) *NodeHandler {
|
|
return &NodeHandler{
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
// @Summary 获取节点详情
|
|
// @Description 根据节点 ID 获取知识图谱中的节点详细信息
|
|
// @Produce json
|
|
// @Param id path string true "节点 ID"
|
|
// @Success 200 {object} models.NodeResponse "成功"
|
|
// @Failure 400 {object} models.ErrorResponse "请求错误"
|
|
// @Failure 404 {object} models.ErrorResponse "节点未找到"
|
|
// @Failure 500 {object} models.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} models.NeighborResponse "成功"
|
|
// @Failure 400 {object} models.ErrorResponse "请求错误"
|
|
// @Failure 404 {object} models.ErrorResponse "节点未找到"
|
|
// @Failure 500 {object} models.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"),
|
|
})
|
|
}
|