From a56a4e391d541ae05de0203f3d493edc3b04681d Mon Sep 17 00:00:00 2001 From: Emiliano Ciavatta Date: Mon, 13 Apr 2020 17:12:35 +0200 Subject: Add AddRoute tests --- routes.go | 118 +++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 36 deletions(-) (limited to 'routes.go') 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) +} -- cgit v1.2.3-70-g09d2