channel.go 2.8 KB

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