redemption.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 GetAllRedemptions(c *gin.Context) {
  10. p, _ := strconv.Atoi(c.Query("p"))
  11. if p < 0 {
  12. p = 0
  13. }
  14. redemptions, err := model.GetAllRedemptions(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": redemptions,
  26. })
  27. return
  28. }
  29. func SearchRedemptions(c *gin.Context) {
  30. keyword := c.Query("keyword")
  31. redemptions, err := model.SearchRedemptions(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": redemptions,
  43. })
  44. return
  45. }
  46. func GetRedemption(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. redemption, err := model.GetRedemptionById(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": redemption,
  67. })
  68. return
  69. }
  70. func AddRedemption(c *gin.Context) {
  71. redemption := model.Redemption{}
  72. err := c.ShouldBindJSON(&redemption)
  73. if err != nil {
  74. c.JSON(http.StatusOK, gin.H{
  75. "success": false,
  76. "message": err.Error(),
  77. })
  78. return
  79. }
  80. if len(redemption.Name) == 0 || len(redemption.Name) > 20 {
  81. c.JSON(http.StatusOK, gin.H{
  82. "success": false,
  83. "message": "兑换码名称长度必须在1-20之间",
  84. })
  85. return
  86. }
  87. if redemption.Count <= 0 {
  88. c.JSON(http.StatusOK, gin.H{
  89. "success": false,
  90. "message": "兑换码个数必须大于0",
  91. })
  92. return
  93. }
  94. if redemption.Count > 100 {
  95. c.JSON(http.StatusOK, gin.H{
  96. "success": false,
  97. "message": "一次兑换码批量生成的个数不能大于 100",
  98. })
  99. return
  100. }
  101. var keys []string
  102. for i := 0; i < redemption.Count; i++ {
  103. key := common.GetUUID()
  104. cleanRedemption := model.Redemption{
  105. UserId: c.GetInt("id"),
  106. Name: redemption.Name,
  107. Key: key,
  108. CreatedTime: common.GetTimestamp(),
  109. Quota: redemption.Quota,
  110. }
  111. err = cleanRedemption.Insert()
  112. if err != nil {
  113. c.JSON(http.StatusOK, gin.H{
  114. "success": false,
  115. "message": err.Error(),
  116. "data": keys,
  117. })
  118. return
  119. }
  120. keys = append(keys, key)
  121. }
  122. c.JSON(http.StatusOK, gin.H{
  123. "success": true,
  124. "message": "",
  125. "data": keys,
  126. })
  127. return
  128. }
  129. func DeleteRedemption(c *gin.Context) {
  130. id, _ := strconv.Atoi(c.Param("id"))
  131. err := model.DeleteRedemptionById(id)
  132. if err != nil {
  133. c.JSON(http.StatusOK, gin.H{
  134. "success": false,
  135. "message": err.Error(),
  136. })
  137. return
  138. }
  139. c.JSON(http.StatusOK, gin.H{
  140. "success": true,
  141. "message": "",
  142. })
  143. return
  144. }
  145. func UpdateRedemption(c *gin.Context) {
  146. statusOnly := c.Query("status_only")
  147. redemption := model.Redemption{}
  148. err := c.ShouldBindJSON(&redemption)
  149. if err != nil {
  150. c.JSON(http.StatusOK, gin.H{
  151. "success": false,
  152. "message": err.Error(),
  153. })
  154. return
  155. }
  156. cleanRedemption, err := model.GetRedemptionById(redemption.Id)
  157. if err != nil {
  158. c.JSON(http.StatusOK, gin.H{
  159. "success": false,
  160. "message": err.Error(),
  161. })
  162. return
  163. }
  164. if statusOnly != "" {
  165. cleanRedemption.Status = redemption.Status
  166. } else {
  167. // If you add more fields, please also update redemption.Update()
  168. cleanRedemption.Name = redemption.Name
  169. cleanRedemption.Quota = redemption.Quota
  170. }
  171. err = cleanRedemption.Update()
  172. if err != nil {
  173. c.JSON(http.StatusOK, gin.H{
  174. "success": false,
  175. "message": err.Error(),
  176. })
  177. return
  178. }
  179. c.JSON(http.StatusOK, gin.H{
  180. "success": true,
  181. "message": "",
  182. "data": cleanRedemption,
  183. })
  184. return
  185. }