package routes import ( "basic-sensor-receiver/app" "net/http" "github.com/gin-gonic/gin" ) type postSensorsBody struct { Name string `json:"name"` } type putSensorsBody struct { Name string `json:"name"` } func GetSensors(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { sensors, err := s.Services.Sensors.GetList() if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, sensors) } } func PostSensors(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { body := postSensorsBody{} if err := c.BindJSON(&body); err != nil { c.AbortWithError(http.StatusBadRequest, err) return } sensor, err := s.Services.Sensors.Create(body.Name) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, sensor) } } func PutSensors(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { body := putSensorsBody{} sensorId, err := getSensorId(c) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return } if err := c.BindJSON(&body); err != nil { c.AbortWithError(http.StatusBadRequest, err) return } sensor, err := s.Services.Sensors.Update(sensorId, body.Name) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, sensor) } }