[add] initial commit
This commit is contained in:
134
app/controllers/sensor.go
Normal file
134
app/controllers/sensor.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gelvin/app/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (c Controller) GetSensors(ctx *gin.Context) {
|
||||
var sensors []models.Sensor
|
||||
result := c.app.Db().Find(&sensors)
|
||||
if result.Error != nil {
|
||||
ctx.IndentedJSON(http.StatusNotFound, c.app.NewError("sensor not found", result.Error.Error()))
|
||||
} else {
|
||||
ctx.IndentedJSON(http.StatusOK, sensors)
|
||||
}
|
||||
}
|
||||
|
||||
func (c Controller) GetSensor(ctx *gin.Context) {
|
||||
var sensor models.Sensor
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||
return
|
||||
}
|
||||
result := c.app.Db().First(&sensor, id)
|
||||
if result.Error != nil {
|
||||
ctx.IndentedJSON(http.StatusNotFound, c.app.NewError("sensor not found", result.Error.Error()))
|
||||
} else {
|
||||
ctx.IndentedJSON(http.StatusOK, sensor)
|
||||
}
|
||||
}
|
||||
|
||||
func (c Controller) AddSensor(ctx *gin.Context) {
|
||||
var sensor models.Sensor
|
||||
var err = ctx.BindJSON(&sensor)
|
||||
if err == nil && !sensor.IsValid() {
|
||||
err = errors.New("invalid sensor")
|
||||
}
|
||||
var result uint
|
||||
httpCode := http.StatusCreated
|
||||
if err == nil {
|
||||
var txSensor models.Sensor
|
||||
tx := c.app.Db().Where(&models.Sensor{Name: sensor.Name}).First(&txSensor)
|
||||
if tx.Error == nil {
|
||||
result, err = c.model.UpdateSensor(sensor, txSensor, tx)
|
||||
httpCode = http.StatusOK
|
||||
} else {
|
||||
result, err = c.model.AddSensor(sensor)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||
} else {
|
||||
id := struct {
|
||||
Id uint `json:"id"`
|
||||
}{result}
|
||||
ctx.IndentedJSON(httpCode, id)
|
||||
}
|
||||
}
|
||||
|
||||
func (c Controller) GetMeasurements(ctx *gin.Context) {
|
||||
var measurements []models.Measurement
|
||||
//result := c.app.Db().Find(&measurements)
|
||||
// MAX(created_at) converts time.Time to string, `as created_at` or datetime() is not sufficient
|
||||
result := c.app.Db().Select("MAX(created_at)",
|
||||
"id", "created_at", "deleted_at", "sensor_id", "value").Group("sensor_id").Find(&measurements)
|
||||
if result.Error != nil {
|
||||
ctx.IndentedJSON(http.StatusOK, struct{}{})
|
||||
} else {
|
||||
ctx.IndentedJSON(http.StatusOK, measurements)
|
||||
} //result := c.app.Db().Select("id sensor_id MAX(created_at) value").Group("sensor_id").Find(&measurements)
|
||||
|
||||
}
|
||||
|
||||
func (c Controller) GetMeasurement(ctx *gin.Context) {
|
||||
var measurements []models.Measurement
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||
return
|
||||
}
|
||||
result := c.app.Db().Where(&models.Measurement{SensorID: uint(id)}).Find(&measurements)
|
||||
if result.Error != nil {
|
||||
ctx.IndentedJSON(http.StatusOK, struct{}{})
|
||||
} else {
|
||||
ctx.IndentedJSON(http.StatusOK, measurements)
|
||||
}
|
||||
}
|
||||
|
||||
func (c Controller) AddMeasurement(ctx *gin.Context) {
|
||||
var measurement models.Measurement
|
||||
err := ctx.BindJSON(&measurement)
|
||||
if err != nil {
|
||||
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||
return
|
||||
}
|
||||
c.addMeasurement(ctx, measurement)
|
||||
}
|
||||
|
||||
func (c Controller) AddMeasurementID(ctx *gin.Context) {
|
||||
var measurement models.Measurement
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||
return
|
||||
}
|
||||
err = ctx.BindJSON(&measurement)
|
||||
if err != nil {
|
||||
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||
return
|
||||
}
|
||||
measurement.SensorID = uint(id)
|
||||
c.addMeasurement(ctx, measurement)
|
||||
}
|
||||
|
||||
func (c Controller) addMeasurement(ctx *gin.Context, measurement models.Measurement) {
|
||||
result, err := c.model.AddMeasurement(measurement)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
ctx.IndentedJSON(http.StatusNotFound, c.app.NewError("sensor not found", err.Error()))
|
||||
} else {
|
||||
ctx.IndentedJSON(http.StatusBadRequest, c.app.NewError("invalid input", err.Error()))
|
||||
}
|
||||
} else {
|
||||
id := struct {
|
||||
Id uint `json:"id"`
|
||||
}{result}
|
||||
ctx.IndentedJSON(http.StatusOK, id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user