redemption.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package controller
  2. import (
  3. "net/http"
  4. "strconv"
  5. "unicode/utf8"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/i18n"
  8. "github.com/QuantumNous/new-api/model"
  9. "github.com/gin-gonic/gin"
  10. )
  11. func GetAllRedemptions(c *gin.Context) {
  12. pageInfo := common.GetPageQuery(c)
  13. redemptions, total, err := model.GetAllRedemptions(pageInfo.GetStartIdx(), pageInfo.GetPageSize())
  14. if err != nil {
  15. common.ApiError(c, err)
  16. return
  17. }
  18. pageInfo.SetTotal(int(total))
  19. pageInfo.SetItems(redemptions)
  20. common.ApiSuccess(c, pageInfo)
  21. return
  22. }
  23. func SearchRedemptions(c *gin.Context) {
  24. keyword := c.Query("keyword")
  25. pageInfo := common.GetPageQuery(c)
  26. redemptions, total, err := model.SearchRedemptions(keyword, pageInfo.GetStartIdx(), pageInfo.GetPageSize())
  27. if err != nil {
  28. common.ApiError(c, err)
  29. return
  30. }
  31. pageInfo.SetTotal(int(total))
  32. pageInfo.SetItems(redemptions)
  33. common.ApiSuccess(c, pageInfo)
  34. return
  35. }
  36. func GetRedemption(c *gin.Context) {
  37. id, err := strconv.Atoi(c.Param("id"))
  38. if err != nil {
  39. common.ApiError(c, err)
  40. return
  41. }
  42. redemption, err := model.GetRedemptionById(id)
  43. if err != nil {
  44. common.ApiError(c, err)
  45. return
  46. }
  47. c.JSON(http.StatusOK, gin.H{
  48. "success": true,
  49. "message": "",
  50. "data": redemption,
  51. })
  52. return
  53. }
  54. func AddRedemption(c *gin.Context) {
  55. redemption := model.Redemption{}
  56. err := c.ShouldBindJSON(&redemption)
  57. if err != nil {
  58. common.ApiError(c, err)
  59. return
  60. }
  61. if utf8.RuneCountInString(redemption.Name) == 0 || utf8.RuneCountInString(redemption.Name) > 20 {
  62. common.ApiErrorI18n(c, i18n.MsgRedemptionNameLength)
  63. return
  64. }
  65. if redemption.Count <= 0 {
  66. common.ApiErrorI18n(c, i18n.MsgRedemptionCountPositive)
  67. return
  68. }
  69. if redemption.Count > 100 {
  70. common.ApiErrorI18n(c, i18n.MsgRedemptionCountMax)
  71. return
  72. }
  73. if valid, msg := validateExpiredTime(c, redemption.ExpiredTime); !valid {
  74. c.JSON(http.StatusOK, gin.H{"success": false, "message": msg})
  75. return
  76. }
  77. var keys []string
  78. for i := 0; i < redemption.Count; i++ {
  79. key := common.GetUUID()
  80. cleanRedemption := model.Redemption{
  81. UserId: c.GetInt("id"),
  82. Name: redemption.Name,
  83. Key: key,
  84. CreatedTime: common.GetTimestamp(),
  85. Quota: redemption.Quota,
  86. ExpiredTime: redemption.ExpiredTime,
  87. }
  88. err = cleanRedemption.Insert()
  89. if err != nil {
  90. common.SysError("failed to insert redemption: " + err.Error())
  91. c.JSON(http.StatusOK, gin.H{
  92. "success": false,
  93. "message": i18n.T(c, i18n.MsgRedemptionCreateFailed),
  94. "data": keys,
  95. })
  96. return
  97. }
  98. keys = append(keys, key)
  99. }
  100. c.JSON(http.StatusOK, gin.H{
  101. "success": true,
  102. "message": "",
  103. "data": keys,
  104. })
  105. return
  106. }
  107. func DeleteRedemption(c *gin.Context) {
  108. id, _ := strconv.Atoi(c.Param("id"))
  109. err := model.DeleteRedemptionById(id)
  110. if err != nil {
  111. common.ApiError(c, err)
  112. return
  113. }
  114. c.JSON(http.StatusOK, gin.H{
  115. "success": true,
  116. "message": "",
  117. })
  118. return
  119. }
  120. func UpdateRedemption(c *gin.Context) {
  121. statusOnly := c.Query("status_only")
  122. redemption := model.Redemption{}
  123. err := c.ShouldBindJSON(&redemption)
  124. if err != nil {
  125. common.ApiError(c, err)
  126. return
  127. }
  128. cleanRedemption, err := model.GetRedemptionById(redemption.Id)
  129. if err != nil {
  130. common.ApiError(c, err)
  131. return
  132. }
  133. if statusOnly == "" {
  134. if valid, msg := validateExpiredTime(c, redemption.ExpiredTime); !valid {
  135. c.JSON(http.StatusOK, gin.H{"success": false, "message": msg})
  136. return
  137. }
  138. // If you add more fields, please also update redemption.Update()
  139. cleanRedemption.Name = redemption.Name
  140. cleanRedemption.Quota = redemption.Quota
  141. cleanRedemption.ExpiredTime = redemption.ExpiredTime
  142. }
  143. if statusOnly != "" {
  144. cleanRedemption.Status = redemption.Status
  145. }
  146. err = cleanRedemption.Update()
  147. if err != nil {
  148. common.ApiError(c, err)
  149. return
  150. }
  151. c.JSON(http.StatusOK, gin.H{
  152. "success": true,
  153. "message": "",
  154. "data": cleanRedemption,
  155. })
  156. return
  157. }
  158. func DeleteInvalidRedemption(c *gin.Context) {
  159. rows, err := model.DeleteInvalidRedemptions()
  160. if err != nil {
  161. common.ApiError(c, err)
  162. return
  163. }
  164. c.JSON(http.StatusOK, gin.H{
  165. "success": true,
  166. "message": "",
  167. "data": rows,
  168. })
  169. return
  170. }
  171. func validateExpiredTime(c *gin.Context, expired int64) (bool, string) {
  172. if expired != 0 && expired < common.GetTimestamp() {
  173. return false, i18n.T(c, i18n.MsgRedemptionExpireTimeInvalid)
  174. }
  175. return true, ""
  176. }