add attr handlers

This commit is contained in:
douxu 2025-08-27 17:33:10 +08:00
parent 349d3398b2
commit 858d02f955
5 changed files with 169 additions and 0 deletions

43
handler/attr_delete.go Normal file
View File

@ -0,0 +1,43 @@
package handler
import (
"net/http"
"modelRT/diagram"
"modelRT/logger"
"modelRT/network"
"github.com/gin-gonic/gin"
)
// AttrDeleteHandler deletes a data attribute
func AttrDeleteHandler(c *gin.Context) {
var request network.AttrDeleteRequest
if err := c.ShouldBindJSON(&request); err != nil {
logger.Error(c, "Failed to unmarshal attribute delete request", "error", err)
c.JSON(http.StatusOK, network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
})
return
}
rs := diagram.NewRedisString(c, request.AttrToken, "", 10, true)
if err := rs.GETDEL(request.AttrToken); err != nil {
logger.Error(c, "Failed to delete attribute from Redis", "attr_token", request.AttrToken, "error", err)
c.JSON(http.StatusOK, network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{"attr_token": request.AttrToken},
})
return
}
c.JSON(http.StatusOK, network.SuccessResponse{
Code: http.StatusOK,
Msg: "success",
PayLoad: map[string]interface{}{
"attr_token": request.AttrToken,
},
})
}

53
handler/attr_load.go Normal file
View File

@ -0,0 +1,53 @@
package handler
import (
"net/http"
"modelRT/database"
"modelRT/logger"
"modelRT/network"
"github.com/gin-gonic/gin"
)
// AttrGetHandler retrieves the value of a data attribute
func AttrGetHandler(c *gin.Context) {
var request network.AttrGetRequest
if err := c.ShouldBindJSON(&request); err != nil {
logger.Error(c, "Failed to unmarshal attribute get request", "error", err)
c.JSON(http.StatusOK, network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
})
return
}
pgClient := database.GetPostgresDBClient()
tx := pgClient.Begin()
attrModel, err := database.ParseAttrToken(c, tx, request.AttrToken)
if err != nil {
tx.Rollback()
logger.Error(c, "Failed to parse attribute token", "attr_token", request.AttrToken, "error", err)
c.JSON(http.StatusOK, network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{"attr_token": request.AttrToken},
})
return
}
tx.Commit()
// The GetAttrValue method is assumed to exist on the AttrModelInterface.
// You need to add this method to your attribute_model.go interface definition.
attrValue := attrModel.GetAttrValue()
c.JSON(http.StatusOK, network.SuccessResponse{
Code: http.StatusOK,
Msg: "success",
PayLoad: map[string]interface{}{
"attr_token": request.AttrToken,
"attr_value": attrValue,
},
})
}

44
handler/attr_update.go Normal file
View File

@ -0,0 +1,44 @@
package handler
import (
"net/http"
"modelRT/diagram"
"modelRT/logger"
"modelRT/network"
"github.com/gin-gonic/gin"
)
// AttrSetHandler sets the value of a data attribute
func AttrSetHandler(c *gin.Context) {
var request network.AttrSetRequest
if err := c.ShouldBindJSON(&request); err != nil {
logger.Error(c, "Failed to unmarshal attribute set request", "error", err)
c.JSON(http.StatusOK, network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
})
return
}
// The logic for handling Redis operations directly from the handler
rs := diagram.NewRedisString(c, request.AttrToken, "", 10, true)
if err := rs.Set(request.AttrToken, request.AttrValue); err != nil {
logger.Error(c, "Failed to set attribute value in Redis", "attr_token", request.AttrToken, "error", err)
c.JSON(http.StatusOK, network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{"attr_token": request.AttrToken},
})
return
}
c.JSON(http.StatusOK, network.SuccessResponse{
Code: http.StatusOK,
Msg: "success",
PayLoad: map[string]interface{}{
"attr_token": request.AttrToken,
},
})
}

View File

@ -11,6 +11,7 @@ type AttrModelInterface interface {
GetZoneInfo() *orm.Zone GetZoneInfo() *orm.Zone
GetStationInfo() *orm.Station GetStationInfo() *orm.Station
GetComponentInfo() *orm.Component GetComponentInfo() *orm.Component
GetAttrValue() interface{} // New method to get the attribute value
IsLocal() bool IsLocal() bool
} }
@ -50,6 +51,11 @@ func (l *LongAttrInfo) IsLocal() bool {
return true return true
} }
// GetAttrValue define return the attribute value
func (l *LongAttrInfo) GetAttrValue() interface{} {
return l.AttrValue
}
// ShortAttrInfo structure define short attribute key info of component // ShortAttrInfo structure define short attribute key info of component
type ShortAttrInfo struct { type ShortAttrInfo struct {
AttrGroupName string AttrGroupName string
@ -82,3 +88,8 @@ func (s *ShortAttrInfo) GetComponentInfo() *orm.Component {
func (s *ShortAttrInfo) IsLocal() bool { func (s *ShortAttrInfo) IsLocal() bool {
return false return false
} }
// GetAttrValue define return the attribute value
func (l *ShortAttrInfo) GetAttrValue() interface{} {
return l.AttrValue
}

18
network/attr_request.go Normal file
View File

@ -0,0 +1,18 @@
// Package network define struct of network operation
package network
// AttrGetRequest defines the request payload for getting an attribute
type AttrGetRequest struct {
AttrToken string `json:"attr_token"`
}
// AttrSetRequest defines the request payload for setting an attribute
type AttrSetRequest struct {
AttrToken string `json:"attr_token"`
AttrValue interface{} `json:"attr_value"`
}
// AttrDeleteRequest defines the request payload for deleting an attribute
type AttrDeleteRequest struct {
AttrToken string `json:"attr_token"`
}