channel.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package controller
  2. import (
  3. "message-pusher/channel"
  4. "message-pusher/common"
  5. "message-pusher/model"
  6. "net/http"
  7. "strconv"
  8. "github.com/gin-gonic/gin"
  9. )
  10. func GetAllChannels(c *gin.Context) {
  11. if c.Query("brief") != "" {
  12. GetBriefChannels(c)
  13. return
  14. }
  15. userId := c.GetInt("id")
  16. p, _ := strconv.Atoi(c.Query("p"))
  17. if p < 0 {
  18. p = 0
  19. }
  20. channels, err := model.GetChannelsByUserId(userId, p*common.ItemsPerPage, common.ItemsPerPage)
  21. if err != nil {
  22. c.JSON(http.StatusOK, gin.H{
  23. "success": false,
  24. "message": err.Error(),
  25. })
  26. return
  27. }
  28. c.JSON(http.StatusOK, gin.H{
  29. "success": true,
  30. "message": "",
  31. "data": channels,
  32. })
  33. return
  34. }
  35. func GetBriefChannels(c *gin.Context) {
  36. userId := c.GetInt("id")
  37. channels, err := model.GetBriefChannelsByUserId(userId)
  38. if err != nil {
  39. c.JSON(http.StatusOK, gin.H{
  40. "success": false,
  41. "message": err.Error(),
  42. })
  43. return
  44. }
  45. c.JSON(http.StatusOK, gin.H{
  46. "success": true,
  47. "message": "",
  48. "data": channels,
  49. })
  50. return
  51. }
  52. func SearchChannels(c *gin.Context) {
  53. userId := c.GetInt("id")
  54. keyword := c.Query("keyword")
  55. channels, err := model.SearchChannels(userId, keyword)
  56. if err != nil {
  57. c.JSON(http.StatusOK, gin.H{
  58. "success": false,
  59. "message": err.Error(),
  60. })
  61. return
  62. }
  63. c.JSON(http.StatusOK, gin.H{
  64. "success": true,
  65. "message": "",
  66. "data": channels,
  67. })
  68. return
  69. }
  70. func GetChannel(c *gin.Context) {
  71. id, err := strconv.Atoi(c.Param("id"))
  72. userId := c.GetInt("id")
  73. if err != nil {
  74. c.JSON(http.StatusOK, gin.H{
  75. "success": false,
  76. "message": err.Error(),
  77. })
  78. return
  79. }
  80. channel_, err := model.GetChannelById(id, userId, false)
  81. if err != nil {
  82. c.JSON(http.StatusOK, gin.H{
  83. "success": false,
  84. "message": err.Error(),
  85. })
  86. return
  87. }
  88. c.JSON(http.StatusOK, gin.H{
  89. "success": true,
  90. "message": "",
  91. "data": channel_,
  92. })
  93. return
  94. }
  95. func AddChannel(c *gin.Context) {
  96. channel_ := model.Channel{}
  97. err := c.ShouldBindJSON(&channel_)
  98. if err != nil {
  99. c.JSON(http.StatusOK, gin.H{
  100. "success": false,
  101. "message": err.Error(),
  102. })
  103. return
  104. }
  105. if len(channel_.Name) == 0 || len(channel_.Name) > 20 {
  106. c.JSON(http.StatusOK, gin.H{
  107. "success": false,
  108. "message": "通道名称长度必须在1-20之间",
  109. })
  110. return
  111. }
  112. if channel_.Name == "email" {
  113. c.JSON(http.StatusOK, gin.H{
  114. "success": false,
  115. "message": "不能使用系统保留名称",
  116. })
  117. return
  118. }
  119. cleanChannel := model.Channel{
  120. Type: channel_.Type,
  121. UserId: c.GetInt("id"),
  122. Name: channel_.Name,
  123. Description: channel_.Description,
  124. Status: common.ChannelStatusEnabled,
  125. Secret: channel_.Secret,
  126. AppId: channel_.AppId,
  127. AccountId: channel_.AccountId,
  128. URL: channel_.URL,
  129. Other: channel_.Other,
  130. CreatedTime: common.GetTimestamp(),
  131. Token: channel_.Token,
  132. }
  133. err = cleanChannel.Insert()
  134. if err != nil {
  135. c.JSON(http.StatusOK, gin.H{
  136. "success": false,
  137. "message": err.Error(),
  138. })
  139. return
  140. }
  141. channel.TokenStoreAddChannel(&cleanChannel)
  142. c.JSON(http.StatusOK, gin.H{
  143. "success": true,
  144. "message": "",
  145. })
  146. return
  147. }
  148. func DeleteChannel(c *gin.Context) {
  149. id, _ := strconv.Atoi(c.Param("id"))
  150. userId := c.GetInt("id")
  151. channel_, err := model.DeleteChannelById(id, userId)
  152. if err != nil {
  153. c.JSON(http.StatusOK, gin.H{
  154. "success": false,
  155. "message": err.Error(),
  156. })
  157. return
  158. }
  159. channel.TokenStoreRemoveChannel(channel_)
  160. c.JSON(http.StatusOK, gin.H{
  161. "success": true,
  162. "message": "",
  163. })
  164. return
  165. }
  166. func UpdateChannel(c *gin.Context) {
  167. userId := c.GetInt("id")
  168. statusOnly := c.Query("status_only")
  169. channel_ := model.Channel{}
  170. err := c.ShouldBindJSON(&channel_)
  171. if err != nil {
  172. c.JSON(http.StatusOK, gin.H{
  173. "success": false,
  174. "message": err.Error(),
  175. })
  176. return
  177. }
  178. oldChannel, err := model.GetChannelById(channel_.Id, userId, true)
  179. if err != nil {
  180. c.JSON(http.StatusOK, gin.H{
  181. "success": false,
  182. "message": err.Error(),
  183. })
  184. return
  185. }
  186. cleanChannel := *oldChannel
  187. if statusOnly != "" {
  188. cleanChannel.Status = channel_.Status
  189. } else {
  190. // If you add more fields, please also update channel_.Update()
  191. cleanChannel.Type = channel_.Type
  192. cleanChannel.Name = channel_.Name
  193. cleanChannel.Description = channel_.Description
  194. if channel_.Secret != "" {
  195. cleanChannel.Secret = channel_.Secret
  196. }
  197. cleanChannel.AppId = channel_.AppId
  198. cleanChannel.AccountId = channel_.AccountId
  199. cleanChannel.URL = channel_.URL
  200. cleanChannel.Other = channel_.Other
  201. cleanChannel.Token = channel_.Token
  202. }
  203. err = cleanChannel.Update()
  204. if err != nil {
  205. c.JSON(http.StatusOK, gin.H{
  206. "success": false,
  207. "message": err.Error(),
  208. })
  209. return
  210. }
  211. channel.TokenStoreUpdateChannel(&cleanChannel, oldChannel)
  212. c.JSON(http.StatusOK, gin.H{
  213. "success": true,
  214. "message": "",
  215. "data": cleanChannel,
  216. })
  217. return
  218. }