package routes import ( "basic-sensor-receiver/app" "net/http" "strconv" "github.com/gin-gonic/gin" ) type postDashboardBody struct { Id int64 `json:"id"` Name string `json:"name"` Contents string `json:"contents"` } type putDashboardBody struct { Name string `json:"name"` 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, err := getIntParam(c, "id") if err != nil { c.AbortWithError(400, err) return } 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.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 := getIntParam(c, "id") if err != nil { c.AbortWithError(400, err) return } body := putDashboardBody{} if err := c.BindJSON(&body); err != nil { c.AbortWithError(400, err) 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 getIntParam(c *gin.Context, key string) (int64, error) { value := c.Param(key) return strconv.ParseInt(value, 10, 64) }