liuxiran commented on a change in pull request #432: URL: https://github.com/apache/apisix-dashboard/pull/432#discussion_r481906507
########## File path: api/route/route_group.go ########## @@ -0,0 +1,195 @@ +package route + +import ( + "encoding/json" + "fmt" + "net/http" + "strconv" + + "github.com/apisix/manager-api/conf" + "github.com/apisix/manager-api/errno" + "github.com/apisix/manager-api/service" + "github.com/gin-gonic/gin" + uuid "github.com/satori/go.uuid" +) + +func AppendRouteGroup(r *gin.Engine) *gin.Engine { + r.POST("/apisix/admin/routegroups", createRouteGroup) + r.GET("/apisix/admin/routegroups/:gid", findRouteGroup) + r.GET("/apisix/admin/routegroups", listRouteGroup) + r.GET("/apisix/admin/names/routegroups", listRouteGroupName) + r.PUT("/apisix/admin/routegroups/:gid", updateRouteGroup) + r.DELETE("/apisix/admin/routegroups/:gid", deleteRouteGroup) + return r +} + +func createRouteGroup(c *gin.Context) { + u4 := uuid.NewV4() + gid := u4.String() + // todo 参数校验 + param, exist := c.Get("requestBody") + if !exist || len(param.([]byte)) < 1 { + e := errno.FromMessage(errno.RouteRequestError, "route_group create with no post data") + logger.Error(e.Msg) + c.AbortWithStatusJSON(http.StatusBadRequest, e.Response()) + return + } + // trans params + rr := &service.RouteGroupRequest{} + if err := rr.Parse(param); err != nil { + e := errno.FromMessage(errno.RouteGroupRequestError, err.Error()) + logger.Error(e.Msg) + c.AbortWithStatusJSON(http.StatusBadRequest, e.Response()) + return + } + rr.Id = gid + fmt.Println(rr) + // mysql + if rd, err := service.Trans2RouteGroupDao(rr); err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, err.Response()) + return + } else { + if err := rd.CreateRouteGroup(); err != nil { + e := errno.FromMessage(errno.DBRouteGroupError, err.Error()) + logger.Error(e.Msg) + c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response()) + return + } + } + c.Data(http.StatusOK, service.ContentType, errno.Success()) +} + +func findRouteGroup(c *gin.Context) { + gid := c.Param("gid") + routeGroup := &service.RouteGroupDao{} + if err, count := routeGroup.FindRouteGroup(gid); err != nil { + e := errno.FromMessage(errno.RouteGroupRequestError, err.Error()+"route_group ID:"+gid) + logger.Error(e.Msg) + c.AbortWithStatusJSON(http.StatusBadRequest, e.Response()) + return + } else { + if count < 1 { + e := errno.FromMessage(errno.RouteRequestError, " route_group ID: "+gid+" not exist") + logger.Error(e.Msg) + c.AbortWithStatusJSON(e.Status, e.Response()) + return + } + } + resp, _ := json.Marshal(routeGroup) + c.Data(http.StatusOK, service.ContentType, resp) +} + +func listRouteGroup(c *gin.Context) { + size, _ := strconv.Atoi(c.Query("size")) + page, _ := strconv.Atoi(c.Query("page")) + if size == 0 { + size = 10 + } + var s string + + rd := &service.RouteGroupDao{} + routeGroupList := []service.RouteGroupDao{} + if search, exist := c.GetQuery("search"); exist && len(search) > 0 { + s = "%" + search + "%" + } + if err, count := rd.GetRouteGroupList(&routeGroupList, s, page, size); err != nil { + e := errno.FromMessage(errno.RouteGroupRequestError, err.Error()) + logger.Error(e.Msg) + c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response()) + return + } else { + result := &service.ListResponse{Count: count, Data: routeGroupList} + resp, _ := json.Marshal(result) + c.Data(http.StatusOK, service.ContentType, resp) + } +} +func listRouteGroupName(c *gin.Context) { + db := conf.DB() Review comment: this is used in select routegroup name in selectbox, and I found that the same method in upstream has not made paging too. so would it need to modified at the same time? @nic-chen ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org