59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"modelRT/constants"
|
|
"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
|
|
|
|
clientToken := c.GetString("client_token")
|
|
if clientToken == "" {
|
|
err := constants.ErrGetClientToken
|
|
|
|
logger.Error(c, "failed to get client token from context", "error", err)
|
|
c.JSON(http.StatusOK, network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
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, clientToken, 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,
|
|
},
|
|
})
|
|
}
|