Files
knowledge-graph-agent/backend/handlers/node_handler.go
T

70 lines
1.3 KiB
Go
Raw Normal View History

2026-04-05 15:03:56 +08:00
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"knowledge-graph-backend/services"
)
type NodeHandler struct {
service services.GraphService
2026-04-05 15:03:56 +08:00
}
func NewNodeHandler(service services.GraphService) *NodeHandler {
2026-04-05 15:03:56 +08:00
return &NodeHandler{
service: service,
2026-04-05 15:03:56 +08:00
}
}
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)
2026-04-05 15:03:56 +08:00
if !found {
c.JSON(http.StatusNotFound, gin.H{
"error": "Not Found",
"message": "Node with ID '" + nodeID + "' not found",
})
return
}
c.JSON(http.StatusOK, node)
}
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)
2026-04-05 15:03:56 +08:00
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"),
})
}