// Package handler provides HTTP handlers for various endpoints. package handler import ( "fmt" "net/http" "modelRT/database" "modelRT/diagram" "modelRT/logger" "modelRT/network" "github.com/bitly/go-simplejson" "github.com/gin-gonic/gin" "github.com/gofrs/uuid" ) // CircuitDiagramCreateHandler define circuit diagram create process API func CircuitDiagramCreateHandler(c *gin.Context) { pgClient := database.GetPostgresDBClient() var request network.CircuitDiagramCreateRequest if err := c.ShouldBindJSON(&request); err != nil { logger.Error(c, "unmarshal circuit diagram create info failed", "error", err) resp := network.FailureResponse{ Code: http.StatusBadRequest, Msg: err.Error(), } c.JSON(http.StatusOK, resp) return } graph, err := diagram.GetGraphMap(request.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": request.PageID, }, } c.JSON(http.StatusOK, resp) return } var topologicCreateInfos []network.TopologicUUIDCreateInfo for _, topologicLink := range request.TopologicLinks { var createInfo network.TopologicUUIDCreateInfo UUIDFrom, err1 := uuid.FromString(topologicLink.UUIDFrom) UUIDTo, err2 := uuid.FromString(topologicLink.UUIDTo) if err1 != nil || err2 != nil { var err error if err1 != nil && err2 == nil { err = fmt.Errorf("convert uuid from string failed:%w", err1) } else if err1 == nil && err2 != nil { err = fmt.Errorf("convert uuid from string failed:%w", err2) } else { err = fmt.Errorf("convert uuid from string failed:%w:%w", err1, err2) } logger.Error(c, "format uuid from string failed", "error", err) resp := network.FailureResponse{ Code: http.StatusBadRequest, Msg: err.Error(), PayLoad: map[string]interface{}{ "topologic_info": topologicLink, }, } c.JSON(http.StatusOK, resp) return } createInfo.UUIDFrom = UUIDFrom createInfo.UUIDTo = UUIDTo topologicCreateInfos = append(topologicCreateInfos, createInfo) } // open transaction tx := pgClient.Begin() err = database.CreateTopologicIntoDB(c, tx, request.PageID, topologicCreateInfos) if err != nil { tx.Rollback() logger.Error(c, "create topologic info into DB failed", "topologic_info", topologicCreateInfos, "error", err) resp := network.FailureResponse{ Code: http.StatusBadRequest, Msg: err.Error(), PayLoad: map[string]interface{}{ "topologic_infos": topologicCreateInfos, }, } c.JSON(http.StatusOK, resp) return } for _, topologicCreateInfo := range topologicCreateInfos { graph.AddEdge(topologicCreateInfo.UUIDFrom, topologicCreateInfo.UUIDTo) } for index, componentInfo := range request.ComponentInfos { componentID, err := database.CreateComponentIntoDB(c, tx, componentInfo) if err != nil { tx.Rollback() logger.Error(c, "insert component info into DB failed", "error", err) resp := network.FailureResponse{ Code: http.StatusBadRequest, Msg: err.Error(), PayLoad: map[string]interface{}{ "component_infos": request.ComponentInfos, }, } c.JSON(http.StatusOK, resp) return } request.ComponentInfos[index].ID = componentID err = database.CreateModelIntoDB(c, tx, componentID, componentInfo.ComponentType, componentInfo.Params) if err != nil { tx.Rollback() logger.Error(c, "create component model into DB failed", "component_infos", request.ComponentInfos, "error", err) resp := network.FailureResponse{ Code: http.StatusBadRequest, Msg: err.Error(), PayLoad: map[string]interface{}{ "uuid": request.PageID, "component_infos": request.ComponentInfos, }, } c.JSON(http.StatusOK, resp) return } } for _, componentInfo := range request.ComponentInfos { paramsJSON, err := simplejson.NewJson([]byte(componentInfo.Params)) if err != nil { tx.Rollback() logger.Error(c, "unmarshal component params info failed", "component_params", componentInfo.Params, "error", err) resp := network.FailureResponse{ Code: http.StatusBadRequest, Msg: err.Error(), PayLoad: map[string]interface{}{ "uuid": componentInfo.UUID, "component_params": componentInfo.Params, }, } c.JSON(http.StatusOK, resp) return } componentMap, err := paramsJSON.Map() if err != nil { tx.Rollback() logger.Error(c, "format params json info to map failed", "error", err) resp := network.FailureResponse{ Code: http.StatusBadRequest, Msg: err.Error(), PayLoad: map[string]interface{}{ "uuid": componentInfo.UUID, "component_params": componentInfo.Params, }, } c.JSON(http.StatusOK, resp) return } diagram.StoreComponentMap(componentInfo.ID, componentMap) } if len(request.FreeVertexs) > 0 { for _, freeVertex := range request.FreeVertexs { graph.FreeVertexs[freeVertex] = struct{}{} } } // commit transaction tx.Commit() resp := network.SuccessResponse{ Code: http.StatusOK, Msg: "success", PayLoad: map[string]interface{}{ "page_id": request.PageID, }, } c.JSON(http.StatusOK, resp) }