graphicek/server/routes/contact_points.go

153 lines
3.2 KiB
Go

package routes
import (
"basic-sensor-receiver/app"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
type postContactPointsBody struct {
Name string `json:"name"`
Type string `json:"type"`
TypeConfig string `json:"typeConfig"`
}
type putContactPointsBody struct {
Name string `json:"name"`
Type string `json:"type"`
TypeConfig string `json:"typeConfig"`
}
type testContactPointBody struct {
Type string `json:"type"`
TypeConfig string `json:"typeConfig"`
}
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 := postContactPointsBody{}
if err := c.BindJSON(&body); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
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) {
body := putContactPointsBody{}
contactPointId, err := getContactPointId(c)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if err := c.BindJSON(&body); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
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 := getContactPointId(c)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
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 := getContactPointId(c)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
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 := c.BindJSON(&body); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
err := s.Services.ContactPoints.Test(body.Type, body.TypeConfig)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.AbortWithStatus(http.StatusOK)
}
}
func getContactPointId(c *gin.Context) (int64, error) {
sensor := c.Param("contactPointId")
return strconv.ParseInt(sensor, 10, 64)
}