102 lines
1.8 KiB
Go
102 lines
1.8 KiB
Go
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 := getIntParamOrAbort(c, "id")
|
|
|
|
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{}
|
|
bindJSONBodyOrAbort(c, &body)
|
|
|
|
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 := getIntParamOrAbort(c, "id")
|
|
|
|
body := putDashboardBody{}
|
|
bindJSONBodyOrAbort(c, &body)
|
|
|
|
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 := getIntParamOrAbort(c, "id")
|
|
|
|
err := s.Services.Dashboards.Delete(id)
|
|
|
|
if err != nil {
|
|
c.AbortWithError(500, err)
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
}
|
|
}
|