graphicek/server/routes/dashboards.go

87 lines
1.5 KiB
Go

package routes
import (
"basic-sensor-receiver/app"
"net/http"
"github.com/gin-gonic/gin"
)
type postDashboardBody struct {
Id string `json:"id"`
Contents string `json:"contents"`
}
type putDashboardBody struct {
Contents string `json:"contents"`
}
func GetDashboards(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) {
items, err := s.Services.Dashboards.GetList()
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(http.StatusOK, items)
}
}
func GetDashboardById(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
item, err := s.Services.Dashboards.GetById(id)
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(http.StatusOK, item)
}
}
func PostDashboard(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) {
body := postDashboardBody{}
if err := c.BindJSON(&body); err != nil {
c.AbortWithError(400, err)
return
}
item, err := s.Services.Dashboards.Create(body.Id, body.Contents)
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(http.StatusOK, item)
}
}
func PutDashboard(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
body := putDashboardBody{}
if err := c.BindJSON(&body); err != nil {
c.AbortWithError(400, err)
return
}
item, err := s.Services.Dashboards.Update(id, body.Contents)
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(http.StatusOK, item)
}
}