graphicek/server/routes/contact_points.go

128 lines
2.8 KiB
Go
Raw Normal View History

2024-03-29 09:55:51 +01:00
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"`
}
2024-03-31 09:50:09 +02:00
type testContactPointBody struct {
Type string `json:"type" binding:"required,oneof=telegram"`
TypeConfig string `json:"typeConfig" binding:"required"`
2024-03-31 09:50:09 +02:00
}
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
}
2024-03-31 09:50:09 +02:00
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
}
2024-03-31 09:50:09 +02:00
err := s.Services.ContactPoints.Test(body.Type, body.TypeConfig)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.AbortWithStatus(http.StatusOK)
}
}