package handler import ( "net/http" "modelRT/constants" "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 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 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, clientToken) 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, }, }) }