77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
// Package handler provides HTTP handlers for various endpoints.
|
|
package handler
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"modelRT/common/errcode"
|
|
"modelRT/database"
|
|
"modelRT/diagram"
|
|
"modelRT/logger"
|
|
"modelRT/network"
|
|
"modelRT/orm"
|
|
|
|
"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
|
|
|
|
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
|
|
}
|
|
|
|
if result.RowsAffected == 0 {
|
|
err := fmt.Errorf("query component detail info by uuid failed:%w", errcode.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
|
|
}
|
|
diagram.UpdateAnchorValue(componentInfo.GlobalUUID.String(), anchorName)
|
|
|
|
resp := network.SuccessResponse{
|
|
Code: http.StatusOK,
|
|
Msg: "success",
|
|
PayLoad: map[string]interface{}{
|
|
"uuid": request.UUID,
|
|
},
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
}
|