channel.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 UpdateChannel(c *gin.Context) {
  128. channel := model.Channel{}
  129. err := c.ShouldBindJSON(&channel)
  130. if err != nil {
  131. c.JSON(http.StatusOK, gin.H{
  132. "success": false,
  133. "message": err.Error(),
  134. })
  135. return
  136. }
  137. err = channel.Update()
  138. if err != nil {
  139. c.JSON(http.StatusOK, gin.H{
  140. "success": false,
  141. "message": err.Error(),
  142. })
  143. return
  144. }
  145. c.JSON(http.StatusOK, gin.H{
  146. "success": true,
  147. "message": "",
  148. "data": channel,
  149. })
  150. return
  151. }