channel.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. cleanChannel := model.Channel{
  92. Type: channel_.Type,
  93. UserId: c.GetInt("id"),
  94. Name: channel_.Name,
  95. Description: channel_.Description,
  96. Status: common.ChannelStatusEnabled,
  97. Secret: channel_.Secret,
  98. AppId: channel_.AppId,
  99. AccountId: channel_.AccountId,
  100. URL: channel_.URL,
  101. Other: channel_.Other,
  102. CreatedTime: common.GetTimestamp(),
  103. }
  104. err = cleanChannel.Insert()
  105. if err != nil {
  106. c.JSON(http.StatusOK, gin.H{
  107. "success": false,
  108. "message": err.Error(),
  109. })
  110. return
  111. }
  112. channel.TokenStoreAddChannel(&cleanChannel)
  113. c.JSON(http.StatusOK, gin.H{
  114. "success": true,
  115. "message": "",
  116. })
  117. return
  118. }
  119. func DeleteChannel(c *gin.Context) {
  120. id, _ := strconv.Atoi(c.Param("id"))
  121. userId := c.GetInt("id")
  122. channel_, err := model.DeleteChannelById(id, userId)
  123. if err != nil {
  124. c.JSON(http.StatusOK, gin.H{
  125. "success": false,
  126. "message": err.Error(),
  127. })
  128. return
  129. }
  130. channel.TokenStoreRemoveChannel(channel_)
  131. c.JSON(http.StatusOK, gin.H{
  132. "success": true,
  133. "message": "",
  134. })
  135. return
  136. }
  137. func UpdateChannel(c *gin.Context) {
  138. userId := c.GetInt("id")
  139. statusOnly := c.Query("status_only")
  140. channel_ := model.Channel{}
  141. err := c.ShouldBindJSON(&channel_)
  142. if err != nil {
  143. c.JSON(http.StatusOK, gin.H{
  144. "success": false,
  145. "message": err.Error(),
  146. })
  147. return
  148. }
  149. oldChannel, err := model.GetChannelById(channel_.Id, userId)
  150. if err != nil {
  151. c.JSON(http.StatusOK, gin.H{
  152. "success": false,
  153. "message": err.Error(),
  154. })
  155. return
  156. }
  157. cleanChannel := oldChannel
  158. if statusOnly != "" {
  159. cleanChannel.Status = channel_.Status
  160. } else {
  161. // If you add more fields, please also update channel_.Update()
  162. cleanChannel.Type = channel_.Type
  163. cleanChannel.Name = channel_.Name
  164. cleanChannel.Description = channel_.Description
  165. cleanChannel.Secret = channel_.Secret
  166. cleanChannel.AppId = channel_.AppId
  167. cleanChannel.AccountId = channel_.AccountId
  168. cleanChannel.URL = channel_.URL
  169. cleanChannel.Other = channel_.Other
  170. }
  171. err = cleanChannel.Update()
  172. if err != nil {
  173. c.JSON(http.StatusOK, gin.H{
  174. "success": false,
  175. "message": err.Error(),
  176. })
  177. return
  178. }
  179. channel.TokenStoreUpdateChannel(cleanChannel, oldChannel)
  180. c.JSON(http.StatusOK, gin.H{
  181. "success": true,
  182. "message": "",
  183. "data": cleanChannel,
  184. })
  185. return
  186. }