modelRT/handler/anchor_point_replace.go

111 lines
3.0 KiB
Go
Raw Normal View History

2024-12-31 16:18:51 +08:00
// Package handler provides HTTP handlers for various endpoints.
package handler
import (
"context"
"fmt"
"net/http"
"time"
"modelRT/constant"
"modelRT/database"
"modelRT/diagram"
2024-12-25 16:34:57 +08:00
"modelRT/logger"
"modelRT/model"
"modelRT/network"
"modelRT/orm"
2024-12-25 16:34:57 +08:00
"github.com/gin-gonic/gin"
)
// ComponentAnchorReplaceHandler define component anchor point replace process API
func ComponentAnchorReplaceHandler(c *gin.Context) {
var uuid, anchorName string
pgClient := database.GetPostgresDBClient()
cancelCtx, cancel := context.WithTimeout(c, 5*time.Second)
defer cancel()
var request network.ComponetAnchorReplaceRequest
if err := c.ShouldBindJSON(&request); err != nil {
logger.Error(c, "unmarshal component anchor point replace info failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
}
c.JSON(http.StatusOK, resp)
return
}
uuid = request.UUID
anchorName = request.AnchorName
2024-12-23 14:47:22 +08:00
var componentInfo orm.Component
result := pgClient.WithContext(cancelCtx).Model(&orm.Component{}).Where("global_uuid = ?", uuid).Find(&componentInfo)
if result.Error != nil {
logger.Error(c, "query component detail info failed", "error", result.Error)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: result.Error.Error(),
}
c.JSON(http.StatusOK, resp)
return
}
2024-12-23 14:47:22 +08:00
if result.RowsAffected == 0 {
err := fmt.Errorf("query component detail info by uuid failed:%w", constant.ErrQueryRowZero)
logger.Error(c, "query component detail info from table is empty", "table_name", "component")
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
}
c.JSON(http.StatusOK, resp)
return
}
cancelCtx, cancel = context.WithTimeout(c, 5*time.Second)
defer cancel()
unmarshalMap := make(map[string]interface{})
tableName := model.SelectModelNameByType(componentInfo.ComponentType)
result = pgClient.WithContext(cancelCtx).Table(tableName).Where("global_uuid = ?", uuid).Find(&unmarshalMap)
if result.Error != nil {
logger.Error(c, "query model detail info failed", "error", result.Error)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: result.Error.Error(),
}
c.JSON(http.StatusOK, resp)
return
}
if unmarshalMap == nil {
err := fmt.Errorf("query model detail info by uuid failed:%w", constant.ErrQueryRowZero)
logger.Error(c, "query model detail info from table is empty", "table_name", tableName)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
}
c.JSON(http.StatusOK, resp)
return
}
componentType := unmarshalMap["component_type"].(int)
if componentType != constant.DemoType {
logger.Error(c, "can not process real time data of component type not equal DemoType", "component_id", componentInfo.ID)
}
diagram.UpdateAnchorValue(componentInfo.ID, anchorName)
resp := network.SuccessResponse{
Code: http.StatusOK,
Msg: "success",
PayLoad: map[string]interface{}{
"uuid": request.UUID,
},
}
c.JSON(http.StatusOK, resp)
}