channel.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. channels, err := model.GetAllChannels(p*pageSize, pageSize, false)
  20. if err != nil {
  21. c.JSON(http.StatusOK, gin.H{
  22. "success": false,
  23. "message": err.Error(),
  24. })
  25. return
  26. }
  27. c.JSON(http.StatusOK, gin.H{
  28. "success": true,
  29. "message": "",
  30. "data": channels,
  31. })
  32. return
  33. }
  34. func SearchChannels(c *gin.Context) {
  35. keyword := c.Query("keyword")
  36. channels, err := model.SearchChannels(keyword)
  37. if err != nil {
  38. c.JSON(http.StatusOK, gin.H{
  39. "success": false,
  40. "message": err.Error(),
  41. })
  42. return
  43. }
  44. c.JSON(http.StatusOK, gin.H{
  45. "success": true,
  46. "message": "",
  47. "data": channels,
  48. })
  49. return
  50. }
  51. func GetChannel(c *gin.Context) {
  52. id, err := strconv.Atoi(c.Param("id"))
  53. if err != nil {
  54. c.JSON(http.StatusOK, gin.H{
  55. "success": false,
  56. "message": err.Error(),
  57. })
  58. return
  59. }
  60. channel, err := model.GetChannelById(id, false)
  61. if err != nil {
  62. c.JSON(http.StatusOK, gin.H{
  63. "success": false,
  64. "message": err.Error(),
  65. })
  66. return
  67. }
  68. c.JSON(http.StatusOK, gin.H{
  69. "success": true,
  70. "message": "",
  71. "data": channel,
  72. })
  73. return
  74. }
  75. func AddChannel(c *gin.Context) {
  76. channel := model.Channel{}
  77. err := c.ShouldBindJSON(&channel)
  78. if err != nil {
  79. c.JSON(http.StatusOK, gin.H{
  80. "success": false,
  81. "message": err.Error(),
  82. })
  83. return
  84. }
  85. channel.CreatedTime = common.GetTimestamp()
  86. keys := strings.Split(channel.Key, "\n")
  87. channels := make([]model.Channel, 0, len(keys))
  88. for _, key := range keys {
  89. if key == "" {
  90. continue
  91. }
  92. localChannel := channel
  93. localChannel.Key = key
  94. channels = append(channels, localChannel)
  95. }
  96. err = model.BatchInsertChannels(channels)
  97. if err != nil {
  98. c.JSON(http.StatusOK, gin.H{
  99. "success": false,
  100. "message": err.Error(),
  101. })
  102. return
  103. }
  104. c.JSON(http.StatusOK, gin.H{
  105. "success": true,
  106. "message": "",
  107. })
  108. return
  109. }
  110. func DeleteChannel(c *gin.Context) {
  111. id, _ := strconv.Atoi(c.Param("id"))
  112. channel := model.Channel{Id: id}
  113. err := channel.Delete()
  114. if err != nil {
  115. c.JSON(http.StatusOK, gin.H{
  116. "success": false,
  117. "message": err.Error(),
  118. })
  119. return
  120. }
  121. c.JSON(http.StatusOK, gin.H{
  122. "success": true,
  123. "message": "",
  124. })
  125. return
  126. }
  127. func DeleteDisabledChannel(c *gin.Context) {
  128. rows, err := model.DeleteDisabledChannel()
  129. if err != nil {
  130. c.JSON(http.StatusOK, gin.H{
  131. "success": false,
  132. "message": err.Error(),
  133. })
  134. return
  135. }
  136. c.JSON(http.StatusOK, gin.H{
  137. "success": true,
  138. "message": "",
  139. "data": rows,
  140. })
  141. return
  142. }
  143. func UpdateChannel(c *gin.Context) {
  144. channel := model.Channel{}
  145. err := c.ShouldBindJSON(&channel)
  146. if err != nil {
  147. c.JSON(http.StatusOK, gin.H{
  148. "success": false,
  149. "message": err.Error(),
  150. })
  151. return
  152. }
  153. err = channel.Update()
  154. if err != nil {
  155. c.JSON(http.StatusOK, gin.H{
  156. "success": false,
  157. "message": err.Error(),
  158. })
  159. return
  160. }
  161. c.JSON(http.StatusOK, gin.H{
  162. "success": true,
  163. "message": "",
  164. "data": channel,
  165. })
  166. return
  167. }