Proper implementation of contact points REST
This commit is contained in:
parent
c9967b2e3d
commit
6c96ef1b23
|
|
@ -29,7 +29,7 @@ func main() {
|
|||
router.Static("/assets", "client/assets")
|
||||
|
||||
// Only allow CORS in development mode
|
||||
if server.Config.Mode == "development" {
|
||||
if server.Config.Mode == "debug" {
|
||||
router.Use(middleware.CorsMiddleware())
|
||||
}
|
||||
|
||||
|
|
@ -57,6 +57,11 @@ func main() {
|
|||
loginProtected.GET("/api/alerts/:alertId", routes.GetAlert(server))
|
||||
loginProtected.PUT("/api/alerts/:alertId", routes.PutAlert(server))
|
||||
loginProtected.DELETE("/api/alerts/:alertId", routes.DeleteAlert(server))
|
||||
loginProtected.GET("/api/contact-points", routes.GetContactPoints(server))
|
||||
loginProtected.POST("/api/contact-points", routes.PostContactPoints(server))
|
||||
loginProtected.GET("/api/contact-points/:contactPointId", routes.GetContactPoint(server))
|
||||
loginProtected.PUT("/api/contact-points/:contactPointId", routes.PutContactPoint(server))
|
||||
loginProtected.DELETE("/api/contact-points/:contactPointId", routes.DeleteContactPoint(server))
|
||||
loginProtected.POST("/api/logout", routes.Logout(server))
|
||||
|
||||
// Routes accessible using auth key
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package middleware
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func CorsMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
|
@ -8,5 +10,9 @@ func CorsMiddleware() gin.HandlerFunc {
|
|||
c.Header("Access-Control-Allow-Credentials", "true")
|
||||
// 2 hours
|
||||
c.Header("Access-Control-Max-Age", "7200")
|
||||
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(200)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1,127 @@
|
|||
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"`
|
||||
}
|
||||
|
||||
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.JSON(http.StatusOK, gin.H{})
|
||||
}
|
||||
}
|
||||
|
||||
func getContactPointId(c *gin.Context) (int64, error) {
|
||||
sensor := c.Param("contactPointId")
|
||||
|
||||
return strconv.ParseInt(sensor, 10, 64)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue