Handle tracks
This commit is contained in:
parent
5799eb32ef
commit
5c7841fdc6
7 changed files with 343 additions and 52 deletions
|
|
@ -1,37 +1,94 @@
|
|||
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 declareTracksRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
||||
router.GET("/tracks", func(c *gin.Context) {
|
||||
tracks, err := reveil.LoadTracks(cfg)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, tracks)
|
||||
})
|
||||
router.POST("/tracks", func(c *gin.Context) {
|
||||
|
||||
c.AbortWithStatusJSON(http.StatusNotImplemented, gin.H{"errmsg": "TODO"})
|
||||
})
|
||||
|
||||
tracksRoutes := router.Group("/tracks/:tid")
|
||||
tracksRoutes.Use(trackHandler)
|
||||
tracksRoutes.Use(func(c *gin.Context) {
|
||||
tracks, err := reveil.LoadTracks(cfg)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
for _, t := range tracks {
|
||||
if base64.StdEncoding.EncodeToString(t.Id) == c.Param("tid") {
|
||||
c.Set("track", t)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Track not found"})
|
||||
})
|
||||
|
||||
tracksRoutes.GET("", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, c.MustGet("track"))
|
||||
})
|
||||
tracksRoutes.PUT("", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, c.MustGet("track"))
|
||||
oldtrack := c.MustGet("track").(*reveil.Track)
|
||||
|
||||
var track reveil.Track
|
||||
if err := c.ShouldBindJSON(&track); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if track.Name != oldtrack.Name {
|
||||
err := oldtrack.Rename(track.Name)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to rename the track: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if track.Enabled != oldtrack.Enabled {
|
||||
var err error
|
||||
if track.Enabled {
|
||||
err = oldtrack.Enable(cfg)
|
||||
} else {
|
||||
err = oldtrack.Disable()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to enable/disable the track: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, oldtrack)
|
||||
})
|
||||
tracksRoutes.DELETE("", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, c.MustGet("track"))
|
||||
track := c.MustGet("track").(*reveil.Track)
|
||||
|
||||
err := track.Remove()
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to remove the track: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func trackHandler(c *gin.Context) {
|
||||
c.Set("track", nil)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue