Handle gongs
This commit is contained in:
parent
3addf9d1ee
commit
06494d1c91
7 changed files with 338 additions and 50 deletions
77
api/gongs.go
77
api/gongs.go
|
|
@ -1,37 +1,92 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.nemunai.re/nemunaire/reveil/config"
|
||||
"git.nemunai.re/nemunaire/reveil/model"
|
||||
)
|
||||
|
||||
func declareGongsRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
||||
router.GET("/gongs", func(c *gin.Context) {
|
||||
gongs, err := reveil.LoadGongs(cfg)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gongs)
|
||||
})
|
||||
router.POST("/gongs", func(c *gin.Context) {
|
||||
|
||||
c.AbortWithStatusJSON(http.StatusNotImplemented, gin.H{"errmsg": "TODO"})
|
||||
})
|
||||
|
||||
gongsRoutes := router.Group("/gongs/:gid")
|
||||
gongsRoutes.Use(gongHandler)
|
||||
gongsRoutes := router.Group("/gongs/:tid")
|
||||
gongsRoutes.Use(func(c *gin.Context) {
|
||||
gongs, err := reveil.LoadGongs(cfg)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
for _, t := range gongs {
|
||||
if base64.StdEncoding.EncodeToString(t.Id) == c.Param("tid") {
|
||||
c.Set("gong", t)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Gong not found"})
|
||||
})
|
||||
|
||||
gongsRoutes.GET("", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, c.MustGet("gong"))
|
||||
})
|
||||
gongsRoutes.PUT("", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, c.MustGet("gong"))
|
||||
oldgong := c.MustGet("gong").(*reveil.Gong)
|
||||
|
||||
var gong reveil.Gong
|
||||
if err := c.ShouldBindJSON(&gong); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if gong.Name != oldgong.Name {
|
||||
err := oldgong.Rename(gong.Name)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to rename the gong: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if gong.Enabled != oldgong.Enabled {
|
||||
var err error
|
||||
if gong.Enabled {
|
||||
err = oldgong.SetDefault(cfg)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to set the new default gong: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, oldgong)
|
||||
})
|
||||
gongsRoutes.DELETE("", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, c.MustGet("gong"))
|
||||
gong := c.MustGet("gong").(*reveil.Gong)
|
||||
|
||||
err := gong.Remove()
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to remove the gong: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func gongHandler(c *gin.Context) {
|
||||
c.Set("gong", nil)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue