2025-08-27 17:33:10 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2025-09-16 15:50:22 +08:00
|
|
|
"modelRT/constants"
|
2025-08-27 17:33:10 +08:00
|
|
|
"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
|
2025-09-16 15:50:22 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-27 17:33:10 +08:00
|
|
|
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()
|
|
|
|
|
|
2025-09-16 15:50:22 +08:00
|
|
|
attrModel, err := database.ParseAttrToken(c, tx, request.AttrToken, clientToken)
|
2025-08-27 17:33:10 +08:00
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|