channel.go 4.2 KB

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