token.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 GetAllTokens(c *gin.Context) {
  10. userId := c.GetInt("id")
  11. p, _ := strconv.Atoi(c.Query("p"))
  12. if p < 0 {
  13. p = 0
  14. }
  15. tokens, err := model.GetAllUserTokens(userId, 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": tokens,
  27. })
  28. return
  29. }
  30. func SearchTokens(c *gin.Context) {
  31. userId := c.GetInt("id")
  32. keyword := c.Query("keyword")
  33. tokens, err := model.SearchUserTokens(userId, keyword)
  34. if err != nil {
  35. c.JSON(http.StatusOK, gin.H{
  36. "success": false,
  37. "message": err.Error(),
  38. })
  39. return
  40. }
  41. c.JSON(http.StatusOK, gin.H{
  42. "success": true,
  43. "message": "",
  44. "data": tokens,
  45. })
  46. return
  47. }
  48. func GetToken(c *gin.Context) {
  49. id, err := strconv.Atoi(c.Param("id"))
  50. if err != nil {
  51. c.JSON(http.StatusOK, gin.H{
  52. "success": false,
  53. "message": err.Error(),
  54. })
  55. return
  56. }
  57. token, err := model.GetTokenById(id)
  58. if err != nil {
  59. c.JSON(http.StatusOK, gin.H{
  60. "success": false,
  61. "message": err.Error(),
  62. })
  63. return
  64. }
  65. c.JSON(http.StatusOK, gin.H{
  66. "success": true,
  67. "message": "",
  68. "data": token,
  69. })
  70. return
  71. }
  72. func AddToken(c *gin.Context) {
  73. token := model.Token{}
  74. err := c.ShouldBindJSON(&token)
  75. if err != nil {
  76. c.JSON(http.StatusOK, gin.H{
  77. "success": false,
  78. "message": err.Error(),
  79. })
  80. return
  81. }
  82. err = token.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 DeleteToken(c *gin.Context) {
  97. id, _ := strconv.Atoi(c.Param("id"))
  98. token := model.Token{Id: id}
  99. err := token.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 UpdateToken(c *gin.Context) {
  114. token := model.Token{}
  115. err := c.ShouldBindJSON(&token)
  116. if err != nil {
  117. c.JSON(http.StatusOK, gin.H{
  118. "success": false,
  119. "message": err.Error(),
  120. })
  121. return
  122. }
  123. err = token.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. })
  135. return
  136. }