optimize code of circuit diagram update API

This commit is contained in:
douxu 2024-11-29 16:34:03 +08:00
parent 16220a6dd7
commit 2df821f69a
5 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,25 @@
{
"page_id":1,
"topologics":[
{
"from_uuid":"12311-111",
"to_uuid":"12311-113"
},
{
"from_uuid":"12311-111",
"to_uuid":"12311-114"
}
],
"component_infos":[
{
"uuid":"12311-113",
"component_type":1,
"params":""
},
{
"uuid":"12311-114",
"component_type":1,
"params":""
}
]
}

View File

@ -27,6 +27,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
},
}
c.JSON(http.StatusOK, resp)
return
}
topologicInfo, err := diagram.GetGraphMap(pageID)
@ -40,6 +41,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
},
}
c.JSON(http.StatusOK, resp)
return
}
payLoad := make(map[string]interface{})
payLoad["root_vertex"] = topologicInfo.RootVertex
@ -61,6 +63,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
},
}
c.JSON(http.StatusOK, resp)
return
}
byteSlice, err := componentParams.MarshalJSON()
if err != nil {
@ -70,6 +73,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
ResponseHeader: header,
}
c.JSON(http.StatusOK, resp)
return
}
componentParamMap[UUIDStr] = byteSlice
}

View File

@ -0,0 +1,74 @@
package handler
import (
"fmt"
"net/http"
"modelRT/diagram"
"modelRT/log"
"modelRT/network"
"github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
"go.uber.org/zap"
)
// CircuitDiagramUpdateHandler define circuit diagram update process API
func CircuitDiagramUpdateHandler(c *gin.Context) {
logger := log.GetLoggerInstance()
var request network.CircuitDiagramUpdateRequest
if err := c.ShouldBindJSON(&request); err != nil {
logger.Error("unmarshal circuit diagram update info failed", zap.Error(err))
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
resp := network.BasicResponse{
ResponseHeader: header,
}
c.JSON(http.StatusOK, resp)
return
}
graph, err := diagram.GetGraphMap(request.PageID)
if err != nil {
logger.Error("get topologic data from set by pageID failed", zap.Error(err))
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
resp := network.BasicResponse{
ResponseHeader: header,
PayLoad: map[string]interface{}{
"page_id": request.PageID,
},
}
c.JSON(http.StatusOK, resp)
return
}
for _, newTopologicLink := range request.TopologicLinks {
fromUUID, err1 := uuid.FromString(newTopologicLink.FromUUID)
toUUID, err2 := uuid.FromString(newTopologicLink.ToUUID)
if err1 != nil || err2 != nil {
var err error
if err1 != nil {
err = fmt.Errorf("format from_uuid failed:%w", err1)
}
if err2 != nil {
err = fmt.Errorf("%s,format to_uuid failed:%w", err.Error(), err2)
}
logger.Error("format uuid from string failed", zap.Error(err))
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
resp := network.BasicResponse{
ResponseHeader: header,
PayLoad: map[string]interface{}{
"from_uuid": newTopologicLink.FromUUID,
"to_uuid": newTopologicLink.ToUUID,
},
}
c.JSON(http.StatusOK, resp)
return
}
graph.AddEdge(fromUUID, toUUID)
// TODO 修改 component 先关信息
fmt.Println(request.ComponentInfos)
}
}

22
network/request.go Normal file
View File

@ -0,0 +1,22 @@
// Package network define struct of network operation
package network
// TopologicChangeInfo defines circuit diagram topologic change info
type TopologicChangeInfo struct {
FromUUID string `json:"from_uuid"`
ToUUID string `json:"to_uuid"`
}
// ComponentInfo defines circuit diagram component params info
type ComponentInfo struct {
UUID string `json:"uuid"`
ComponentType int `json:"component_type"`
Params string `json:"params"`
}
// CircuitDiagramUpdateRequest defines request params of circuit diagram update api
type CircuitDiagramUpdateRequest struct {
PageID int64 `json:"page_id"`
TopologicLinks []TopologicChangeInfo `json:"topologics"`
ComponentInfos []ComponentInfo `json:"component_infos"`
}