init: 1.0.0
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/pkg/cache"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
cacheSystemIdPrefix = "cache:System:id:"
|
||||
cacheSystemKeyPrefix = "cache:System:key:"
|
||||
)
|
||||
var _ Model = (*customSystemModel)(nil)
|
||||
|
||||
type (
|
||||
Model interface {
|
||||
systemModel
|
||||
customSystemLogicModel
|
||||
}
|
||||
systemModel interface {
|
||||
Insert(ctx context.Context, data *System) error
|
||||
FindOne(ctx context.Context, id int64) (*System, error)
|
||||
FindOneByKey(ctx context.Context, email string) (*System, error)
|
||||
Update(ctx context.Context, data *System) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
||||
}
|
||||
|
||||
customSystemModel struct {
|
||||
*defaultSystemModel
|
||||
}
|
||||
defaultSystemModel struct {
|
||||
cache.CachedConn
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
func newSystemModel(db *gorm.DB, c *redis.Client) *defaultSystemModel {
|
||||
return &defaultSystemModel{
|
||||
CachedConn: cache.NewConn(db, c),
|
||||
table: "`System`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultSystemModel) getCacheKeys(data *System) []string {
|
||||
if data == nil {
|
||||
return []string{}
|
||||
}
|
||||
SystemIdKey := fmt.Sprintf("%s%v", cacheSystemIdPrefix, data.Id)
|
||||
cacheKeys := []string{
|
||||
SystemIdKey,
|
||||
}
|
||||
return cacheKeys
|
||||
}
|
||||
|
||||
func (m *defaultSystemModel) FindOneByKey(ctx context.Context, key string) (*System, error) {
|
||||
system := new(System)
|
||||
cacheKey := fmt.Sprintf("%s%v", cacheSystemKeyPrefix, key)
|
||||
err := m.QueryCtx(ctx, system, cacheKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&System{}).Where("`key` = ?", key).First(v).Error
|
||||
})
|
||||
return system, err
|
||||
}
|
||||
|
||||
func (m *defaultSystemModel) Insert(ctx context.Context, data *System) error {
|
||||
err := m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
return conn.Create(&data).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultSystemModel) FindOne(ctx context.Context, id int64) (*System, error) {
|
||||
SystemIdKey := fmt.Sprintf("%s%v", cacheSystemIdPrefix, id)
|
||||
var resp System
|
||||
err := m.QueryCtx(ctx, &resp, SystemIdKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&System{}).Where("`id` = ?", id).First(&resp).Error
|
||||
})
|
||||
switch {
|
||||
case err == nil:
|
||||
return &resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultSystemModel) Update(ctx context.Context, data *System) error {
|
||||
old, err := m.FindOne(ctx, data.Id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
db := conn
|
||||
return db.Save(data).Error
|
||||
}, m.getCacheKeys(old)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultSystemModel) Delete(ctx context.Context, id int64) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
db := conn
|
||||
return db.Delete(&System{}, id).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultSystemModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
||||
return m.TransactCtx(ctx, fn)
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/internal/config"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type customSystemLogicModel interface {
|
||||
GetSmsConfig(ctx context.Context) ([]*System, error)
|
||||
GetSiteConfig(ctx context.Context) ([]*System, error)
|
||||
GetSubscribeConfig(ctx context.Context) ([]*System, error)
|
||||
GetRegisterConfig(ctx context.Context) ([]*System, error)
|
||||
GetVerifyConfig(ctx context.Context) ([]*System, error)
|
||||
GetNodeConfig(ctx context.Context) ([]*System, error)
|
||||
GetInviteConfig(ctx context.Context) ([]*System, error)
|
||||
GetTosConfig(ctx context.Context) ([]*System, error)
|
||||
GetCurrencyConfig(ctx context.Context) ([]*System, error)
|
||||
GetVerifyCodeConfig(ctx context.Context) ([]*System, error)
|
||||
UpdateNodeMultiplierConfig(ctx context.Context, config string) error
|
||||
FindNodeMultiplierConfig(ctx context.Context) (*System, error)
|
||||
}
|
||||
|
||||
// NewModel returns a model for the database table.
|
||||
func NewModel(conn *gorm.DB, c *redis.Client) Model {
|
||||
return &customSystemModel{
|
||||
defaultSystemModel: newSystemModel(conn, c),
|
||||
}
|
||||
}
|
||||
|
||||
// GetSmsConfig returns the sms config.
|
||||
func (m *customSystemModel) GetSmsConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.SmsConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "sms").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// GetSiteConfig returns the site config.
|
||||
func (m *customSystemModel) GetSiteConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.SiteConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "site").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// GetEmailConfig returns the email config.
|
||||
func (m *customSystemModel) GetEmailConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.EmailSmtpConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "email").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// GetSubscribeConfig returns the subscribe config.
|
||||
func (m *customSystemModel) GetSubscribeConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.SubscribeConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "subscribe").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// GetRegisterConfig returns the register config.
|
||||
func (m *customSystemModel) GetRegisterConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.RegisterConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "register").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// GetVerifyConfig returns the verify config.
|
||||
func (m *customSystemModel) GetVerifyConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.VerifyConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "verify").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// GetNodeConfig returns the server config.
|
||||
func (m *customSystemModel) GetNodeConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.NodeConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "server").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// GetInviteConfig returns the invite config.
|
||||
func (m *customSystemModel) GetInviteConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.InviteConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "invite").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// GetTelegramConfig returns the telegram config.
|
||||
func (m *customSystemModel) GetTelegramConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.TelegramConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "telegram").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// GetTosConfig returns the tos config.
|
||||
func (m *customSystemModel) GetTosConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.TosConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "tos").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// GetCurrencyConfig returns the currency config.
|
||||
func (m *customSystemModel) GetCurrencyConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.CurrencyConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "currency").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
|
||||
func (m *customSystemModel) UpdateNodeMultiplierConfig(ctx context.Context, config string) error {
|
||||
return m.ExecNoCacheCtx(ctx, func(conn *gorm.DB) error {
|
||||
return conn.Model(&System{}).Where("`category` = ? AND `key` = ?", "server", "NodeMultiplierConfig").Update("value", config).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (m *customSystemModel) FindNodeMultiplierConfig(ctx context.Context) (*System, error) {
|
||||
var data System
|
||||
err := m.QueryNoCacheCtx(ctx, &data, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ? AND `key` = ?", "server", "NodeMultiplierConfig").Find(v).Error
|
||||
})
|
||||
return &data, err
|
||||
}
|
||||
|
||||
// GetVerifyCodeConfig returns the verify code config.
|
||||
|
||||
func (m *customSystemModel) GetVerifyCodeConfig(ctx context.Context) ([]*System, error) {
|
||||
var configs []*System
|
||||
err := m.QueryCtx(ctx, &configs, config.VerifyCodeConfigKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Where("`category` = ?", "verify_code").Find(v).Error
|
||||
})
|
||||
return configs, err
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package system
|
||||
|
||||
import "time"
|
||||
|
||||
type System struct {
|
||||
Id int64 `gorm:"primarykey"`
|
||||
Category string `gorm:"type:varchar(100);default:'';not null;comment:Category"`
|
||||
Key string `gorm:"index:index_key;unique;type:varchar(100);default:'';not null;comment:Key Name"`
|
||||
Value string `gorm:"type:text;not null;comment:Key Value"`
|
||||
Type string `gorm:"type:varchar(50);default:'';not null;comment:Type"`
|
||||
Desc string `gorm:"type:text;not null;comment:Description"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
}
|
||||
|
||||
func (System) TableName() string {
|
||||
return "system"
|
||||
}
|
||||
Reference in New Issue
Block a user