132 lines
3.0 KiB
Go
132 lines
3.0 KiB
Go
package routes
|
|
|
|
import (
|
|
"basic-sensor-receiver/app"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type postAlertsBody struct {
|
|
ContactPointId int64 `json:"contactPointId"`
|
|
Name string `json:"name"`
|
|
Condition string `json:"condition"`
|
|
TriggerInterval int64 `json:"triggerInterval"`
|
|
CustomMessage string `json:"customMessage"`
|
|
CustomResolvedMessage string `json:"customResolvedMessage"`
|
|
}
|
|
|
|
type putAlertsBody struct {
|
|
ContactPointId int64 `json:"contactPointId"`
|
|
Name string `json:"name"`
|
|
Condition string `json:"condition"`
|
|
TriggerInterval int64 `json:"triggerInterval"`
|
|
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 := postAlertsBody{}
|
|
|
|
if err := c.BindJSON(&body); err != nil {
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
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) {
|
|
body := putAlertsBody{}
|
|
|
|
alertId, err := getAlertId(c)
|
|
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
if err := c.BindJSON(&body); err != nil {
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
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 := getAlertId(c)
|
|
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
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 := getAlertId(c)
|
|
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
if err := s.Services.Alerts.DeleteById(alertId); err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
}
|
|
}
|
|
|
|
func getAlertId(c *gin.Context) (int64, error) {
|
|
id := c.Param("alertId")
|
|
|
|
return strconv.ParseInt(id, 10, 64)
|
|
}
|