channel.go 3.1 KB

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