package routes import ( "basic-sensor-receiver/app" "net/http" "github.com/gin-gonic/gin" ) type postAndPutAlertsBody struct { ContactPointId int64 `json:"contactPointId" binding:"required"` Name string `json:"name" binding:"required"` Condition string `json:"condition" binding:"required"` TriggerInterval int64 `json:"triggerInterval" binding:"required"` CustomMessage string `json:"customMessage"` CustomResolvedMessage string `json:"customResolvedMessage"` } func GetAlerts(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { alerts, err := s.Services.Alerts.GetList() if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, alerts) } } func PostAlerts(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { body := postAndPutAlertsBody{} if err := bindJSONBodyOrAbort(c, &body); err != nil { return } alert, err := s.Services.Alerts.Create(body.ContactPointId, body.Name, body.Condition, body.TriggerInterval, body.CustomMessage, body.CustomResolvedMessage) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, alert) } } func PutAlert(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { alertId, err := getIntParamOrAbort(c, "alertId") if err != nil { return } body := postAndPutAlertsBody{} if err := bindJSONBodyOrAbort(c, &body); err != nil { return } alert, err := s.Services.Alerts.Update(alertId, body.ContactPointId, body.Name, body.Condition, body.TriggerInterval, body.CustomMessage, body.CustomResolvedMessage) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, alert) } } func GetAlert(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { alertId, err := getIntParamOrAbort(c, "alertId") if err != nil { return } alert, err := s.Services.Alerts.GetById(alertId) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, alert) } } func DeleteAlert(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { alertId, err := getIntParamOrAbort(c, "alertId") if err != nil { return } if err := s.Services.Alerts.DeleteById(alertId); err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.Status(http.StatusOK) } }