package routes import ( "basic-sensor-receiver/app" "net/http" "github.com/gin-gonic/gin" ) type postOrPutContactPointsBody struct { Name string `json:"name" binding:"required"` Type string `json:"type" binding:"required,oneof=telegram"` TypeConfig string `json:"typeConfig" binding:"required"` } type testContactPointBody struct { Type string `json:"type" binding:"required,oneof=telegram"` TypeConfig string `json:"typeConfig" binding:"required"` } func GetContactPoints(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { contactPoints, err := s.Services.ContactPoints.GetList() if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, contactPoints) } } func PostContactPoints(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { body := postOrPutContactPointsBody{} if err := bindJSONBodyOrAbort(c, &body); err != nil { return } contactPoint, err := s.Services.ContactPoints.Create(body.Name, body.Type, body.TypeConfig) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, contactPoint) } } func PutContactPoint(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { contactPointId, err := getIntParamOrAbort(c, "contactPointId") if err != nil { return } body := postOrPutContactPointsBody{} if err := bindJSONBodyOrAbort(c, &body); err != nil { return } contactPoint, err := s.Services.ContactPoints.Update(contactPointId, body.Name, body.Type, body.TypeConfig) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, contactPoint) } } func GetContactPoint(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { contactPointId, err := getIntParamOrAbort(c, "contactPointId") if err != nil { return } contactPoint, err := s.Services.ContactPoints.GetById(contactPointId) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.JSON(http.StatusOK, contactPoint) } } func DeleteContactPoint(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { contactPointId, err := getIntParamOrAbort(c, "contactPointId") if err != nil { return } err = s.Services.ContactPoints.Delete(contactPointId) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.AbortWithStatus(http.StatusOK) } } func TestContactPoint(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { body := testContactPointBody{} if err := bindJSONBodyOrAbort(c, &body); err != nil { return } err := s.Services.ContactPoints.Test(body.Type, body.TypeConfig) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } c.AbortWithStatus(http.StatusOK) } }