|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gin-gonic/gin" |
| 5 | + "net/http" |
| 6 | + "one-api/common" |
| 7 | + "one-api/model" |
| 8 | + "strconv" |
| 9 | +) |
| 10 | + |
| 11 | +func GetAllRedemptions(c *gin.Context) { |
| 12 | + p, _ := strconv.Atoi(c.Query("p")) |
| 13 | + if p < 0 { |
| 14 | + p = 0 |
| 15 | + } |
| 16 | + redemptions, err := model.GetAllRedemptions(p*common.ItemsPerPage, common.ItemsPerPage) |
| 17 | + if err != nil { |
| 18 | + c.JSON(http.StatusOK, gin.H{ |
| 19 | + "success": false, |
| 20 | + "message": err.Error(), |
| 21 | + }) |
| 22 | + return |
| 23 | + } |
| 24 | + c.JSON(http.StatusOK, gin.H{ |
| 25 | + "success": true, |
| 26 | + "message": "", |
| 27 | + "data": redemptions, |
| 28 | + }) |
| 29 | + return |
| 30 | +} |
| 31 | + |
| 32 | +func SearchRedemptions(c *gin.Context) { |
| 33 | + keyword := c.Query("keyword") |
| 34 | + redemptions, err := model.SearchRedemptions(keyword) |
| 35 | + if err != nil { |
| 36 | + c.JSON(http.StatusOK, gin.H{ |
| 37 | + "success": false, |
| 38 | + "message": err.Error(), |
| 39 | + }) |
| 40 | + return |
| 41 | + } |
| 42 | + c.JSON(http.StatusOK, gin.H{ |
| 43 | + "success": true, |
| 44 | + "message": "", |
| 45 | + "data": redemptions, |
| 46 | + }) |
| 47 | + return |
| 48 | +} |
| 49 | + |
| 50 | +func GetRedemption(c *gin.Context) { |
| 51 | + id, err := strconv.Atoi(c.Param("id")) |
| 52 | + if err != nil { |
| 53 | + c.JSON(http.StatusOK, gin.H{ |
| 54 | + "success": false, |
| 55 | + "message": err.Error(), |
| 56 | + }) |
| 57 | + return |
| 58 | + } |
| 59 | + redemption, err := model.GetRedemptionById(id) |
| 60 | + if err != nil { |
| 61 | + c.JSON(http.StatusOK, gin.H{ |
| 62 | + "success": false, |
| 63 | + "message": err.Error(), |
| 64 | + }) |
| 65 | + return |
| 66 | + } |
| 67 | + c.JSON(http.StatusOK, gin.H{ |
| 68 | + "success": true, |
| 69 | + "message": "", |
| 70 | + "data": redemption, |
| 71 | + }) |
| 72 | + return |
| 73 | +} |
| 74 | + |
| 75 | +func AddRedemption(c *gin.Context) { |
| 76 | + redemption := model.Redemption{} |
| 77 | + err := c.ShouldBindJSON(&redemption) |
| 78 | + if err != nil { |
| 79 | + c.JSON(http.StatusOK, gin.H{ |
| 80 | + "success": false, |
| 81 | + "message": err.Error(), |
| 82 | + }) |
| 83 | + return |
| 84 | + } |
| 85 | + if len(redemption.Name) == 0 || len(redemption.Name) > 20 { |
| 86 | + c.JSON(http.StatusOK, gin.H{ |
| 87 | + "success": false, |
| 88 | + "message": "兑换码名称长度必须在1-20之间", |
| 89 | + }) |
| 90 | + return |
| 91 | + } |
| 92 | + if redemption.Count <= 0 { |
| 93 | + c.JSON(http.StatusOK, gin.H{ |
| 94 | + "success": false, |
| 95 | + "message": "兑换码个数必须大于0", |
| 96 | + }) |
| 97 | + return |
| 98 | + } |
| 99 | + if redemption.Count > 100 { |
| 100 | + c.JSON(http.StatusOK, gin.H{ |
| 101 | + "success": false, |
| 102 | + "message": "一次兑换码批量生成的个数不能大于 100", |
| 103 | + }) |
| 104 | + return |
| 105 | + } |
| 106 | + var keys []string |
| 107 | + for i := 0; i < redemption.Count; i++ { |
| 108 | + key := common.GetUUID() |
| 109 | + cleanRedemption := model.Redemption{ |
| 110 | + UserId: c.GetInt("id"), |
| 111 | + Name: redemption.Name, |
| 112 | + Key: key, |
| 113 | + CreatedTime: common.GetTimestamp(), |
| 114 | + Quota: redemption.Quota, |
| 115 | + } |
| 116 | + err = cleanRedemption.Insert() |
| 117 | + if err != nil { |
| 118 | + c.JSON(http.StatusOK, gin.H{ |
| 119 | + "success": false, |
| 120 | + "message": err.Error(), |
| 121 | + "data": keys, |
| 122 | + }) |
| 123 | + return |
| 124 | + } |
| 125 | + keys = append(keys, key) |
| 126 | + } |
| 127 | + c.JSON(http.StatusOK, gin.H{ |
| 128 | + "success": true, |
| 129 | + "message": "", |
| 130 | + "data": keys, |
| 131 | + }) |
| 132 | + return |
| 133 | +} |
| 134 | + |
| 135 | +func DeleteRedemption(c *gin.Context) { |
| 136 | + id, _ := strconv.Atoi(c.Param("id")) |
| 137 | + err := model.DeleteRedemptionById(id) |
| 138 | + if err != nil { |
| 139 | + c.JSON(http.StatusOK, gin.H{ |
| 140 | + "success": false, |
| 141 | + "message": err.Error(), |
| 142 | + }) |
| 143 | + return |
| 144 | + } |
| 145 | + c.JSON(http.StatusOK, gin.H{ |
| 146 | + "success": true, |
| 147 | + "message": "", |
| 148 | + }) |
| 149 | + return |
| 150 | +} |
| 151 | + |
| 152 | +func UpdateRedemption(c *gin.Context) { |
| 153 | + statusOnly := c.Query("status_only") |
| 154 | + redemption := model.Redemption{} |
| 155 | + err := c.ShouldBindJSON(&redemption) |
| 156 | + if err != nil { |
| 157 | + c.JSON(http.StatusOK, gin.H{ |
| 158 | + "success": false, |
| 159 | + "message": err.Error(), |
| 160 | + }) |
| 161 | + return |
| 162 | + } |
| 163 | + cleanRedemption, err := model.GetRedemptionById(redemption.Id) |
| 164 | + if err != nil { |
| 165 | + c.JSON(http.StatusOK, gin.H{ |
| 166 | + "success": false, |
| 167 | + "message": err.Error(), |
| 168 | + }) |
| 169 | + return |
| 170 | + } |
| 171 | + if statusOnly != "" { |
| 172 | + cleanRedemption.Status = redemption.Status |
| 173 | + } else { |
| 174 | + // If you add more fields, please also update redemption.Update() |
| 175 | + cleanRedemption.Name = redemption.Name |
| 176 | + cleanRedemption.Quota = redemption.Quota |
| 177 | + } |
| 178 | + err = cleanRedemption.Update() |
| 179 | + if err != nil { |
| 180 | + c.JSON(http.StatusOK, gin.H{ |
| 181 | + "success": false, |
| 182 | + "message": err.Error(), |
| 183 | + }) |
| 184 | + return |
| 185 | + } |
| 186 | + c.JSON(http.StatusOK, gin.H{ |
| 187 | + "success": true, |
| 188 | + "message": "", |
| 189 | + "data": cleanRedemption, |
| 190 | + }) |
| 191 | + return |
| 192 | +} |
0 commit comments