channel.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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)
  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. }
  132. err = cleanChannel.Insert()
  133. if err != nil {
  134. c.JSON(http.StatusOK, gin.H{
  135. "success": false,
  136. "message": err.Error(),
  137. })
  138. return
  139. }
  140. channel.TokenStoreAddChannel(&cleanChannel)
  141. c.JSON(http.StatusOK, gin.H{
  142. "success": true,
  143. "message": "",
  144. })
  145. return
  146. }
  147. func DeleteChannel(c *gin.Context) {
  148. id, _ := strconv.Atoi(c.Param("id"))
  149. userId := c.GetInt("id")
  150. channel_, err := model.DeleteChannelById(id, userId)
  151. if err != nil {
  152. c.JSON(http.StatusOK, gin.H{
  153. "success": false,
  154. "message": err.Error(),
  155. })
  156. return
  157. }
  158. channel.TokenStoreRemoveChannel(channel_)
  159. c.JSON(http.StatusOK, gin.H{
  160. "success": true,
  161. "message": "",
  162. })
  163. return
  164. }
  165. func UpdateChannel(c *gin.Context) {
  166. userId := c.GetInt("id")
  167. statusOnly := c.Query("status_only")
  168. channel_ := model.Channel{}
  169. err := c.ShouldBindJSON(&channel_)
  170. if err != nil {
  171. c.JSON(http.StatusOK, gin.H{
  172. "success": false,
  173. "message": err.Error(),
  174. })
  175. return
  176. }
  177. oldChannel, err := model.GetChannelById(channel_.Id, userId)
  178. if err != nil {
  179. c.JSON(http.StatusOK, gin.H{
  180. "success": false,
  181. "message": err.Error(),
  182. })
  183. return
  184. }
  185. cleanChannel := *oldChannel
  186. if statusOnly != "" {
  187. cleanChannel.Status = channel_.Status
  188. } else {
  189. // If you add more fields, please also update channel_.Update()
  190. cleanChannel.Type = channel_.Type
  191. cleanChannel.Name = channel_.Name
  192. cleanChannel.Description = channel_.Description
  193. cleanChannel.Secret = channel_.Secret
  194. cleanChannel.AppId = channel_.AppId
  195. cleanChannel.AccountId = channel_.AccountId
  196. cleanChannel.URL = channel_.URL
  197. cleanChannel.Other = channel_.Other
  198. }
  199. err = cleanChannel.Update()
  200. if err != nil {
  201. c.JSON(http.StatusOK, gin.H{
  202. "success": false,
  203. "message": err.Error(),
  204. })
  205. return
  206. }
  207. channel.TokenStoreUpdateChannel(&cleanChannel, oldChannel)
  208. c.JSON(http.StatusOK, gin.H{
  209. "success": true,
  210. "message": "",
  211. "data": cleanChannel,
  212. })
  213. return
  214. }