modelRT/handler/component_attribute_update.go

83 lines
2.1 KiB
Go
Raw Normal View History

// Package handler provides HTTP handlers for various endpoints.
package handler
import (
"fmt"
"net/http"
"modelRT/database"
"modelRT/diagram"
"modelRT/logger"
"modelRT/network"
"github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
)
// ComponentAttributeUpdateHandler define circuit diagram component attribute value update process API
func ComponentAttributeUpdateHandler(c *gin.Context) {
pgClient := database.GetPostgresDBClient()
var request network.ComponentAttributeUpdateInfo
if err := c.ShouldBindJSON(&request); err != nil {
logger.Error(c, "unmarshal circuit diagram component attribute update info failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
}
c.JSON(http.StatusOK, resp)
return
}
componentUUID := uuid.FromStringOrNil(request.UUIDStr)
if componentUUID == uuid.Nil {
err := fmt.Errorf("convert component uuid failed")
logger.Error(c, "convert component uuid type from string to uuid failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
}
c.JSON(http.StatusOK, resp)
return
}
// open transaction
tx := pgClient.Begin()
componentInfo, err := database.QueryComponentByUUID(c, tx, componentUUID)
if err != nil {
logger.Error(c, "query component info by uuid from postgres failed failed", "error", err)
tx.Rollback()
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
}
c.JSON(http.StatusOK, resp)
return
}
// TODO 从 project_manager 表进行查询,然后更新对应表的数据的值
// TODO 更新 redis 中的缓存的数值
componentAttributeKey := fmt.Sprintf("%s_%s", componentInfo.Tag, request.AttributeConfigs[0].AttributeExtendType)
// attributeSet.CompTag, colParams.AttributeType
attributeHSet := diagram.NewRedisHash(c, componentAttributeKey, 5000, false)
attributeHSet.SetRedisHashByKV("", nil)
// commit transaction
tx.Commit()
resp := network.SuccessResponse{
Code: http.StatusOK,
Msg: "success",
Payload: map[string]interface{}{
"uuid": request.UUIDStr,
},
}
c.JSON(http.StatusOK, resp)
}