channel.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "one-api/common"
  6. "one-api/model"
  7. "strconv"
  8. "strings"
  9. )
  10. func GetAllChannels(c *gin.Context) {
  11. p, _ := strconv.Atoi(c.Query("p"))
  12. pageSize, _ := strconv.Atoi(c.Query("page_size"))
  13. if p < 0 {
  14. p = 0
  15. }
  16. if pageSize < 0 {
  17. pageSize = common.ItemsPerPage
  18. }
  19. idSort, _ := strconv.ParseBool(c.Query("id_sort"))
  20. channels, err := model.GetAllChannels(p*pageSize, pageSize, false, idSort)
  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 SearchChannels(c *gin.Context) {
  36. keyword := c.Query("keyword")
  37. group := c.Query("group")
  38. //idSort, _ := strconv.ParseBool(c.Query("id_sort"))
  39. channels, err := model.SearchChannels(keyword, group)
  40. if err != nil {
  41. c.JSON(http.StatusOK, gin.H{
  42. "success": false,
  43. "message": err.Error(),
  44. })
  45. return
  46. }
  47. c.JSON(http.StatusOK, gin.H{
  48. "success": true,
  49. "message": "",
  50. "data": channels,
  51. })
  52. return
  53. }
  54. func GetChannel(c *gin.Context) {
  55. id, err := strconv.Atoi(c.Param("id"))
  56. if err != nil {
  57. c.JSON(http.StatusOK, gin.H{
  58. "success": false,
  59. "message": err.Error(),
  60. })
  61. return
  62. }
  63. channel, err := model.GetChannelById(id, false)
  64. if err != nil {
  65. c.JSON(http.StatusOK, gin.H{
  66. "success": false,
  67. "message": err.Error(),
  68. })
  69. return
  70. }
  71. c.JSON(http.StatusOK, gin.H{
  72. "success": true,
  73. "message": "",
  74. "data": channel,
  75. })
  76. return
  77. }
  78. func AddChannel(c *gin.Context) {
  79. channel := model.Channel{}
  80. err := c.ShouldBindJSON(&channel)
  81. if err != nil {
  82. c.JSON(http.StatusOK, gin.H{
  83. "success": false,
  84. "message": err.Error(),
  85. })
  86. return
  87. }
  88. channel.CreatedTime = common.GetTimestamp()
  89. keys := strings.Split(channel.Key, "\n")
  90. channels := make([]model.Channel, 0, len(keys))
  91. for _, key := range keys {
  92. if key == "" {
  93. continue
  94. }
  95. localChannel := channel
  96. localChannel.Key = key
  97. channels = append(channels, localChannel)
  98. }
  99. err = model.BatchInsertChannels(channels)
  100. if err != nil {
  101. c.JSON(http.StatusOK, gin.H{
  102. "success": false,
  103. "message": err.Error(),
  104. })
  105. return
  106. }
  107. c.JSON(http.StatusOK, gin.H{
  108. "success": true,
  109. "message": "",
  110. })
  111. return
  112. }
  113. func DeleteChannel(c *gin.Context) {
  114. id, _ := strconv.Atoi(c.Param("id"))
  115. channel := model.Channel{Id: id}
  116. err := channel.Delete()
  117. if err != nil {
  118. c.JSON(http.StatusOK, gin.H{
  119. "success": false,
  120. "message": err.Error(),
  121. })
  122. return
  123. }
  124. c.JSON(http.StatusOK, gin.H{
  125. "success": true,
  126. "message": "",
  127. })
  128. return
  129. }
  130. func DeleteDisabledChannel(c *gin.Context) {
  131. rows, err := model.DeleteDisabledChannel()
  132. if err != nil {
  133. c.JSON(http.StatusOK, gin.H{
  134. "success": false,
  135. "message": err.Error(),
  136. })
  137. return
  138. }
  139. c.JSON(http.StatusOK, gin.H{
  140. "success": true,
  141. "message": "",
  142. "data": rows,
  143. })
  144. return
  145. }
  146. func UpdateChannel(c *gin.Context) {
  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. err = channel.Update()
  157. if err != nil {
  158. c.JSON(http.StatusOK, gin.H{
  159. "success": false,
  160. "message": err.Error(),
  161. })
  162. return
  163. }
  164. c.JSON(http.StatusOK, gin.H{
  165. "success": true,
  166. "message": "",
  167. "data": channel,
  168. })
  169. return
  170. }