aboutsummaryrefslogtreecommitdiff
path: root/routes.go
diff options
context:
space:
mode:
authorEmiliano Ciavatta2020-04-13 15:12:35 +0000
committerEmiliano Ciavatta2020-04-13 15:12:35 +0000
commita56a4e391d541ae05de0203f3d493edc3b04681d (patch)
treeab9344a650305aafb5afe552dc8cad63684de643 /routes.go
parent7113463dead05631339fdab94de9440201c42489 (diff)
Add AddRoute tests
Diffstat (limited to 'routes.go')
-rw-r--r--routes.go118
1 files changed, 82 insertions, 36 deletions
diff --git a/routes.go b/routes.go
index 37088e2..111dd78 100644
--- a/routes.go
+++ b/routes.go
@@ -1,55 +1,101 @@
package main
import (
- "fmt"
"github.com/gin-gonic/gin"
- log "github.com/sirupsen/logrus"
- "github.com/go-playground/validator/v10"
"net/http"
)
-func ApplicationRoutes(engine *gin.Engine) {
- engine.Static("/", "./frontend/build")
+func ApplicationRoutes(engine *gin.Engine, rulesManager RulesManager) {
+ // engine.Static("/", "./frontend/build")
api := engine.Group("/api")
{
- api.POST("/rule", func(c *gin.Context) {
+ api.GET("/rules", func(c *gin.Context) {
+ success(c, rulesManager.GetRules())
+ })
+
+ api.POST("/rules", func(c *gin.Context) {
var rule Rule
- //data, _ := c.GetRawData()
- //
- //var json = jsoniter.ConfigCompatibleWithStandardLibrary
- //err := json.Unmarshal(data, &filter)
- //
- //if err != nil {
- // log.WithError(err).Error("failed to unmarshal")
- // c.String(500, "failed to unmarshal")
- //}
- //
- //err = validator.New().Struct(filter)
- //log.WithError(err).WithField("filter", filter).Error("aaaa")
- //c.String(200, "ok")
+ if err := c.ShouldBindJSON(&rule); err != nil {
+ badRequest(c, err)
+ return
+ }
+
+ if id, err := rulesManager.AddRule(c, rule); err != nil {
+ unprocessableEntity(c, err)
+ } else {
+ success(c, UnorderedDocument{"id": id})
+ }
+ })
+
+ api.GET("/rules/:id", func(c *gin.Context) {
+ hex := c.Param("id")
+ id, err := RowIDFromHex(hex)
+ if err != nil {
+ badRequest(c, err)
+ return
+ }
+ rule, found := rulesManager.GetRule(id)
+ if !found {
+ notFound(c, UnorderedDocument{"id": id})
+ } else {
+ success(c, rule)
+ }
+ })
+ api.PUT("/rules/:id", func(c *gin.Context) {
+ hex := c.Param("id")
+ id, err := RowIDFromHex(hex)
+ if err != nil {
+ badRequest(c, err)
+ return
+ }
+ var rule Rule
if err := c.ShouldBindJSON(&rule); err != nil {
- validationErrors, ok := err.(validator.ValidationErrors)
- if !ok {
- log.WithError(err).WithField("rule", rule).Error("oops")
- c.JSON(http.StatusBadRequest, gin.H{})
- return
- }
-
- for _, fieldErr := range validationErrors {
- log.Println(fieldErr)
- c.JSON(http.StatusBadRequest, gin.H{
- "error": fmt.Sprintf("field '%v' does not respect the %v(%v) rule",
- fieldErr.Field(), fieldErr.Tag(), fieldErr.Param()),
- })
- log.WithError(err).WithField("rule", rule).Error("oops")
- return // exit on first error
- }
+ badRequest(c, err)
+ return
}
- c.JSON(200, rule)
+ updated := rulesManager.UpdateRule(c, id, rule)
+ if !updated {
+ notFound(c, UnorderedDocument{"id": id})
+ } else {
+ success(c, rule)
+ }
})
}
}
+
+func success(c *gin.Context, obj interface{}) {
+ c.JSON(http.StatusOK, obj)
+}
+
+func badRequest(c *gin.Context, err error) {
+ c.JSON(http.StatusBadRequest, UnorderedDocument{"result": "error", "error": err.Error()})
+
+ //validationErrors, ok := err.(validator.ValidationErrors)
+ //if !ok {
+ // log.WithError(err).WithField("rule", rule).Error("oops")
+ // c.JSON(http.StatusBadRequest, gin.H{})
+ // return
+ //}
+ //
+ //for _, fieldErr := range validationErrors {
+ // log.Println(fieldErr)
+ // c.JSON(http.StatusBadRequest, gin.H{
+ // "error": fmt.Sprintf("field '%v' does not respect the %v(%v) rule",
+ // fieldErr.Field(), fieldErr.Tag(), fieldErr.Param()),
+ // })
+ // log.WithError(err).WithField("rule", rule).Error("oops")
+ // return // exit on first error
+ //}
+}
+
+func unprocessableEntity(c *gin.Context, err error) {
+ c.JSON(http.StatusOK, UnorderedDocument{"result": "error", "error": err.Error()})
+}
+
+func notFound(c *gin.Context, obj interface{}) {
+ c.JSON(http.StatusNotFound, obj)
+}