2
0

channel.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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)
  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. err = channel.Insert()
  81. if err != nil {
  82. c.JSON(http.StatusOK, gin.H{
  83. "success": false,
  84. "message": err.Error(),
  85. })
  86. return
  87. }
  88. c.JSON(http.StatusOK, gin.H{
  89. "success": true,
  90. "message": "",
  91. })
  92. return
  93. }
  94. func DeleteChannel(c *gin.Context) {
  95. id, _ := strconv.Atoi(c.Param("id"))
  96. channel := model.Channel{Id: id}
  97. err := channel.Delete()
  98. if err != nil {
  99. c.JSON(http.StatusOK, gin.H{
  100. "success": false,
  101. "message": err.Error(),
  102. })
  103. return
  104. }
  105. c.JSON(http.StatusOK, gin.H{
  106. "success": true,
  107. "message": "",
  108. })
  109. return
  110. }
  111. func UpdateChannel(c *gin.Context) {
  112. channel := model.Channel{}
  113. err := c.ShouldBindJSON(&channel)
  114. if err != nil {
  115. c.JSON(http.StatusOK, gin.H{
  116. "success": false,
  117. "message": err.Error(),
  118. })
  119. return
  120. }
  121. err = channel.Update()
  122. if err != nil {
  123. c.JSON(http.StatusOK, gin.H{
  124. "success": false,
  125. "message": err.Error(),
  126. })
  127. return
  128. }
  129. c.JSON(http.StatusOK, gin.H{
  130. "success": true,
  131. "message": "",
  132. })
  133. return
  134. }