(c *gin.Context)
| 76 | } |
| 77 | |
| 78 | func AddRedemption(c *gin.Context) { |
| 79 | redemption := model.Redemption{} |
| 80 | err := c.ShouldBindJSON(&redemption) |
| 81 | if err != nil { |
| 82 | c.JSON(http.StatusOK, gin.H{ |
| 83 | "success": false, |
| 84 | "message": err.Error(), |
| 85 | }) |
| 86 | return |
| 87 | } |
| 88 | if len(redemption.Name) == 0 || len(redemption.Name) > 20 { |
| 89 | c.JSON(http.StatusOK, gin.H{ |
| 90 | "success": false, |
| 91 | "message": "兑换码名称长度必须在1-20之间", |
| 92 | }) |
| 93 | return |
| 94 | } |
| 95 | if redemption.Count <= 0 { |
| 96 | c.JSON(http.StatusOK, gin.H{ |
| 97 | "success": false, |
| 98 | "message": "兑换码个数必须大于0", |
| 99 | }) |
| 100 | return |
| 101 | } |
| 102 | if redemption.Count > 100 { |
| 103 | c.JSON(http.StatusOK, gin.H{ |
| 104 | "success": false, |
| 105 | "message": "一次兑换码批量生成的个数不能大于 100", |
| 106 | }) |
| 107 | return |
| 108 | } |
| 109 | var keys []string |
| 110 | for i := 0; i < redemption.Count; i++ { |
| 111 | key := random.GetUUID() |
| 112 | cleanRedemption := model.Redemption{ |
| 113 | UserId: c.GetInt(ctxkey.Id), |
| 114 | Name: redemption.Name, |
| 115 | Key: key, |
| 116 | CreatedTime: helper.GetTimestamp(), |
| 117 | Quota: redemption.Quota, |
| 118 | } |
| 119 | err = cleanRedemption.Insert() |
| 120 | if err != nil { |
| 121 | c.JSON(http.StatusOK, gin.H{ |
| 122 | "success": false, |
| 123 | "message": err.Error(), |
| 124 | "data": keys, |
| 125 | }) |
| 126 | return |
| 127 | } |
| 128 | keys = append(keys, key) |
| 129 | } |
| 130 | c.JSON(http.StatusOK, gin.H{ |
| 131 | "success": true, |
| 132 | "message": "", |
| 133 | "data": keys, |
| 134 | }) |
| 135 | return |
nothing calls this directly
no test coverage detected