router_menu.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package back
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func prepareRouterMenu() {
  7. router.POST("/menu-insert-item/:category", handlerInsertMenuItem)
  8. router.POST("/menu-delete-item", handlerDeleteMenuItem)
  9. router.POST("/menu-update-item", handlerUpdateMenuItem)
  10. router.POST("/menu-update-item-active", handlerUpdateMenuItemActive)
  11. router.POST("/menu-get/:category", handlerMenuGet)
  12. router.POST("/menu-get-source/:category", handlerMenuGetSource)
  13. }
  14. func handlerInsertMenuItem(c *gin.Context) {
  15. role := getRoleFromContext(c)
  16. if role != "__admin" {
  17. c.JSON(200, gin.H{
  18. "Error": "Отказано в доступе",
  19. })
  20. }
  21. m := menu{}
  22. e := c.BindJSON(&m)
  23. if e != nil {
  24. fmt.Println(e)
  25. c.JSON(200, gin.H{
  26. "Error": e.Error(),
  27. })
  28. return
  29. }
  30. m.Categoty = c.Param("category")
  31. e = m.Insert()
  32. if e != nil {
  33. fmt.Println(e)
  34. c.JSON(200, gin.H{
  35. "Error": e.Error(),
  36. })
  37. return
  38. }
  39. prepareMenu()
  40. c.JSON(200, gin.H{
  41. "Error": nil,
  42. })
  43. }
  44. func handlerDeleteMenuItem(c *gin.Context) {
  45. role := getRoleFromContext(c)
  46. if role != "__admin" {
  47. c.JSON(200, gin.H{
  48. "Error": "Отказано в доступе",
  49. })
  50. }
  51. m := menu{}
  52. e := c.BindJSON(&m)
  53. if e != nil {
  54. fmt.Println(e)
  55. c.JSON(200, gin.H{
  56. "Error": e.Error(),
  57. })
  58. return
  59. }
  60. e = m.Delete()
  61. if e != nil {
  62. fmt.Println(e)
  63. c.JSON(200, gin.H{
  64. "Error": e.Error(),
  65. })
  66. return
  67. }
  68. prepareMenu()
  69. c.JSON(200, gin.H{
  70. "Error": nil,
  71. })
  72. }
  73. func handlerUpdateMenuItem(c *gin.Context) {
  74. role := getRoleFromContext(c)
  75. if role != "__admin" {
  76. c.JSON(200, gin.H{
  77. "Error": "Отказано в доступе",
  78. })
  79. }
  80. m := menu{}
  81. e := c.BindJSON(&m)
  82. if e != nil {
  83. fmt.Println(e)
  84. c.JSON(200, gin.H{
  85. "Error": e.Error(),
  86. })
  87. return
  88. }
  89. e = m.Update()
  90. if e != nil {
  91. fmt.Println(e)
  92. c.JSON(200, gin.H{
  93. "Error": e.Error(),
  94. })
  95. return
  96. }
  97. prepareMenu()
  98. c.JSON(200, gin.H{
  99. "Error": nil,
  100. })
  101. }
  102. func handlerUpdateMenuItemActive(c *gin.Context) {
  103. role := getRoleFromContext(c)
  104. if role != "__admin" {
  105. c.JSON(200, gin.H{
  106. "Error": "Отказано в доступе",
  107. })
  108. }
  109. m := menu{}
  110. e := c.BindJSON(&m)
  111. if e != nil {
  112. fmt.Println(e)
  113. c.JSON(200, gin.H{
  114. "Error": e.Error(),
  115. })
  116. return
  117. }
  118. e = m.UpdateActive()
  119. if e != nil {
  120. fmt.Println(e)
  121. c.JSON(200, gin.H{
  122. "Error": e.Error(),
  123. })
  124. return
  125. }
  126. prepareMenu()
  127. c.JSON(200, gin.H{
  128. "Error": nil,
  129. })
  130. }
  131. func handlerMenuGet(c *gin.Context) {
  132. isAdmin := false
  133. role := getRoleFromContext(c)
  134. if role == "__admin" {
  135. isAdmin = true
  136. }
  137. category := c.Param("category")
  138. h := gin.H{}
  139. switch category {
  140. case "__main":
  141. if isAdmin {
  142. h["Menu"] = templateMenuAll
  143. } else {
  144. h["Menu"] = templateMenu
  145. }
  146. case "__top":
  147. if isAdmin {
  148. h["Menu"] = templateMenuAllTop
  149. } else {
  150. h["Menu"] = templateMenuTop
  151. }
  152. }
  153. c.JSON(200, h)
  154. }
  155. func handlerMenuGetSource(c *gin.Context) {
  156. isAdmin := false
  157. role := getRoleFromContext(c)
  158. if role == "__admin" {
  159. isAdmin = true
  160. }
  161. category := c.Param("category")
  162. h := gin.H{}
  163. switch category {
  164. case "__main":
  165. if isAdmin {
  166. h["Menu"] = buildedMenuAll
  167. } else {
  168. h["Menu"] = buildedMenu
  169. }
  170. case "__top":
  171. if isAdmin {
  172. h["Menu"] = buildedMenuAllTop
  173. } else {
  174. h["Menu"] = buildedMenuTop
  175. }
  176. }
  177. c.JSON(200, h)
  178. }