channel.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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)
  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. channel.AccessedTime = common.GetTimestamp()
  83. keys := strings.Split(channel.Key, "\n")
  84. channels := make([]model.Channel, 0)
  85. for _, key := range keys {
  86. if key == "" {
  87. continue
  88. }
  89. localChannel := channel
  90. localChannel.Key = key
  91. channels = append(channels, localChannel)
  92. }
  93. err = model.BatchInsertChannels(channels)
  94. if err != nil {
  95. c.JSON(http.StatusOK, gin.H{
  96. "success": false,
  97. "message": err.Error(),
  98. })
  99. return
  100. }
  101. c.JSON(http.StatusOK, gin.H{
  102. "success": true,
  103. "message": "",
  104. })
  105. return
  106. }
  107. func DeleteChannel(c *gin.Context) {
  108. id, _ := strconv.Atoi(c.Param("id"))
  109. channel := model.Channel{Id: id}
  110. err := channel.Delete()
  111. if err != nil {
  112. c.JSON(http.StatusOK, gin.H{
  113. "success": false,
  114. "message": err.Error(),
  115. })
  116. return
  117. }
  118. c.JSON(http.StatusOK, gin.H{
  119. "success": true,
  120. "message": "",
  121. })
  122. return
  123. }
  124. func UpdateChannel(c *gin.Context) {
  125. channel := model.Channel{}
  126. err := c.ShouldBindJSON(&channel)
  127. if err != nil {
  128. c.JSON(http.StatusOK, gin.H{
  129. "success": false,
  130. "message": err.Error(),
  131. })
  132. return
  133. }
  134. err = channel.Update()
  135. if err != nil {
  136. c.JSON(http.StatusOK, gin.H{
  137. "success": false,
  138. "message": err.Error(),
  139. })
  140. return
  141. }
  142. c.JSON(http.StatusOK, gin.H{
  143. "success": true,
  144. "message": "",
  145. "data": channel,
  146. })
  147. return
  148. }