channel.go 2.5 KB

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