package routes import ( "basic-sensor-receiver/app" "database/sql" "net/http" "github.com/gin-gonic/gin" ) type postDashboardBody struct { Name string `json:"name" binding:"required"` Contents string `json:"contents" binding:"required"` } type putDashboardBody struct { Name string `json:"name" binding:"required"` Contents string `json:"contents" binding:"required"` } 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, err := getIntParamOrAbort(c, "id") if err != nil { return } item, err := s.Services.Dashboards.GetById(id) if err == sql.ErrNoRows { c.Status(404) return } 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 := bindJSONBodyOrAbort(c, &body); err != nil { return } item, err := s.Services.Dashboards.Create(body.Name, 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, err := getIntParamOrAbort(c, "id") if err != nil { return } body := putDashboardBody{} if err := bindJSONBodyOrAbort(c, &body); err != nil { return } item, err := s.Services.Dashboards.Update(id, body.Name, body.Contents) if err != nil { c.AbortWithError(500, err) return } c.JSON(http.StatusOK, item) } } func DeleteDashboard(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { id, err := getIntParamOrAbort(c, "id") if err != nil { return } err = s.Services.Dashboards.Delete(id) if err != nil { c.AbortWithError(500, err) return } c.Status(http.StatusOK) } }