package back import ( "fmt" "github.com/gin-gonic/gin" ) func prepareRouterMenu() { router.POST("/menu-insert-item/:category", handlerInsertMenuItem) router.POST("/menu-delete-item", handlerDeleteMenuItem) router.POST("/menu-update-item", handlerUpdateMenuItem) router.POST("/menu-update-item-active", handlerUpdateMenuItemActive) router.POST("/menu-get/:category", handlerMenuGet) router.POST("/menu-get-source/:category", handlerMenuGetSource) } func handlerInsertMenuItem(c *gin.Context) { role := getRoleFromContext(c) if role != "__admin" { c.JSON(200, gin.H{ "Error": "Отказано в доступе", }) } m := menu{} e := c.BindJSON(&m) if e != nil { fmt.Println(e) c.JSON(200, gin.H{ "Error": e.Error(), }) return } m.Categoty = c.Param("category") e = m.Insert() if e != nil { fmt.Println(e) c.JSON(200, gin.H{ "Error": e.Error(), }) return } prepareMenu() c.JSON(200, gin.H{ "Error": nil, }) } func handlerDeleteMenuItem(c *gin.Context) { role := getRoleFromContext(c) if role != "__admin" { c.JSON(200, gin.H{ "Error": "Отказано в доступе", }) } m := menu{} e := c.BindJSON(&m) if e != nil { fmt.Println(e) c.JSON(200, gin.H{ "Error": e.Error(), }) return } e = m.Delete() if e != nil { fmt.Println(e) c.JSON(200, gin.H{ "Error": e.Error(), }) return } prepareMenu() c.JSON(200, gin.H{ "Error": nil, }) } func handlerUpdateMenuItem(c *gin.Context) { role := getRoleFromContext(c) if role != "__admin" { c.JSON(200, gin.H{ "Error": "Отказано в доступе", }) } m := menu{} e := c.BindJSON(&m) if e != nil { fmt.Println(e) c.JSON(200, gin.H{ "Error": e.Error(), }) return } e = m.Update() if e != nil { fmt.Println(e) c.JSON(200, gin.H{ "Error": e.Error(), }) return } prepareMenu() c.JSON(200, gin.H{ "Error": nil, }) } func handlerUpdateMenuItemActive(c *gin.Context) { role := getRoleFromContext(c) if role != "__admin" { c.JSON(200, gin.H{ "Error": "Отказано в доступе", }) } m := menu{} e := c.BindJSON(&m) if e != nil { fmt.Println(e) c.JSON(200, gin.H{ "Error": e.Error(), }) return } e = m.UpdateActive() if e != nil { fmt.Println(e) c.JSON(200, gin.H{ "Error": e.Error(), }) return } prepareMenu() c.JSON(200, gin.H{ "Error": nil, }) } func handlerMenuGet(c *gin.Context) { isAdmin := false role := getRoleFromContext(c) if role == "__admin" { isAdmin = true } category := c.Param("category") h := gin.H{} switch category { case "__main": if isAdmin { h["Menu"] = templateMenuAll } else { h["Menu"] = templateMenu } case "__top": if isAdmin { h["Menu"] = templateMenuAllTop } else { h["Menu"] = templateMenuTop } } c.JSON(200, h) } func handlerMenuGetSource(c *gin.Context) { isAdmin := false role := getRoleFromContext(c) if role == "__admin" { isAdmin = true } category := c.Param("category") h := gin.H{} switch category { case "__main": if isAdmin { h["Menu"] = buildedMenuAll } else { h["Menu"] = buildedMenu } case "__top": if isAdmin { h["Menu"] = buildedMenuAllTop } else { h["Menu"] = buildedMenuTop } } c.JSON(200, h) }