Update redemption.go

This commit is contained in:
analogpvt 2023-05-19 23:20:57 +05:30 committed by GitHub
parent c19ac576d9
commit 938981fb42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,7 +32,7 @@ func SearchRedemptions(keyword string) (redemptions []*Redemption, err error) {
func GetRedemptionById(id int) (*Redemption, error) { func GetRedemptionById(id int) (*Redemption, error) {
if id == 0 { if id == 0 {
return nil, errors.New("id 为空") return nil, errors.New("id Empty")
} }
redemption := Redemption{Id: id} redemption := Redemption{Id: id}
var err error = nil var err error = nil
@ -42,18 +42,18 @@ func GetRedemptionById(id int) (*Redemption, error) {
func Redeem(key string, userId int) (quota int, err error) { func Redeem(key string, userId int) (quota int, err error) {
if key == "" { if key == "" {
return 0, errors.New("未提供兑换码") return 0, errors.New("Redemption code not provided.")
} }
if userId == 0 { if userId == 0 {
return 0, errors.New("无效的 user id") return 0, errors.New("Invalid user id")
} }
redemption := &Redemption{} redemption := &Redemption{}
err = DB.Where("`key` = ?", key).First(redemption).Error err = DB.Where("`key` = ?", key).First(redemption).Error
if err != nil { if err != nil {
return 0, errors.New("无效的兑换码") return 0, errors.New("Invalid redemption code.")
} }
if redemption.Status != common.RedemptionCodeStatusEnabled { if redemption.Status != common.RedemptionCodeStatusEnabled {
return 0, errors.New("该兑换码已被使用") return 0, errors.New("This redemption code has already been used.")
} }
err = IncreaseUserQuota(userId, redemption.Quota) err = IncreaseUserQuota(userId, redemption.Quota)
if err != nil { if err != nil {
@ -64,7 +64,7 @@ func Redeem(key string, userId int) (quota int, err error) {
redemption.Status = common.RedemptionCodeStatusUsed redemption.Status = common.RedemptionCodeStatusUsed
err := redemption.SelectUpdate() err := redemption.SelectUpdate()
if err != nil { if err != nil {
common.SysError("更新兑换码状态失败" + err.Error()) common.SysError("Failed to update redemption code status." + err.Error())
} }
}() }()
return redemption.Quota, nil return redemption.Quota, nil