feat: 知识图谱测试api
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"knowledge-graph-backend/services"
|
||||
)
|
||||
|
||||
type GraphHandler struct {
|
||||
dataService *services.DataService
|
||||
}
|
||||
|
||||
func NewGraphHandler(dataService *services.DataService) *GraphHandler {
|
||||
return &GraphHandler{
|
||||
dataService: dataService,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *GraphHandler) GetGraphData(c *gin.Context) {
|
||||
data := h.dataService.GetGraphData()
|
||||
c.JSON(http.StatusOK, data)
|
||||
}
|
||||
|
||||
func (h *GraphHandler) GetStats(c *gin.Context) {
|
||||
stats := h.dataService.GetStats()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"stats": stats,
|
||||
"timestamp": gin.H{
|
||||
"totalNodes": stats["totalNodes"],
|
||||
"totalEdges": stats["totalEdges"],
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"knowledge-graph-backend/services"
|
||||
)
|
||||
|
||||
type NodeHandler struct {
|
||||
dataService *services.DataService
|
||||
}
|
||||
|
||||
func NewNodeHandler(dataService *services.DataService) *NodeHandler {
|
||||
return &NodeHandler{
|
||||
dataService: dataService,
|
||||
}
|
||||
}
|
||||
|
||||
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.dataService.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)
|
||||
}
|
||||
|
||||
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.dataService.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,33 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"knowledge-graph-backend/services"
|
||||
)
|
||||
|
||||
type SearchHandler struct {
|
||||
dataService *services.DataService
|
||||
}
|
||||
|
||||
func NewSearchHandler(dataService *services.DataService) *SearchHandler {
|
||||
return &SearchHandler{
|
||||
dataService: dataService,
|
||||
}
|
||||
}
|
||||
|
||||
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.dataService.SearchNodes(query)
|
||||
|
||||
c.JSON(http.StatusOK, results)
|
||||
}
|
||||
Reference in New Issue
Block a user