From f18e51e3a9ead60131ae06ac8361c95fc1271605 Mon Sep 17 00:00:00 2001 From: fengsilin Date: Wed, 29 Apr 2026 15:05:09 +0800 Subject: [PATCH] feat: add redemption remark support --- model/redemption.go | 7 ++-- model/redemption_test.go | 83 +++++++++++++++++++++++++++++++++------- 2 files changed, 73 insertions(+), 17 deletions(-) diff --git a/model/redemption.go b/model/redemption.go index 507c563..69ce6cd 100644 --- a/model/redemption.go +++ b/model/redemption.go @@ -20,6 +20,7 @@ type Redemption struct { Key string `json:"key" gorm:"type:char(32);uniqueIndex"` Status int `json:"status" gorm:"default:1"` Name string `json:"name" gorm:"index"` + Remark string `json:"remark"` Quota int `json:"quota" gorm:"default:100"` CreatedTime int64 `json:"created_time" gorm:"bigint"` RedeemedTime int64 `json:"redeemed_time" gorm:"bigint"` @@ -79,9 +80,9 @@ func SearchRedemptions(keyword string, startIdx int, num int) (redemptions []*Re // Only try to convert to ID if the string represents a valid integer if id, err := strconv.Atoi(keyword); err == nil { - query = query.Where("id = ? OR name LIKE ?", id, keyword+"%") + query = query.Where("id = ? OR name LIKE ? OR remark LIKE ?", id, keyword+"%", keyword+"%") } else { - query = query.Where("name LIKE ?", keyword+"%") + query = query.Where("name LIKE ? OR remark LIKE ?", keyword+"%", keyword+"%") } // Get total count @@ -187,7 +188,7 @@ func (redemption *Redemption) SelectUpdate() error { // Update Make sure your token's fields is completed, because this will update non-zero values func (redemption *Redemption) Update() error { var err error - err = DB.Model(redemption).Select("name", "status", "quota", "redeemed_time", "expired_time").Updates(redemption).Error + err = DB.Model(redemption).Select("name", "remark", "status", "quota", "redeemed_time", "expired_time").Updates(redemption).Error return err } diff --git a/model/redemption_test.go b/model/redemption_test.go index 11179ed..14d0947 100644 --- a/model/redemption_test.go +++ b/model/redemption_test.go @@ -48,13 +48,13 @@ func TestRedeem_Success(t *testing.T) { // 创建兑换码 redemption := Redemption{ - Id: 1, - UserId: 1, - Key: "test-key-123", - Status: common.RedemptionCodeStatusEnabled, - Quota: 50000, - CreatedTime: common.GetTimestamp(), - ExpiredTime: 0, // 永不过期 + Id: 1, + UserId: 1, + Key: "test-key-123", + Status: common.RedemptionCodeStatusEnabled, + Quota: 50000, + CreatedTime: common.GetTimestamp(), + ExpiredTime: 0, // 永不过期 } require.NoError(t, db.Create(&redemption).Error) @@ -148,13 +148,13 @@ func TestRedeem_Expired(t *testing.T) { // 创建已过期的兑换码 redemption := Redemption{ - Id: 1, - UserId: 1, - Key: "expired-key", - Status: common.RedemptionCodeStatusEnabled, - Quota: 50000, - CreatedTime: common.GetTimestamp() - 86400, - ExpiredTime: common.GetTimestamp() - 3600, // 已过期 + Id: 1, + UserId: 1, + Key: "expired-key", + Status: common.RedemptionCodeStatusEnabled, + Quota: 50000, + CreatedTime: common.GetTimestamp() - 86400, + ExpiredTime: common.GetTimestamp() - 3600, // 已过期 } require.NoError(t, db.Create(&redemption).Error) @@ -252,3 +252,58 @@ func TestRedeem_SyncedUser(t *testing.T) { require.NoError(t, db.First(&updatedUser, 100).Error) assert.Equal(t, 150000, updatedUser.Quota) } + +func TestRedemptionUpdateRemarkPersistsRemark(t *testing.T) { + db := setupRedemptionDB(t) + + redemption := Redemption{ + Id: 1, + UserId: 1, + Key: "remark-update-key", + Status: common.RedemptionCodeStatusEnabled, + Name: "starter", + Remark: "initial-note", + Quota: 100, + CreatedTime: common.GetTimestamp(), + } + require.NoError(t, db.Create(&redemption).Error) + + redemption.Remark = "vip-updated" + require.NoError(t, redemption.Update()) + + var updated Redemption + require.NoError(t, db.First(&updated, redemption.Id).Error) + assert.Equal(t, "vip-updated", updated.Remark) +} + +func TestSearchRedemptionsByRemarkRemark(t *testing.T) { + db := setupRedemptionDB(t) + + require.NoError(t, db.Create(&Redemption{ + Id: 1, + UserId: 1, + Key: "remark-search-key", + Status: common.RedemptionCodeStatusEnabled, + Name: "starter-pack", + Remark: "vip benefit", + Quota: 100, + CreatedTime: common.GetTimestamp(), + }).Error) + require.NoError(t, db.Create(&Redemption{ + Id: 2, + UserId: 1, + Key: "remark-search-other", + Status: common.RedemptionCodeStatusEnabled, + Name: "basic-pack", + Remark: "standard benefit", + Quota: 100, + CreatedTime: common.GetTimestamp(), + }).Error) + + results, total, err := SearchRedemptions("vip", 0, 10) + require.NoError(t, err) + require.Equal(t, int64(1), total) + require.Len(t, results, 1) + assert.Equal(t, 1, results[0].Id) + assert.Equal(t, "vip benefit", results[0].Remark) +}