123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- 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)
- }
|