2024-03-29 09:55:51 +01:00
|
|
|
package routes
|
2024-03-29 21:05:21 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"basic-sensor-receiver/app"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2024-04-01 12:41:04 +02:00
|
|
|
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-29 21:05:21 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-31 09:50:09 +02:00
|
|
|
type testContactPointBody struct {
|
2024-04-01 12:41:04 +02:00
|
|
|
Type string `json:"type" binding:"required,oneof=telegram"`
|
|
|
|
|
TypeConfig string `json:"typeConfig" binding:"required"`
|
2024-03-31 09:50:09 +02:00
|
|
|
}
|
|
|
|
|
|
2024-03-29 21:05:21 +01: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) {
|
2024-04-01 12:41:04 +02:00
|
|
|
body := postOrPutContactPointsBody{}
|
2024-03-29 21:05:21 +01:00
|
|
|
|
2024-04-01 17:03:45 +02:00
|
|
|
if err := bindJSONBodyOrAbort(c, &body); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-29 21:05:21 +01:00
|
|
|
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) {
|
2024-04-01 17:03:45 +02:00
|
|
|
contactPointId, err := getIntParamOrAbort(c, "contactPointId")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-29 21:05:21 +01:00
|
|
|
|
2024-04-01 12:41:04 +02:00
|
|
|
body := postOrPutContactPointsBody{}
|
2024-04-01 17:03:45 +02:00
|
|
|
if err := bindJSONBodyOrAbort(c, &body); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-29 21:05:21 +01:00
|
|
|
|
|
|
|
|
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) {
|
2024-04-01 17:03:45 +02:00
|
|
|
contactPointId, err := getIntParamOrAbort(c, "contactPointId")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-29 21:05:21 +01:00
|
|
|
|
|
|
|
|
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) {
|
2024-04-01 17:03:45 +02:00
|
|
|
contactPointId, err := getIntParamOrAbort(c, "contactPointId")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-29 21:05:21 +01:00
|
|
|
|
2024-04-01 17:03:45 +02:00
|
|
|
err = s.Services.ContactPoints.Delete(contactPointId)
|
2024-03-29 21:05:21 +01:00
|
|
|
|
|
|
|
|
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{}
|
2024-04-01 17:03:45 +02:00
|
|
|
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)
|
2024-03-29 21:05:21 +01:00
|
|
|
}
|
|
|
|
|
}
|