@@ -0,0 +1,106 @@
|
||||
package subscribe_promo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/perfect-panel/server/pkg/cache"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var _ Model = (*customSubscribePromoModel)(nil)
|
||||
|
||||
var cacheSubscribePromoIdPrefix = "cache:subscribe_promo:id:"
|
||||
|
||||
type (
|
||||
Model interface {
|
||||
subscribePromoModel
|
||||
customSubscribePromoLogicModel
|
||||
}
|
||||
|
||||
subscribePromoModel interface {
|
||||
Insert(ctx context.Context, data *SubscribePromo, tx ...*gorm.DB) error
|
||||
FindOne(ctx context.Context, id int64) (*SubscribePromo, error)
|
||||
Update(ctx context.Context, data *SubscribePromo, tx ...*gorm.DB) error
|
||||
Delete(ctx context.Context, id int64, tx ...*gorm.DB) error
|
||||
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
||||
}
|
||||
|
||||
customSubscribePromoModel struct {
|
||||
*defaultSubscribePromoModel
|
||||
}
|
||||
defaultSubscribePromoModel struct {
|
||||
cache.CachedConn
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
func newSubscribePromoModel(db *gorm.DB, c *redis.Client) *defaultSubscribePromoModel {
|
||||
return &defaultSubscribePromoModel{
|
||||
CachedConn: cache.NewConn(db, c),
|
||||
table: "`subscribe_promo`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultSubscribePromoModel) getCacheKeys(data *SubscribePromo) []string {
|
||||
if data == nil {
|
||||
return []string{}
|
||||
}
|
||||
return []string{fmt.Sprintf("%s%v", cacheSubscribePromoIdPrefix, data.Id)}
|
||||
}
|
||||
|
||||
func (m *defaultSubscribePromoModel) Insert(ctx context.Context, data *SubscribePromo, tx ...*gorm.DB) error {
|
||||
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return conn.Create(data).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
}
|
||||
|
||||
func (m *defaultSubscribePromoModel) FindOne(ctx context.Context, id int64) (*SubscribePromo, error) {
|
||||
key := fmt.Sprintf("%s%v", cacheSubscribePromoIdPrefix, id)
|
||||
var resp SubscribePromo
|
||||
err := m.QueryCtx(ctx, &resp, key, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&SubscribePromo{}).Where("`id` = ?", id).First(v).Error
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (m *defaultSubscribePromoModel) Update(ctx context.Context, data *SubscribePromo, tx ...*gorm.DB) error {
|
||||
old, err := m.FindOne(ctx, data.Id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return conn.Save(data).Error
|
||||
}, m.getCacheKeys(old)...)
|
||||
}
|
||||
|
||||
func (m *defaultSubscribePromoModel) Delete(ctx context.Context, id int64, tx ...*gorm.DB) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return conn.Delete(&SubscribePromo{}, id).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
}
|
||||
|
||||
func (m *defaultSubscribePromoModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
||||
return m.TransactCtx(ctx, fn)
|
||||
}
|
||||
Reference in New Issue
Block a user