MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / SearchRedemptions

Function SearchRedemptions

model/redemption.go:61–101  ·  view source on GitHub ↗
(keyword string, startIdx int, num int)

Source from the content-addressed store, hash-verified

59}
60
61func SearchRedemptions(keyword string, startIdx int, num int) (redemptions []*Redemption, total int64, err error) {
62 tx := DB.Begin()
63 if tx.Error != nil {
64 return nil, 0, tx.Error
65 }
66 defer func() {
67 if r := recover(); r != nil {
68 tx.Rollback()
69 }
70 }()
71
72 // Build query based on keyword type
73 query := tx.Model(&Redemption{})
74
75 // Only try to convert to ID if the string represents a valid integer
76 if id, err := strconv.Atoi(keyword); err == nil {
77 query = query.Where("id = ? OR name LIKE ?", id, keyword+"%")
78 } else {
79 query = query.Where("name LIKE ?", keyword+"%")
80 }
81
82 // Get total count
83 err = query.Count(&total).Error
84 if err != nil {
85 tx.Rollback()
86 return nil, 0, err
87 }
88
89 // Get paginated data
90 err = query.Order("id desc").Limit(num).Offset(startIdx).Find(&redemptions).Error
91 if err != nil {
92 tx.Rollback()
93 return nil, 0, err
94 }
95
96 if err = tx.Commit().Error; err != nil {
97 return nil, 0, err
98 }
99
100 return redemptions, total, nil
101}
102
103func GetRedemptionById(id int) (*Redemption, error) {
104 if id == 0 {

Callers 1

SearchRedemptionsFunction · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected