modelRT/handler/measurement_load.go

56 lines
1.4 KiB
Go
Raw Normal View History

2025-09-05 17:10:34 +08:00
package handler
import (
"net/http"
"modelRT/database"
"modelRT/logger"
"modelRT/network"
"github.com/gin-gonic/gin"
)
// TODO 优化redis 读取步骤
// MeasurementGetHandler retrieves the value of measurement data
func MeasurementGetHandler(c *gin.Context) {
var request network.MeasurementGetRequest
if err := c.ShouldBindJSON(&request); err != nil {
logger.Error(c, "Failed to unmarshal measurement 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.MeasurementToken)
if err != nil {
tx.Rollback()
logger.Error(c, "Failed to parse attribute token", "attr_token", request.MeasurementToken, "error", err)
c.JSON(http.StatusOK, network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{"attr_token": request.MeasurementToken},
})
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.MeasurementToken,
"attr_value": attrValue,
},
})
}