(c *gin.Context)
| 57 | } |
| 58 | |
| 59 | func AddRedemption(c *gin.Context) { |
| 60 | redemption := model.Redemption{} |
| 61 | err := c.ShouldBindJSON(&redemption) |
| 62 | if err != nil { |
| 63 | common.ApiError(c, err) |
| 64 | return |
| 65 | } |
| 66 | if len(redemption.Name) == 0 || len(redemption.Name) > 20 { |
| 67 | c.JSON(http.StatusOK, gin.H{ |
| 68 | "success": false, |
| 69 | "message": "兑换码名称长度必须在1-20之间", |
| 70 | }) |
| 71 | return |
| 72 | } |
| 73 | if redemption.Count <= 0 { |
| 74 | c.JSON(http.StatusOK, gin.H{ |
| 75 | "success": false, |
| 76 | "message": "兑换码个数必须大于0", |
| 77 | }) |
| 78 | return |
| 79 | } |
| 80 | if redemption.Count > 100 { |
| 81 | c.JSON(http.StatusOK, gin.H{ |
| 82 | "success": false, |
| 83 | "message": "一次兑换码批量生成的个数不能大于 100", |
| 84 | }) |
| 85 | return |
| 86 | } |
| 87 | if err := validateExpiredTime(redemption.ExpiredTime); err != nil { |
| 88 | c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()}) |
| 89 | return |
| 90 | } |
| 91 | var keys []string |
| 92 | for i := 0; i < redemption.Count; i++ { |
| 93 | key := common.GetUUID() |
| 94 | cleanRedemption := model.Redemption{ |
| 95 | UserId: c.GetInt("id"), |
| 96 | Name: redemption.Name, |
| 97 | Key: key, |
| 98 | CreatedTime: common.GetTimestamp(), |
| 99 | Quota: redemption.Quota, |
| 100 | ExpiredTime: redemption.ExpiredTime, |
| 101 | } |
| 102 | err = cleanRedemption.Insert() |
| 103 | if err != nil { |
| 104 | c.JSON(http.StatusOK, gin.H{ |
| 105 | "success": false, |
| 106 | "message": err.Error(), |
| 107 | "data": keys, |
| 108 | }) |
| 109 | return |
| 110 | } |
| 111 | keys = append(keys, key) |
| 112 | } |
| 113 | c.JSON(http.StatusOK, gin.H{ |
| 114 | "success": true, |
| 115 | "message": "", |
| 116 | "data": keys, |
nothing calls this directly
no test coverage detected