Skip to content

Commit 331177d

Browse files
committed
fix: return quota to user when delete token (close #37)
1 parent 4fed003 commit 331177d

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

controller/relay.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func relayHelper(c *gin.Context) error {
137137
ratio = common.RatioGPT3dot5
138138
}
139139
quota = int(float64(quota) * ratio)
140-
err := model.ConsumeTokenQuota(tokenId, quota)
140+
err := model.DecreaseTokenQuota(tokenId, quota)
141141
if err != nil {
142142
common.SysError("Error consuming token remain quota: " + err.Error())
143143
}

model/redemption.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func Redeem(key string, tokenId int) (quota int, err error) {
5555
if redemption.Status != common.RedemptionCodeStatusEnabled {
5656
return 0, errors.New("该兑换码已被使用")
5757
}
58-
err = TopUpTokenQuota(tokenId, redemption.Quota)
58+
err = IncreaseTokenQuota(tokenId, redemption.Quota)
5959
if err != nil {
6060
return 0, err
6161
}

model/token.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,26 @@ func DeleteTokenById(id int, userId int) (err error) {
116116
if err != nil {
117117
return err
118118
}
119+
quota := token.RemainQuota
120+
if quota != 0 {
121+
if quota > 0 {
122+
err = IncreaseUserQuota(userId, quota)
123+
} else {
124+
err = DecreaseUserQuota(userId, quota)
125+
}
126+
}
127+
if err != nil {
128+
return err
129+
}
119130
return token.Delete()
120131
}
121132

122-
func ConsumeTokenQuota(id int, quota int) (err error) {
123-
err = DB.Model(&Token{}).Where("id = ?", id).Update("remain_quota", gorm.Expr("remain_quota - ?", quota)).Error
133+
func IncreaseTokenQuota(id int, quota int) (err error) {
134+
err = DB.Model(&Token{}).Where("id = ?", id).Update("remain_quota", gorm.Expr("remain_quota + ?", quota)).Error
124135
return err
125136
}
126137

127-
func TopUpTokenQuota(id int, quota int) (err error) {
128-
err = DB.Model(&Token{}).Where("id = ?", id).Update("remain_quota", gorm.Expr("remain_quota + ?", quota)).Error
138+
func DecreaseTokenQuota(id int, quota int) (err error) {
139+
err = DB.Model(&Token{}).Where("id = ?", id).Update("remain_quota", gorm.Expr("remain_quota - ?", quota)).Error
129140
return err
130141
}

model/user.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ func GetUserQuota(id int) (quota int, err error) {
225225
return quota, err
226226
}
227227

228+
func IncreaseUserQuota(id int, quota int) (err error) {
229+
err = DB.Model(&User{}).Where("id = ?", id).Update("quota", gorm.Expr("quota + ?", quota)).Error
230+
return err
231+
}
232+
228233
func DecreaseUserQuota(id int, quota int) (err error) {
229234
err = DB.Model(&User{}).Where("id = ?", id).Update("quota", gorm.Expr("quota - ?", quota)).Error
230235
return err

0 commit comments

Comments
 (0)