modelRT/handler/circuit_diagram_load.go

139 lines
3.6 KiB
Go

// Package handler provides HTTP handlers for various endpoints.
package handler
import (
"net/http"
"strconv"
"modelRT/database"
"modelRT/diagram"
"modelRT/logger"
"modelRT/network"
"github.com/gin-gonic/gin"
)
// CircuitDiagramLoadHandler define circuit diagram load process API
// @Summary load circuit diagram info
// @Description load circuit diagram info by page id
// @Tags load circuit_diagram
// @Accept json
// @Produce json
// @Param page_id path int64 true "page ID"
// @Success 200 {object} network.SuccessResponse "request process success"
// @Failure 400 {object} network.FailureResponse "request process failed"
// @Router /model/diagram_load/{page_id} [get]
func CircuitDiagramLoadHandler(c *gin.Context) {
pgClient := database.GetPostgresDBClient()
pageID, err := strconv.ParseInt(c.Query("page_id"), 10, 64)
if err != nil {
logger.Error(c, "get pageID from url param failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{
"page_id": pageID,
},
}
c.JSON(http.StatusOK, resp)
return
}
topologicInfo, err := diagram.GetGraphMap(pageID)
if err != nil {
logger.Error(c, "get topologic data from set by pageID failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{
"page_id": pageID,
},
}
c.JSON(http.StatusOK, resp)
return
}
payLoad := make(map[string]interface{})
payLoad["root_vertex"] = topologicInfo.RootVertex
payLoad["topologic"] = topologicInfo.VerticeLinks
componentParamMap := make(map[string]any)
for _, VerticeLink := range topologicInfo.VerticeLinks {
for _, componentUUID := range VerticeLink {
component, err := database.QueryComponentByUUID(c, pgClient, componentUUID)
if err != nil {
logger.Error(c, "get component id info from DB by uuid failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{
"uuid": componentUUID,
},
}
c.JSON(http.StatusOK, resp)
return
}
componentParams, err := diagram.GetComponentMap(component.ID)
if err != nil {
logger.Error(c, "get component data from set by uuid failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{
"uuid": componentUUID,
},
}
c.JSON(http.StatusOK, resp)
return
}
componentParamMap[componentUUID.String()] = componentParams
}
}
rootVertexUUID := topologicInfo.RootVertex.String()
rootComponent, err := database.QueryComponentByUUID(c, pgClient, topologicInfo.RootVertex)
if err != nil {
logger.Error(c, "get component id info from DB by uuid failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{
"uuid": topologicInfo.RootVertex,
},
}
c.JSON(http.StatusOK, resp)
return
}
rootComponentParam, err := diagram.GetComponentMap(rootComponent.ID)
if err != nil {
logger.Error(c, "get component data from set by uuid failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{
"uuid": rootVertexUUID,
},
}
c.JSON(http.StatusOK, resp)
return
}
componentParamMap[rootVertexUUID] = rootComponentParam
payLoad["component_params"] = componentParamMap
resp := network.SuccessResponse{
Code: http.StatusOK,
Msg: "success",
PayLoad: payLoad,
}
c.JSON(http.StatusOK, resp)
}